Skip to content

Commit

Permalink
Add errnil linter.
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Feb 26, 2021
1 parent 05836e4 commit 9fdc871
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
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.

17 changes: 17 additions & 0 deletions pkg/golinters/nilerr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package golinters

import (
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
"github.com/gostaticanalysis/nilerr"
"golang.org/x/tools/go/analysis"
)

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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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 9fdc871

Please sign in to comment.