Skip to content

Commit f4a9898

Browse files
authored
[QoI] Improve "never used" diagnostics (#5638)
On 'let (x) = some', we should remove r-paren as well. On 'let x: Int = some', we can't remove 'let' introducer.
1 parent b19a2f0 commit f4a9898

File tree

5 files changed

+20
-10
lines changed

5 files changed

+20
-10
lines changed

lib/Sema/MiscDiagnostics.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2559,11 +2559,15 @@ VarDeclUsageChecker::~VarDeclUsageChecker() {
25592559
// ->
25602560
// _ = foo()
25612561
if (auto *pbd = var->getParentPatternBinding())
2562-
if (pbd->getSingleVar() == var && pbd->getInit(0) != nullptr) {
2562+
if (pbd->getSingleVar() == var && pbd->getInit(0) != nullptr &&
2563+
!isa<TypedPattern>(pbd->getPatternList()[0].getPattern())) {
25632564
unsigned varKind = var->isLet();
2565+
SourceRange replaceRange(
2566+
pbd->getStartLoc(),
2567+
pbd->getPatternList()[0].getPattern()->getEndLoc());
25642568
TC.diagnose(var->getLoc(), diag::pbd_never_used,
25652569
var->getName(), varKind)
2566-
.fixItReplace(SourceRange(pbd->getLoc(), var->getLoc()), "_");
2570+
.fixItReplace(replaceRange, "_");
25672571
continue;
25682572
}
25692573

test/SILGen/errors.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -842,7 +842,7 @@ func testOptionalTryNeverFails() {
842842
// CHECK-NEXT: return [[VOID]] : $()
843843
// CHECK-NEXT: } // end sil function '_TF6errors28testOptionalTryNeverFailsVarFT_T_'
844844
func testOptionalTryNeverFailsVar() {
845-
var unit: ()? = try? () // expected-warning {{no calls to throwing functions occur within 'try' expression}} expected-warning {{initialization of variable 'unit' was never used; consider replacing with assignment to '_' or removing it}}
845+
var unit: ()? = try? () // expected-warning {{no calls to throwing functions occur within 'try' expression}} expected-warning {{variable 'unit' was never used; consider replacing with '_' or removing it}}
846846
}
847847

848848
// CHECK-LABEL: sil hidden @_TF6errors36testOptionalTryNeverFailsAddressOnly

test/decl/var/usage.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,3 +246,9 @@ func test(_ a : Int?, b : Any) {
246246

247247
}
248248

249+
func test2() {
250+
let a = 4 // expected-warning {{initialization of immutable value 'a' was never used; consider replacing with assignment to '_' or removing it}} {{3-8=_}}
251+
var ( b ) = 6 // expected-warning {{initialization of variable 'b' was never used; consider replacing with assignment to '_' or removing it}} {{3-12=_}}
252+
var c: Int = 4 // expected-warning {{variable 'c' was never used; consider replacing with '_' or removing it}} {{7-8=_}}
253+
let (d): Int = 9 // expected-warning {{immutable value 'd' was never used; consider replacing with '_' or removing it}} {{8-9=_}}
254+
}

test/expr/expressions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ func stringliterals(_ d: [String: Int]) {
456456
"something else")"
457457
// expected-error @-2 {{unterminated string literal}} expected-error @-1 {{unterminated string literal}}
458458

459-
// expected-warning @+2 {{initialization of variable 'x2' was never used; consider replacing with assignment to '_' or removing it}}
459+
// expected-warning @+2 {{variable 'x2' was never used; consider replacing with '_' or removing it}}
460460
// expected-error @+1 {{unterminated string literal}}
461461
var x2 : () = ("hello" + "
462462
;

test/expr/postfix/dot/init_ref_delegation.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ class TestNestedExpr {
334334

335335
convenience init(a: Int) {
336336
let x: () = self.init() // expected-error {{initializer delegation ('self.init') cannot be nested in another statement}}
337-
// expected-warning@-1 {{initialization of immutable value 'x' was never used; consider replacing with assignment to '_' or removing it}}
337+
// expected-warning@-1 {{immutable value 'x' was never used; consider replacing with '_' or removing it}}
338338
}
339339

340340
convenience init(b: Int) {
@@ -348,7 +348,7 @@ class TestNestedExpr {
348348

349349
convenience init(d: Int) {
350350
let x: () = self.init(fail: true)! // expected-error {{initializer delegation ('self.init') cannot be nested in another statement}}
351-
// expected-warning@-1 {{initialization of immutable value 'x' was never used; consider replacing with assignment to '_' or removing it}}
351+
// expected-warning@-1 {{immutable value 'x' was never used; consider replacing with '_' or removing it}}
352352
}
353353

354354
convenience init(e: Int) {
@@ -362,7 +362,7 @@ class TestNestedExpr {
362362

363363
convenience init(g: Int) {
364364
let x: () = try! self.init(error: true) // expected-error {{initializer delegation ('self.init') cannot be nested in another statement}}
365-
// expected-warning@-1 {{initialization of immutable value 'x' was never used; consider replacing with assignment to '_' or removing it}}
365+
// expected-warning@-1 {{immutable value 'x' was never used; consider replacing with '_' or removing it}}
366366
}
367367

368368
convenience init(h: Int) {
@@ -378,7 +378,7 @@ class TestNestedExpr {
378378
class TestNestedExprSub : TestNestedExpr {
379379
init(a: Int) {
380380
let x: () = super.init() // expected-error {{initializer chaining ('super.init') cannot be nested in another statement}}
381-
// expected-warning@-1 {{initialization of immutable value 'x' was never used; consider replacing with assignment to '_' or removing it}}
381+
// expected-warning@-1 {{immutable value 'x' was never used; consider replacing with '_' or removing it}}
382382
}
383383

384384
init(b: Int) {
@@ -392,7 +392,7 @@ class TestNestedExprSub : TestNestedExpr {
392392

393393
init(d: Int) {
394394
let x: () = super.init(fail: true)! // expected-error {{initializer chaining ('super.init') cannot be nested in another statement}}
395-
// expected-warning@-1 {{initialization of immutable value 'x' was never used; consider replacing with assignment to '_' or removing it}}
395+
// expected-warning@-1 {{immutable value 'x' was never used; consider replacing with '_' or removing it}}
396396
}
397397

398398
init(e: Int) {
@@ -406,7 +406,7 @@ class TestNestedExprSub : TestNestedExpr {
406406

407407
init(g: Int) {
408408
let x: () = try! super.init(error: true) // expected-error {{initializer chaining ('super.init') cannot be nested in another statement}}
409-
// expected-warning@-1 {{initialization of immutable value 'x' was never used; consider replacing with assignment to '_' or removing it}}
409+
// expected-warning@-1 {{immutable value 'x' was never used; consider replacing with '_' or removing it}}
410410
}
411411

412412
init(h: Int) {

0 commit comments

Comments
 (0)