File tree Expand file tree Collapse file tree 5 files changed +76
-0
lines changed Expand file tree Collapse file tree 5 files changed +76
-0
lines changed Original file line number Diff line number Diff line change @@ -66,6 +66,7 @@ require (
6666 github.com/shazow/go-diff v0.0.0-20160112020656-b6b7b6733b8c
6767 github.com/shirou/gopsutil/v3 v3.21.6
6868 github.com/sirupsen/logrus v1.8.1
69+ github.com/sivchari/nilassign v0.1.0
6970 github.com/sonatard/noctx v0.0.1
7071 github.com/sourcegraph/go-diff v0.6.1
7172 github.com/spf13/cobra v1.2.1
Original file line number Diff line number Diff line change 1+ package golinters
2+
3+ import (
4+ "github.com/sivchari/nilassign"
5+ "golang.org/x/tools/go/analysis"
6+
7+ "github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
8+ )
9+
10+ func NewNilAssign () * goanalysis.Linter {
11+ analyzers := []* analysis.Analyzer {
12+ nilassign .Analyzer ,
13+ }
14+
15+ return goanalysis .NewLinter (
16+ "nilassign" ,
17+ "Finds that assigning to invalid memory address or nil pointer dereference." ,
18+ analyzers ,
19+ nil ,
20+ ).WithLoadMode (goanalysis .LoadModeTypesInfo )
21+ }
Original file line number Diff line number Diff line change @@ -485,6 +485,11 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
485485 WithLoadForGoAnalysis ().
486486 WithPresets (linter .PresetBugs ).
487487 WithURL ("https://github.com/gostaticanalysis/nilerr" ),
488+ linter .NewConfig (golinters .NewNilAssign ()).
489+ WithSince ("v1.42.0" ).
490+ WithLoadForGoAnalysis ().
491+ WithPresets (linter .PresetBugs ).
492+ WithURL ("https://github.com/sivchari/nilassign" ),
488493 linter .NewConfig (golinters .NewForceTypeAssert ()).
489494 WithSince ("v1.38.0" ).
490495 WithPresets (linter .PresetStyle ).
Original file line number Diff line number Diff line change 1+ //args: -Enilassign
2+ package testdata
3+
4+ var num = 1
5+
6+ func pvar () {
7+ var ii * int
8+ * ii = 1 // ERROR "this assignment occurs invalid memory address or nil pointer dereference"
9+
10+ var i * int
11+ i = & num // OK
12+ _ = i // OK
13+ }
14+
15+ func pstruct () {
16+ n := new (Node )
17+
18+ * n .PVal = 1 // ERROR "this assignment occurs invalid memory address or nil pointer dereference"
19+ * n .ChildNode .PVal = 1 // ERROR "this assignment occurs invalid memory address or nil pointer dereference"
20+
21+ n .ChildNode = & Node {PVal : & num } // OK
22+ n .PVal = & num // OK
23+ }
24+
25+ type Node struct {
26+ PVal * int
27+ ChildNode * Node
28+ }
You can’t perform that action at this time.
0 commit comments