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

feat: add nilaway static analysis tool #4212

Closed
wants to merge 4 commits into from
Closed
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
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ require (
gitlab.com/bosi/decorder v0.4.1
go-simpler.org/musttag v0.8.0
go-simpler.org/sloglint v0.3.0
golang.org/x/exp v0.0.0-20230510235704-dd950f8aeaea
golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1
golang.org/x/tools v0.15.0
gopkg.in/yaml.v3 v3.0.1
honnef.co/go/tools v0.4.6
Expand Down Expand Up @@ -158,6 +158,7 @@ require (
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/kisielk/gotool v1.0.0 // indirect
github.com/klauspost/compress v1.17.0 // indirect
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
Expand Down Expand Up @@ -189,6 +190,7 @@ require (
github.com/yusufpapurcu/wmi v1.2.3 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
go.uber.org/nilaway v0.0.0-20231117175943-a267567c6fff // indirect
go.uber.org/zap v1.24.0 // indirect
golang.org/x/exp/typeparams v0.0.0-20230307190834-24139beb5833 // indirect
golang.org/x/mod v0.14.0 // indirect
Expand Down
6 changes: 6 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/nilaway.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package golinters

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

func NewNilAway() *goanalysis.Linter {
a := nilaway.Analyzer

return goanalysis.NewLinter(
a.Name,
a.Doc,
[]*analysis.Analyzer{a},
nil,
).WithLoadMode(goanalysis.LoadModeTypesInfo)
}
6 changes: 6 additions & 0 deletions pkg/lint/lintersdb/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,12 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
WithPresets(linter.PresetComplexity).
WithURL("https://github.com/nakabonne/nestif"),

linter.NewConfig(golinters.NewNilAway()).
WithSince("v1.55.3").
WithLoadForGoAnalysis().
WithPresets(linter.PresetBugs).
WithURL("https://github.com/uber-go/nilaway/"),

linter.NewConfig(golinters.NewNilErr()).
WithSince("v1.38.0").
WithLoadForGoAnalysis().
Expand Down
26 changes: 26 additions & 0 deletions test/testdata/nilaway.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package testdata

type P struct {
f int
}

// nilAwayExample1 tests NilAway's detection of uninitialized variable access.
func nilAwayExample1(someCondition bool) {
var p *P
if someCondition {
p = &P{}
}
// NilAway should report a potential nil pointer dereference here if someCondition is false.
if p != nil {
print(p.f)
}
}

// nilAwayExample2 tests NilAway's detection of nil returns across function boundaries.
func nilAwayExample2() {
print(*nilAwayFoo()) // NilAway should report an error here.
}

func nilAwayFoo() *int {
return nil
}