Skip to content

QF1002: report diagnostic at "switch" token, not entire stmt #1647

@adonovan

Description

@adonovan

Gopls now includes many staticcheck analyzers by default, including QF1002. When it reports a diagnostic, the entire switch statement shows up highlighted in my editor. I think usability would be improved by this change to restrict it to just the "switch" keyword:

    quickfix/qf1002: report diagnostic over switch keyword, not stmt
    
    This change restricts the extent of the diagnostic to just
    the "switch" keyword, not the entire multiline statement,
    to reduce visual distraction.

diff --git a/quickfix/qf1002/qf1002.go b/quickfix/qf1002/qf1002.go
index ff4bf9cd..9bcca3ec 100644
--- a/quickfix/qf1002/qf1002.go
+++ b/quickfix/qf1002/qf1002.go
@@ -111,7 +111,8 @@ func run(pass *analysis.Pass) (interface{}, error) {
                }
                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)),
+               rng := edit.Range{swtch.Pos(), swtch.Pos() + token.Pos(len("switch"))}
+               report.Report(pass, rng, fmt.Sprintf("could use tagged switch on %s", report.Render(pass, x)),
                        report.Fixes(edit.Fix("Replace with tagged switch", edits...)))
        }

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions