Skip to content

Commit

Permalink
Quick fix for off-by-count errors in pattern binding diagnostics
Browse files Browse the repository at this point in the history
Rather than return the last var pattern, only match on the ones that
match the vardecl the diagnostic is warning about.
  • Loading branch information
CodaFi committed Jul 11, 2016
1 parent 5164668 commit 2a5c7dd
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/Sema/MiscDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2315,7 +2315,8 @@ VarDeclUsageChecker::~VarDeclUsageChecker() {
VarPattern *foundVP = nullptr;
pattern->forEachNode([&](Pattern *P) {
if (auto *VP = dyn_cast<VarPattern>(P))
foundVP = VP;
if (VP->getSingleVar() == var)
foundVP = VP;
});

if (foundVP && !foundVP->isLet())
Expand Down
16 changes: 15 additions & 1 deletion test/decl/var/usage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,21 @@ class TestClass {
}
}

enum TestEnum {
case Test(Int, Int, Int)
}

func testEnum() -> Int {
let ev = TestEnum.Test(5, 6, 7)
switch ev {
case .Test(var i, var j, var k): // expected-warning {{variable 'i' was never mutated; consider changing to 'let' constant}} {{14-17=let}}
// expected-warning@-1 {{variable 'j' was never mutated; consider changing to 'let' constant}} {{21-24=let}}
// expected-warning@-2 {{variable 'k' was never mutated; consider changing to 'let' constant}} {{28-31=let}}
return i + j + k
default:
return 0
}
}

func nestedFunction() -> Int {
var x = 42 // No warning about being never-set.
Expand Down Expand Up @@ -231,4 +246,3 @@ func test(_ a : Int?, b : Any) {

}


0 comments on commit 2a5c7dd

Please sign in to comment.