Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add nilerr linter. #1788

Merged
merged 1 commit into from
Feb 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

18 changes: 18 additions & 0 deletions pkg/golinters/nilerr.go
Original file line number Diff line number Diff line change
@@ -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
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
35 changes: 35 additions & 0 deletions test/testdata/nilerr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//args: -Enilerr
package testdata

import "os"

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

return nil
}

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

return nil
}

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

return nil
}

func nilErrDo() error {
return os.ErrNotExist
}