From 8501b24c2dd217dbc35d426100e33c3bbec29094 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Sun, 17 Apr 2022 23:39:29 +0200 Subject: [PATCH] unused: fix false-positive --- pkg/golinters/unused.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/pkg/golinters/unused.go b/pkg/golinters/unused.go index cfdf1f2ca799..13e05a5085ba 100644 --- a/pkg/golinters/unused.go +++ b/pkg/golinters/unused.go @@ -35,8 +35,27 @@ func NewUnused(settings *config.StaticCheckSettings) *goanalysis.Linter { sr := unused.Serialize(pass, res.(unused.Result), pass.Fset) + used := make(map[string]bool) + for _, obj := range sr.Used { + used[fmt.Sprintf("%s %d %s", obj.Position.Filename, obj.Position.Line, obj.Name)] = true + } + var issues []goanalysis.Issue + // Inspired by https://github.com/dominikh/go-tools/blob/d694aadcb1f50c2d8ac0a1dd06217ebb9f654764/lintcmd/lint.go#L177-L197 for _, object := range sr.Unused { + if object.Kind == "type param" { + continue + } + + if object.InGenerated { + continue + } + + key := fmt.Sprintf("%s %d %s", object.Position.Filename, object.Position.Line, object.Name) + if used[key] { + continue + } + issue := goanalysis.NewIssue(&result.Issue{ FromLinter: name, Text: fmt.Sprintf("%s %s is unused", object.Kind, object.Name),