Skip to content

Commit

Permalink
quickfix/qf1002: emit valid fix for switches with initialization stat…
Browse files Browse the repository at this point in the history
…ements

Closes: gh-1613
  • Loading branch information
dominikh committed Oct 24, 2024
1 parent e91ecad commit 2739fd0
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion quickfix/qf1002/qf1002.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func run(pass *analysis.Pass) (interface{}, error) {

edits = append(edits, edit.ReplaceWithString(edit.Range{stmt.List[0].Pos(), stmt.Colon}, strings.Join(values, ", ")))
}
pos := swtch.Switch + token.Pos(len("switch"))
pos := swtch.Body.Lbrace
edits = append(edits, edit.ReplaceWithString(edit.Range{pos, pos}, " "+report.Render(pass, x)))
report.Report(pass, swtch, fmt.Sprintf("could use tagged switch on %s", report.Render(pass, x)),
report.Fixes(edit.Fix("Replace with tagged switch", edits...)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,11 @@ func fn1() {
switch {
case x == 1 && x == 2:
}

switch b := 42; { //@ diag(`could use tagged switch on b`)
case b == 0:
case b == 1:
default:
_ = b
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,11 @@ func fn1() {
switch {
case x == 1 && x == 2:
}

switch b := 42; b { //@ diag(`could use tagged switch on b`)
case 0:
case 1:
default:
_ = b
}
}

0 comments on commit 2739fd0

Please sign in to comment.