Skip to content

Commit

Permalink
Update staticcheck to v0.1.2 (2020.2.2) (#1756)
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Feb 21, 2021
1 parent a1e3749 commit 2e7c389
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 46 deletions.
12 changes: 12 additions & 0 deletions .golangci.yml
Expand Up @@ -139,6 +139,18 @@ issues:
- path: pkg/golinters/scopelint.go
text: 'directive `//nolint:interfacer` is unused for linter interfacer'

# TODO temporary rule, must be removed
# related to https://github.com/golangci/golangci-lint/pull/1756
# must be replaced by '//nolint:staticcheck // require changes in github.com/OpenPeeDeeP/depguard'
- path: pkg/golinters/depguard.go
text: 'SA1019: package golang.org/x/tools/go/loader is deprecated'

# TODO temporary rule, must be removed
# related to https://github.com/golangci/golangci-lint/pull/1756
# must be replaced by '///nolint:staticcheck // it's an adapter for golang.org/x/tools/go/packages'
- path: pkg/golinters/goanalysis/adapters.go
text: 'SA1019: package golang.org/x/tools/go/loader is deprecated'

run:
skip-dirs:
- test/testdata_etc
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -77,7 +77,7 @@ require (
golang.org/x/text v0.3.4 // indirect
golang.org/x/tools v0.1.0
gopkg.in/yaml.v2 v2.4.0
honnef.co/go/tools v0.0.1-2020.1.6
honnef.co/go/tools v0.1.2
mvdan.cc/gofumpt v0.1.0
mvdan.cc/interfacer v0.0.0-20180901003855-c20040233aed
mvdan.cc/lint v0.0.0-20170908181259-adc824a0674b // indirect
Expand Down
5 changes: 2 additions & 3 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

82 changes: 41 additions & 41 deletions pkg/golinters/unused.go
@@ -1,10 +1,10 @@
package golinters

import (
"go/types"
"fmt"
"sync"

"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/packages"
"honnef.co/go/tools/unused"

"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
Expand All @@ -13,53 +13,53 @@ import (
)

func NewUnused() *goanalysis.Linter {
u := unused.NewChecker(false)
analyzers := []*analysis.Analyzer{u.Analyzer()}
const name = "unused"

var mu sync.Mutex
var resIssues []goanalysis.Issue

analyzer := &analysis.Analyzer{
Name: name,
Doc: unused.Analyzer.Doc,
Requires: unused.Analyzer.Requires,
Run: func(pass *analysis.Pass) (interface{}, error) {
res, err := unused.Analyzer.Run(pass)
if err != nil {
return nil, err
}

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

var issues []goanalysis.Issue
for _, object := range sr.Unused {
issue := goanalysis.NewIssue(&result.Issue{
FromLinter: name,
Text: fmt.Sprintf("%s %s is unused", object.Kind, object.Name),
Pos: object.Position,
}, pass)

issues = append(issues, issue)
}

mu.Lock()
resIssues = append(resIssues, issues...)
mu.Unlock()

return nil, nil
},
}

analyzers := []*analysis.Analyzer{analyzer}
setAnalyzersGoVersion(analyzers)

const name = "unused"
lnt := goanalysis.NewLinter(
name,
"Checks Go code for unused constants, variables, functions and types",
analyzers,
nil,
).WithIssuesReporter(func(lintCtx *linter.Context) []goanalysis.Issue {
typesToPkg := map[*types.Package]*packages.Package{}
for _, pkg := range lintCtx.OriginalPackages {
typesToPkg[pkg.Types] = pkg
}
return resIssues
}).WithLoadMode(goanalysis.LoadModeSyntax | goanalysis.LoadModeTypesInfo)

var issues []goanalysis.Issue
for _, ur := range u.Result() {
p := u.ProblemObject(lintCtx.Packages[0].Fset, ur)
pkg := typesToPkg[ur.Pkg()]
i := &result.Issue{
FromLinter: name,
Text: p.Message,
Pos: p.Pos,
Pkg: pkg,
LineRange: &result.Range{
From: p.Pos.Line,
To: p.End.Line,
},
}
// See https://github.com/golangci/golangci-lint/issues/1048
// If range is invalid, this will break `--fix` mode.
if i.LineRange.To >= i.LineRange.From {
i.Replacement = &result.Replacement{
// Suggest deleting unused stuff.
NeedOnlyDelete: true,
}
}
issues = append(issues, goanalysis.NewIssue(i, nil))
}
return issues
}).WithContextSetter(func(lintCtx *linter.Context) {
if lintCtx.Settings().Unused.CheckExported {
lintCtx.Log.Infof("Using whole program analysis for unused, it can be memory-heavy")
u.WholeProgram = true
}
}).WithLoadMode(goanalysis.LoadModeWholeProgram)
lnt.UseOriginalPackages()
return lnt
}
2 changes: 1 addition & 1 deletion test/testdata/staticcheck.go
Expand Up @@ -23,7 +23,7 @@ func StaticcheckNolintMegacheck() {
}

func StaticcheckDeprecated() {
_ = runtime.CPUProfile() // ERROR "SA1019: runtime.CPUProfile is deprecated"
_ = runtime.CPUProfile() // ERROR "SA1019: runtime.CPUProfile has been deprecated .*"
}

func StaticcheckPrintf() {
Expand Down
13 changes: 13 additions & 0 deletions test/testdata/unused.go
@@ -1,6 +1,19 @@
//args: -Eunused
package testdata

func fn1() {} // ERROR "func `fn1` is unused"

//nolint:unused
func fn2() { fn3() }

func fn3() {} // ERROR "func `fn3` is unused"

func fn4() { fn5() } // ERROR "func `fn4` is unused"

func fn5() {} // ERROR "func `fn5` is unused"

func fn6() { fn4() } // ERROR "func `fn6` is unused"

type unusedStruct struct{} // ERROR "type `unusedStruct` is unused"

type unusedStructNolintUnused struct{} //nolint:unused
Expand Down

0 comments on commit 2e7c389

Please sign in to comment.