Skip to content

Commit f79bc88

Browse files
authored
unused: fix false-positive (#2772)
1 parent 333187c commit f79bc88

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

Diff for: pkg/golinters/unused.go

+19
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,27 @@ func NewUnused(settings *config.StaticCheckSettings) *goanalysis.Linter {
3535

3636
sr := unused.Serialize(pass, res.(unused.Result), pass.Fset)
3737

38+
used := make(map[string]bool)
39+
for _, obj := range sr.Used {
40+
used[fmt.Sprintf("%s %d %s", obj.Position.Filename, obj.Position.Line, obj.Name)] = true
41+
}
42+
3843
var issues []goanalysis.Issue
44+
// Inspired by https://github.com/dominikh/go-tools/blob/d694aadcb1f50c2d8ac0a1dd06217ebb9f654764/lintcmd/lint.go#L177-L197
3945
for _, object := range sr.Unused {
46+
if object.Kind == "type param" {
47+
continue
48+
}
49+
50+
if object.InGenerated {
51+
continue
52+
}
53+
54+
key := fmt.Sprintf("%s %d %s", object.Position.Filename, object.Position.Line, object.Name)
55+
if used[key] {
56+
continue
57+
}
58+
4059
issue := goanalysis.NewIssue(&result.Issue{
4160
FromLinter: name,
4261
Text: fmt.Sprintf("%s %s is unused", object.Kind, object.Name),

0 commit comments

Comments
 (0)