Skip to content

Commit 2739fd0

Browse files
committed
quickfix/qf1002: emit valid fix for switches with initialization statements
Closes: gh-1613
1 parent e91ecad commit 2739fd0

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

quickfix/qf1002/qf1002.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func run(pass *analysis.Pass) (interface{}, error) {
109109

110110
edits = append(edits, edit.ReplaceWithString(edit.Range{stmt.List[0].Pos(), stmt.Colon}, strings.Join(values, ", ")))
111111
}
112-
pos := swtch.Switch + token.Pos(len("switch"))
112+
pos := swtch.Body.Lbrace
113113
edits = append(edits, edit.ReplaceWithString(edit.Range{pos, pos}, " "+report.Render(pass, x)))
114114
report.Report(pass, swtch, fmt.Sprintf("could use tagged switch on %s", report.Render(pass, x)),
115115
report.Fixes(edit.Fix("Replace with tagged switch", edits...)))

quickfix/qf1002/testdata/go1.0/CheckTaglessSwitch/CheckTaglessSwitch.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,11 @@ func fn1() {
5757
switch {
5858
case x == 1 && x == 2:
5959
}
60+
61+
switch b := 42; { //@ diag(`could use tagged switch on b`)
62+
case b == 0:
63+
case b == 1:
64+
default:
65+
_ = b
66+
}
6067
}

quickfix/qf1002/testdata/go1.0/CheckTaglessSwitch/CheckTaglessSwitch.go.golden

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,11 @@ func fn1() {
5757
switch {
5858
case x == 1 && x == 2:
5959
}
60+
61+
switch b := 42; b { //@ diag(`could use tagged switch on b`)
62+
case 0:
63+
case 1:
64+
default:
65+
_ = b
66+
}
6067
}

0 commit comments

Comments
 (0)