Skip to content

Commit 4a222a0

Browse files
committed
[Sema][Diagnostics] Add fixit for warning when inferring an undesirable type
1 parent 2748207 commit 4a222a0

File tree

5 files changed

+12
-12
lines changed

5 files changed

+12
-12
lines changed

lib/Sema/TypeCheckPattern.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,8 +1018,8 @@ bool TypeChecker::coercePatternToType(Pattern *&P, TypeResolution resolution,
10181018
!(options & TypeResolutionFlags::FromNonInferredPattern)) {
10191019
diags.diagnose(NP->getLoc(), diag, NP->getDecl()->getName(), type,
10201020
NP->getDecl()->isLet());
1021-
diags.diagnose(NP->getLoc(),
1022-
diag::add_explicit_type_annotation_to_silence);
1021+
diags.diagnose(NP->getLoc(), diag::add_explicit_type_annotation_to_silence)
1022+
.fixItInsertAfter(var->getNameLoc(), ": " + type->getString());
10231023
}
10241024

10251025
return false;

test/decl/var/lazy_properties.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ struct Outer {
112112

113113
lazy var y = {_ = 3}()
114114
// expected-warning@-1 {{variable 'y' inferred to have type '()', which may be unexpected}}
115-
// expected-note@-2 {{add an explicit type annotation to silence this warning}}
115+
// expected-note@-2 {{add an explicit type annotation to silence this warning}} {{15-15=: ()}}
116116
}
117117
}
118118

test/decl/var/properties.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,11 +1272,11 @@ class WeakFixItTest {
12721272

12731273
// SR-8811 (Warning)
12741274

1275-
let sr8811a = fatalError() // expected-warning {{constant 'sr8811a' inferred to have type 'Never', which is an enum with no cases}} expected-note {{add an explicit type annotation to silence this warning}}
1275+
let sr8811a = fatalError() // expected-warning {{constant 'sr8811a' inferred to have type 'Never', which is an enum with no cases}} expected-note {{add an explicit type annotation to silence this warning}} {{12-12=: Never}}
12761276

12771277
let sr8811b: Never = fatalError() // Ok
12781278

1279-
let sr8811c = (16, fatalError()) // expected-warning {{constant 'sr8811c' inferred to have type '(Int, Never)', which contains an enum with no cases}} expected-note {{add an explicit type annotation to silence this warning}}
1279+
let sr8811c = (16, fatalError()) // expected-warning {{constant 'sr8811c' inferred to have type '(Int, Never)', which contains an enum with no cases}} expected-note {{add an explicit type annotation to silence this warning}} {{12-12=: (Int, Never)}}
12801280

12811281
let sr8811d: (Int, Never) = (16, fatalError()) // Ok
12821282

@@ -1292,11 +1292,11 @@ class SR_10995 {
12921292
}
12931293

12941294
func sr_10995_foo() {
1295-
let doubleOptionalNever = makeDoubleOptionalNever() // expected-warning {{constant 'doubleOptionalNever' inferred to have type 'Never??', which may be unexpected}}
1296-
// expected-note@-1 {{add an explicit type annotation to silence this warning}}
1295+
let doubleOptionalNever = makeDoubleOptionalNever() // expected-warning {{constant 'doubleOptionalNever' inferred to have type 'Never??', which may be unexpected}}
1296+
// expected-note@-1 {{add an explicit type annotation to silence this warning}} {{28-28=: Never??}}
12971297
// expected-warning@-2 {{initialization of immutable value 'doubleOptionalNever' was never used; consider replacing with assignment to '_' or removing it}}
12981298
let singleOptionalNever = makeSingleOptionalNever() // expected-warning {{constant 'singleOptionalNever' inferred to have type 'Never?', which may be unexpected}}
1299-
// expected-note@-1 {{add an explicit type annotation to silence this warning}}
1299+
// expected-note@-1 {{add an explicit type annotation to silence this warning}} {{28-28=: Never?}}
13001300
// expected-warning@-2 {{initialization of immutable value 'singleOptionalNever' was never used; consider replacing with assignment to '_' or removing it}}
13011301
}
13021302
}

test/decl/var/variables.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ struct Broken {
3636

3737
// rdar://16252090 - Warning when inferring empty tuple type for declarations
3838
var emptyTuple = testShadowing() // expected-warning {{variable 'emptyTuple' inferred to have type '()'}} \
39-
// expected-note {{add an explicit type annotation to silence this warning}}
39+
// expected-note {{add an explicit type annotation to silence this warning}} {{15-15=: ()}}
4040

4141
// rdar://15263687 - Diagnose variables inferenced to 'AnyObject'
4242
var ao1 : AnyObject
4343
var ao2 = ao1
4444

4545
var aot1 : AnyObject.Type
4646
var aot2 = aot1 // expected-warning {{variable 'aot2' inferred to have type 'AnyObject.Type', which may be unexpected}} \
47-
// expected-note {{add an explicit type annotation to silence this warning}}
47+
// expected-note {{add an explicit type annotation to silence this warning}} {{9-9=: AnyObject.Type}}
4848

4949

5050
for item in [AnyObject]() { // No warning in for-each loop.

test/expr/expressions.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -588,9 +588,9 @@ func conversionTest(_ a: inout Double, b: inout Int) {
588588
var pi_f3 = float.init(getPi()) // expected-error {{ambiguous use of 'init(_:)'}}
589589
var pi_f4 = float.init(pi_f)
590590

591-
var e = Empty(f) // expected-warning {{variable 'e' inferred to have type 'Empty', which is an enum with no cases}} expected-note {{add an explicit type annotation to silence this warning}}
591+
var e = Empty(f) // expected-warning {{variable 'e' inferred to have type 'Empty', which is an enum with no cases}} expected-note {{add an explicit type annotation to silence this warning}} {{8-8=: Empty}}
592592
var e2 = Empty(d) // expected-error{{cannot convert value of type 'Double' to expected argument type 'Float'}}
593-
var e3 = Empty(Float(d)) // expected-warning {{variable 'e3' inferred to have type 'Empty', which is an enum with no cases}} expected-note {{add an explicit type annotation to silence this warning}}
593+
var e3 = Empty(Float(d)) // expected-warning {{variable 'e3' inferred to have type 'Empty', which is an enum with no cases}} expected-note {{add an explicit type annotation to silence this warning}} {{9-9=: Empty}}
594594
}
595595

596596
struct Rule {

0 commit comments

Comments
 (0)