Skip to content

Commit

Permalink
Add nilerr linter.
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Feb 26, 2021
1 parent 05836e4 commit a977799
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Expand Up @@ -31,6 +31,7 @@ require (
github.com/golangci/revgrep v0.0.0-20210208091834-cd28932614b5
github.com/golangci/unconvert v0.0.0-20180507085042-28b1c447d1f4
github.com/gordonklaus/ineffassign v0.0.0-20210225214923-2e10b2664254
github.com/gostaticanalysis/nilerr v0.1.1
github.com/jgautheron/goconst v1.4.0
github.com/jingyugao/rowserrcheck v0.0.0-20210130005344-c6a0c12dd98d
github.com/jirfag/go-printf-func-name v0.0.0-20191110105641-45db9963cdd3
Expand Down
3 changes: 3 additions & 0 deletions go.sum

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

18 changes: 18 additions & 0 deletions pkg/golinters/nilerr.go
@@ -0,0 +1,18 @@
package golinters

import (
"github.com/gostaticanalysis/nilerr"
"golang.org/x/tools/go/analysis"

"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
)

func NewNilErr() *goanalysis.Linter {
a := nilerr.Analyzer
return goanalysis.NewLinter(
a.Name,
"Finds the code that returns nil even if it checks that the error is not nil.",
[]*analysis.Analyzer{a},
nil,
).WithLoadMode(goanalysis.LoadModeTypesInfo)
}
4 changes: 4 additions & 0 deletions pkg/lint/lintersdb/manager.go
Expand Up @@ -386,6 +386,10 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
WithPresets(linter.PresetStyle).
WithLoadForGoAnalysis().
WithURL("https://github.com/julz/importas"),
linter.NewConfig(golinters.NewNilErr()).
WithLoadForGoAnalysis().
WithPresets(linter.PresetBugs).
WithURL("https://github.com/gostaticanalysis/nilerr"),

// nolintlint must be last because it looks at the results of all the previous linters for unused nolint directives
linter.NewConfig(golinters.NewNoLintLint()).
Expand Down
33 changes: 33 additions & 0 deletions test/testdata/nilerr.go
@@ -0,0 +1,33 @@
//args: -Enilerr
package testdata

func nilErr1() error {
err := nilErrDo()
if err == nil {
return err // ERROR `error is nil \(line 5\) but it returns error`
}

return nil
}

func nilErr2() error {
err := nilErrDo()
if err == nil {
return err // ERROR `error is nil \(line 14\) but it returns error`
}

return nil
}

func nilErr3() error {
err := nilErrDo()
if err != nil {
return nil // ERROR `error is not nil \(line 23\) but it returns nil`
}

return nil
}

func nilErrDo() error {
return nil
}

0 comments on commit a977799

Please sign in to comment.