Skip to content

Commit 2e7c389

Browse files
authored
Update staticcheck to v0.1.2 (2020.2.2) (#1756)
1 parent a1e3749 commit 2e7c389

File tree

6 files changed

+70
-46
lines changed

6 files changed

+70
-46
lines changed

.golangci.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,18 @@ issues:
139139
- path: pkg/golinters/scopelint.go
140140
text: 'directive `//nolint:interfacer` is unused for linter interfacer'
141141

142+
# TODO temporary rule, must be removed
143+
# related to https://github.com/golangci/golangci-lint/pull/1756
144+
# must be replaced by '//nolint:staticcheck // require changes in github.com/OpenPeeDeeP/depguard'
145+
- path: pkg/golinters/depguard.go
146+
text: 'SA1019: package golang.org/x/tools/go/loader is deprecated'
147+
148+
# TODO temporary rule, must be removed
149+
# related to https://github.com/golangci/golangci-lint/pull/1756
150+
# must be replaced by '///nolint:staticcheck // it's an adapter for golang.org/x/tools/go/packages'
151+
- path: pkg/golinters/goanalysis/adapters.go
152+
text: 'SA1019: package golang.org/x/tools/go/loader is deprecated'
153+
142154
run:
143155
skip-dirs:
144156
- test/testdata_etc

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ require (
7777
golang.org/x/text v0.3.4 // indirect
7878
golang.org/x/tools v0.1.0
7979
gopkg.in/yaml.v2 v2.4.0
80-
honnef.co/go/tools v0.0.1-2020.1.6
80+
honnef.co/go/tools v0.1.2
8181
mvdan.cc/gofumpt v0.1.0
8282
mvdan.cc/interfacer v0.0.0-20180901003855-c20040233aed
8383
mvdan.cc/lint v0.0.0-20170908181259-adc824a0674b // indirect

go.sum

Lines changed: 2 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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/staticcheck.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func StaticcheckNolintMegacheck() {
2323
}
2424

2525
func StaticcheckDeprecated() {
26-
_ = runtime.CPUProfile() // ERROR "SA1019: runtime.CPUProfile is deprecated"
26+
_ = runtime.CPUProfile() // ERROR "SA1019: runtime.CPUProfile has been deprecated .*"
2727
}
2828

2929
func StaticcheckPrintf() {

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)