diff --git a/.golangci.next.reference.yml b/.golangci.next.reference.yml index cd7cfcaa0580..b8445c5591eb 100644 --- a/.golangci.next.reference.yml +++ b/.golangci.next.reference.yml @@ -81,6 +81,7 @@ linters: - nakedret - nestif - nilerr + - nilnesserr - nilnil - nlreturn - noctx @@ -198,6 +199,7 @@ linters: - nakedret - nestif - nilerr + - nilnesserr - nilnil - nlreturn - noctx diff --git a/go.mod b/go.mod index 7dd4e2a6d811..c7103a97f50f 100644 --- a/go.mod +++ b/go.mod @@ -19,6 +19,7 @@ require ( github.com/alexkohler/nakedret/v2 v2.0.5 github.com/alexkohler/prealloc v1.0.0 github.com/alingse/asasalint v0.0.11 + github.com/alingse/nilnesserr v0.1.1 github.com/ashanbrown/forbidigo v1.6.0 github.com/ashanbrown/makezero v1.2.0 github.com/bkielbasa/cyclop v1.2.3 diff --git a/go.sum b/go.sum index 2dc0443ecd6a..10e32540253e 100644 --- a/go.sum +++ b/go.sum @@ -76,6 +76,8 @@ github.com/alexkohler/prealloc v1.0.0 h1:Hbq0/3fJPQhNkN0dR95AVrr6R7tou91y0uHG5pO github.com/alexkohler/prealloc v1.0.0/go.mod h1:VetnK3dIgFBBKmg0YnD9F9x6Icjd+9cvfHR56wJVlKE= github.com/alingse/asasalint v0.0.11 h1:SFwnQXJ49Kx/1GghOFz1XGqHYKp21Kq1nHad/0WQRnw= github.com/alingse/asasalint v0.0.11/go.mod h1:nCaoMhw7a9kSJObvQyVzNTPBDbNpdocqrSP7t/cW5+I= +github.com/alingse/nilnesserr v0.1.1 h1:7cYuJewpy9jFNMEA72Q1+3Nm3zKHzg+Q28D5f2bBFUA= +github.com/alingse/nilnesserr v0.1.1/go.mod h1:1xJPrXonEtX7wyTq8Dytns5P2hNzoWymVUIaKm4HNFg= github.com/ashanbrown/forbidigo v1.6.0 h1:D3aewfM37Yb3pxHujIPSpTf6oQk9sc9WZi8gerOIVIY= github.com/ashanbrown/forbidigo v1.6.0/go.mod h1:Y8j9jy9ZYAEHXdu723cUlraTqbzjKF1MUyfOKL+AjcU= github.com/ashanbrown/makezero v1.2.0 h1:/2Lp1bypdmK9wDIq7uWBlDF1iMUpIIS4A+pF6C9IEUU= diff --git a/jsonschema/golangci.next.jsonschema.json b/jsonschema/golangci.next.jsonschema.json index a5358dadd8ef..6553bc369cea 100644 --- a/jsonschema/golangci.next.jsonschema.json +++ b/jsonschema/golangci.next.jsonschema.json @@ -397,6 +397,7 @@ "nakedret", "nestif", "nilerr", + "nilnesserr", "nilnil", "nlreturn", "noctx", diff --git a/pkg/golinters/nilnesserr/nilnesserr.go b/pkg/golinters/nilnesserr/nilnesserr.go new file mode 100644 index 000000000000..8349377b7b69 --- /dev/null +++ b/pkg/golinters/nilnesserr/nilnesserr.go @@ -0,0 +1,23 @@ +package nilnesserr + +import ( + "github.com/alingse/nilnesserr" + "golang.org/x/tools/go/analysis" + + "github.com/golangci/golangci-lint/pkg/goanalysis" + "github.com/golangci/golangci-lint/pkg/golinters/internal" +) + +func New() *goanalysis.Linter { + a, err := nilnesserr.NewAnalyzer(nilnesserr.LinterSetting{}) + if err != nil { + internal.LinterLogger.Fatalf("nilnesserr: create analyzer: %v", err) + } + + return goanalysis.NewLinter( + a.Name, + a.Doc, + []*analysis.Analyzer{a}, + nil, + ).WithLoadMode(goanalysis.LoadModeTypesInfo) +} diff --git a/pkg/golinters/nilnesserr/nilnesserr_integration_test.go b/pkg/golinters/nilnesserr/nilnesserr_integration_test.go new file mode 100644 index 000000000000..10824feff52c --- /dev/null +++ b/pkg/golinters/nilnesserr/nilnesserr_integration_test.go @@ -0,0 +1,11 @@ +package nilnesserr + +import ( + "testing" + + "github.com/golangci/golangci-lint/test/testshared/integration" +) + +func TestFromTestdata(t *testing.T) { + integration.RunTestdata(t) +} diff --git a/pkg/golinters/nilnesserr/testdata/nilnesserr.go b/pkg/golinters/nilnesserr/testdata/nilnesserr.go new file mode 100644 index 000000000000..6c6b98cb8074 --- /dev/null +++ b/pkg/golinters/nilnesserr/testdata/nilnesserr.go @@ -0,0 +1,37 @@ +//golangcitest:args -Enilnesserr +package testdata + +import "fmt" + +func do() error { + return fmt.Errorf("do error") +} + +func do2() error { + return fmt.Errorf("do2 error") +} + +func someCall() error { + err := do() + if err != nil { + return err + } + err2 := do2() + if err2 != nil { + return err // want `return a nil value error after check error` + } + return nil +} + +func sameCall2() error { + err := do() + if err == nil { + err2 := do2() + if err2 != nil { + return err // want `return a nil value error after check error` + } + return nil + } + return err + +} diff --git a/pkg/golinters/nilnesserr/testdata/nilnesserr_cgo.go b/pkg/golinters/nilnesserr/testdata/nilnesserr_cgo.go new file mode 100644 index 000000000000..94cd960b6a4b --- /dev/null +++ b/pkg/golinters/nilnesserr/testdata/nilnesserr_cgo.go @@ -0,0 +1,56 @@ +//golangcitest:args -Enilnesserr +package testdata + +/* + #include + #include + + void myprint(char* s) { + printf("%d\n", s); + } +*/ +import "C" + +import ( + "fmt" + "unsafe" +) + +func _() { + cs := C.CString("Hello from stdio\n") + C.myprint(cs) + C.free(unsafe.Pointer(cs)) +} + +func do() error { + return fmt.Errorf("do error") +} + +func do2() error { + return fmt.Errorf("do2 error") +} + +func someCall() error { + err := do() + if err != nil { + return err + } + err2 := do2() + if err2 != nil { + return err // want `return a nil value error after check error` + } + return nil +} + +func sameCall2() error { + err := do() + if err == nil { + err2 := do2() + if err2 != nil { + return err // want `return a nil value error after check error` + } + return nil + } + return err + +} diff --git a/pkg/lint/lintersdb/builder_linter.go b/pkg/lint/lintersdb/builder_linter.go index f859fcdd57d5..cda053c01972 100644 --- a/pkg/lint/lintersdb/builder_linter.go +++ b/pkg/lint/lintersdb/builder_linter.go @@ -72,6 +72,7 @@ import ( "github.com/golangci/golangci-lint/pkg/golinters/nakedret" "github.com/golangci/golangci-lint/pkg/golinters/nestif" "github.com/golangci/golangci-lint/pkg/golinters/nilerr" + "github.com/golangci/golangci-lint/pkg/golinters/nilnesserr" "github.com/golangci/golangci-lint/pkg/golinters/nilnil" "github.com/golangci/golangci-lint/pkg/golinters/nlreturn" "github.com/golangci/golangci-lint/pkg/golinters/noctx" @@ -596,6 +597,12 @@ func (LinterBuilder) Build(cfg *config.Config) ([]*linter.Config, error) { WithPresets(linter.PresetBugs). WithURL("https://github.com/gostaticanalysis/nilerr"), + linter.NewConfig(nilnesserr.New()). + WithSince("v1.63.0"). + WithLoadForGoAnalysis(). + WithPresets(linter.PresetBugs). + WithURL("https://github.com/alingse/nilnesserr"), + linter.NewConfig(nilnil.New(&cfg.LintersSettings.NilNil)). WithSince("v1.43.0"). WithPresets(linter.PresetStyle).