Skip to content

Commit

Permalink
Add nilnesserr linter (#5240)
Browse files Browse the repository at this point in the history
Co-authored-by: Fernandez Ludovic <ldez@users.noreply.github.com>
  • Loading branch information
alingse and ldez authored Dec 26, 2024
1 parent 8a5d479 commit 1467bc0
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .golangci.next.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ linters:
- nakedret
- nestif
- nilerr
- nilnesserr
- nilnil
- nlreturn
- noctx
Expand Down Expand Up @@ -198,6 +199,7 @@ linters:
- nakedret
- nestif
- nilerr
- nilnesserr
- nilnil
- nlreturn
- noctx
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum

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

1 change: 1 addition & 0 deletions jsonschema/golangci.next.jsonschema.json
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,7 @@
"nakedret",
"nestif",
"nilerr",
"nilnesserr",
"nilnil",
"nlreturn",
"noctx",
Expand Down
23 changes: 23 additions & 0 deletions pkg/golinters/nilnesserr/nilnesserr.go
Original file line number Diff line number Diff line change
@@ -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)
}
11 changes: 11 additions & 0 deletions pkg/golinters/nilnesserr/nilnesserr_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package nilnesserr

import (
"testing"

"github.com/golangci/golangci-lint/test/testshared/integration"
)

func TestFromTestdata(t *testing.T) {
integration.RunTestdata(t)
}
37 changes: 37 additions & 0 deletions pkg/golinters/nilnesserr/testdata/nilnesserr.go
Original file line number Diff line number Diff line change
@@ -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

}
56 changes: 56 additions & 0 deletions pkg/golinters/nilnesserr/testdata/nilnesserr_cgo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//golangcitest:args -Enilnesserr
package testdata

/*
#include <stdio.h>
#include <stdlib.h>
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

}
7 changes: 7 additions & 0 deletions pkg/lint/lintersdb/builder_linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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).
Expand Down

0 comments on commit 1467bc0

Please sign in to comment.