Skip to content

Commit 29248ee

Browse files
committed
feat: new unused implementation.
1 parent 8c8b4e8 commit 29248ee

File tree

2 files changed

+54
-41
lines changed

2 files changed

+54
-41
lines changed

pkg/golinters/unused.go

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package golinters
22

33
import (
4-
"go/types"
4+
"fmt"
5+
"sync"
56

67
"golang.org/x/tools/go/analysis"
7-
"golang.org/x/tools/go/packages"
88
"honnef.co/go/tools/unused"
99

1010
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
@@ -13,53 +13,53 @@ import (
1313
)
1414

1515
func NewUnused() *goanalysis.Linter {
16-
u := unused.NewChecker(false)
17-
analyzers := []*analysis.Analyzer{u.Analyzer()}
16+
const name = "unused"
17+
18+
var mu sync.Mutex
19+
var resIssues []goanalysis.Issue
20+
21+
analyzer := &analysis.Analyzer{
22+
Name: name,
23+
Doc: unused.Analyzer.Doc,
24+
Requires: unused.Analyzer.Requires,
25+
Run: func(pass *analysis.Pass) (interface{}, error) {
26+
res, err := unused.Analyzer.Run(pass)
27+
if err != nil {
28+
return nil, err
29+
}
30+
31+
sr := unused.Serialize(pass, res.(unused.Result), pass.Fset)
32+
33+
var issues []goanalysis.Issue
34+
for _, object := range sr.Unused {
35+
issue := goanalysis.NewIssue(&result.Issue{
36+
FromLinter: name,
37+
Text: fmt.Sprintf("%s %s is unused", object.Kind, object.Name),
38+
Pos: object.Position,
39+
}, pass)
40+
41+
issues = append(issues, issue)
42+
}
43+
44+
mu.Lock()
45+
resIssues = append(resIssues, issues...)
46+
mu.Unlock()
47+
48+
return nil, nil
49+
},
50+
}
51+
52+
analyzers := []*analysis.Analyzer{analyzer}
1853
setAnalyzersGoVersion(analyzers)
1954

20-
const name = "unused"
2155
lnt := goanalysis.NewLinter(
2256
name,
2357
"Checks Go code for unused constants, variables, functions and types",
2458
analyzers,
2559
nil,
2660
).WithIssuesReporter(func(lintCtx *linter.Context) []goanalysis.Issue {
27-
typesToPkg := map[*types.Package]*packages.Package{}
28-
for _, pkg := range lintCtx.OriginalPackages {
29-
typesToPkg[pkg.Types] = pkg
30-
}
61+
return resIssues
62+
}).WithLoadMode(goanalysis.LoadModeSyntax | goanalysis.LoadModeTypesInfo)
3163

32-
var issues []goanalysis.Issue
33-
for _, ur := range u.Result() {
34-
p := u.ProblemObject(lintCtx.Packages[0].Fset, ur)
35-
pkg := typesToPkg[ur.Pkg()]
36-
i := &result.Issue{
37-
FromLinter: name,
38-
Text: p.Message,
39-
Pos: p.Pos,
40-
Pkg: pkg,
41-
LineRange: &result.Range{
42-
From: p.Pos.Line,
43-
To: p.End.Line,
44-
},
45-
}
46-
// See https://github.com/golangci/golangci-lint/issues/1048
47-
// If range is invalid, this will break `--fix` mode.
48-
if i.LineRange.To >= i.LineRange.From {
49-
i.Replacement = &result.Replacement{
50-
// Suggest deleting unused stuff.
51-
NeedOnlyDelete: true,
52-
}
53-
}
54-
issues = append(issues, goanalysis.NewIssue(i, nil))
55-
}
56-
return issues
57-
}).WithContextSetter(func(lintCtx *linter.Context) {
58-
if lintCtx.Settings().Unused.CheckExported {
59-
lintCtx.Log.Infof("Using whole program analysis for unused, it can be memory-heavy")
60-
u.WholeProgram = true
61-
}
62-
}).WithLoadMode(goanalysis.LoadModeWholeProgram)
63-
lnt.UseOriginalPackages()
6464
return lnt
6565
}

test/testdata/unused.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
//args: -Eunused
22
package testdata
33

4+
func fn1() {} // ERROR "func `fn1` is unused"
5+
6+
//nolint:unused
7+
func fn2() { fn3() }
8+
9+
func fn3() {} // ERROR "func `fn3` is unused"
10+
11+
func fn4() { fn5() } // ERROR "func `fn4` is unused"
12+
13+
func fn5() {} // ERROR "func `fn5` is unused"
14+
15+
func fn6() { fn4() } // ERROR "func `fn6` is unused"
16+
417
type unusedStruct struct{} // ERROR "type `unusedStruct` is unused"
518

619
type unusedStructNolintUnused struct{} //nolint:unused

0 commit comments

Comments
 (0)