Skip to content

Commit 66fc779

Browse files
authored
Add nilerr linter. (#1788)
1 parent 05836e4 commit 66fc779

File tree

5 files changed

+61
-0
lines changed

5 files changed

+61
-0
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ require (
3131
github.com/golangci/revgrep v0.0.0-20210208091834-cd28932614b5
3232
github.com/golangci/unconvert v0.0.0-20180507085042-28b1c447d1f4
3333
github.com/gordonklaus/ineffassign v0.0.0-20210225214923-2e10b2664254
34+
github.com/gostaticanalysis/nilerr v0.1.1
3435
github.com/jgautheron/goconst v1.4.0
3536
github.com/jingyugao/rowserrcheck v0.0.0-20210130005344-c6a0c12dd98d
3637
github.com/jirfag/go-printf-func-name v0.0.0-20191110105641-45db9963cdd3

go.sum

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

pkg/golinters/nilerr.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package golinters
2+
3+
import (
4+
"github.com/gostaticanalysis/nilerr"
5+
"golang.org/x/tools/go/analysis"
6+
7+
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
8+
)
9+
10+
func NewNilErr() *goanalysis.Linter {
11+
a := nilerr.Analyzer
12+
return goanalysis.NewLinter(
13+
a.Name,
14+
"Finds the code that returns nil even if it checks that the error is not nil.",
15+
[]*analysis.Analyzer{a},
16+
nil,
17+
).WithLoadMode(goanalysis.LoadModeTypesInfo)
18+
}

pkg/lint/lintersdb/manager.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,10 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
386386
WithPresets(linter.PresetStyle).
387387
WithLoadForGoAnalysis().
388388
WithURL("https://github.com/julz/importas"),
389+
linter.NewConfig(golinters.NewNilErr()).
390+
WithLoadForGoAnalysis().
391+
WithPresets(linter.PresetBugs).
392+
WithURL("https://github.com/gostaticanalysis/nilerr"),
389393

390394
// nolintlint must be last because it looks at the results of all the previous linters for unused nolint directives
391395
linter.NewConfig(golinters.NewNoLintLint()).

test/testdata/nilerr.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//args: -Enilerr
2+
package testdata
3+
4+
import "os"
5+
6+
func nilErr1() error {
7+
err := nilErrDo()
8+
if err == nil {
9+
return err // ERROR `error is nil \(line 7\) but it returns error`
10+
}
11+
12+
return nil
13+
}
14+
15+
func nilErr2() error {
16+
err := nilErrDo()
17+
if err == nil {
18+
return err // ERROR `error is nil \(line 16\) but it returns error`
19+
}
20+
21+
return nil
22+
}
23+
24+
func nilErr3() error {
25+
err := nilErrDo()
26+
if err != nil {
27+
return nil // ERROR `error is not nil \(line 25\) but it returns nil`
28+
}
29+
30+
return nil
31+
}
32+
33+
func nilErrDo() error {
34+
return os.ErrNotExist
35+
}

0 commit comments

Comments
 (0)