Skip to content

Commit c8f50b3

Browse files
committed
[reference-bindings] Make the never mutated diagnostic only apply to vars, not also to inout bindings.
1 parent adeae2f commit c8f50b3

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

lib/Sema/MiscDiagnostics.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3526,7 +3526,8 @@ VarDeclUsageChecker::~VarDeclUsageChecker() {
35263526

35273527
// If this is a mutable 'var', and it was never written to, suggest
35283528
// upgrading to 'let'.
3529-
if (!var->isLet() && (access & RK_Written) == 0 &&
3529+
if (var->getIntroducer() == VarDecl::Introducer::Var
3530+
&& (access & RK_Written) == 0 &&
35303531
// Don't warn if we have something like "let (x,y) = ..." and 'y' was
35313532
// never mutated, but 'x' was.
35323533
!isVarDeclPartOfPBDThatHadSomeMutation(var)) {

test/SILOptimizer/reference_bindings.swift

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,16 @@
33
func varBindingTest() {
44
var x = "1"
55
x = "1"
6-
inout x1 = x // expected-warning {{variable 'x1' was never mutated; consider changing to 'let' constant}}
7-
// expected-error @-1 {{overlapping accesses to 'x', but modification requires exclusive access; consider copying to a local variable}}
6+
inout x1 = x // expected-error {{overlapping accesses to 'x', but modification requires exclusive access; consider copying to a local variable}}
87
let _ = x1
98
inout x2 = x // expected-note {{conflicting access is here}}
10-
// expected-warning @-1 {{variable 'x2' was never mutated; consider changing to 'let' constant}}
119
let _ = x2
1210
}
1311

1412
func varBindingTest2() {
1513
var x = "1"
1614
x = "1"
17-
inout x1 = x // expected-warning {{variable 'x1' was never mutated; consider changing to 'let' constant}}
18-
// expected-error @-1 {{overlapping accesses to 'x', but modification requires exclusive access; consider copying to a local variable}}
15+
inout x1 = x // expected-error {{overlapping accesses to 'x', but modification requires exclusive access; consider copying to a local variable}}
1916
let _ = x1
2017
x = "2" // expected-note {{conflicting access is here}}
2118
}
@@ -24,7 +21,7 @@ func varBindingTest3() {
2421
var x = "1"
2522
x = "1"
2623
do {
27-
inout x1 = x // expected-warning {{variable 'x1' was never mutated; consider changing to 'let' constant}}
24+
inout x1 = x
2825
let _ = x1
2926
}
3027
x = "2"

test/Sema/reference_bindings.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,16 @@ func foo(_ arg: String, _ consumingArg: consuming String, _ borrowingArg: borrow
5252
inout getterStructVarFieldX = varStruct.sGetter // expected-error {{inout bindings must be bound to an lvalue}}
5353
inout modifyStructVarFieldX = varStruct.sModify
5454
}
55+
56+
// Make sure that we get never used to diagnostics, but not never written to diagnostics.
57+
func neverWrittenMutatedDiagnostics() {
58+
var x = "123"
59+
x = "223"
60+
do {
61+
inout x2 = x // expected-warning {{initialization of variable 'x2' was never used; consider replacing with assignment to '_' or removing it}}
62+
}
63+
do {
64+
inout x2 = x
65+
let _ = x2
66+
}
67+
}

0 commit comments

Comments
 (0)