File tree 5 files changed +76
-0
lines changed
5 files changed +76
-0
lines changed Original file line number Diff line number Diff line change @@ -66,6 +66,7 @@ require (
66
66
github.com/shazow/go-diff v0.0.0-20160112020656-b6b7b6733b8c
67
67
github.com/shirou/gopsutil/v3 v3.21.6
68
68
github.com/sirupsen/logrus v1.8.1
69
+ github.com/sivchari/nilassign v0.1.0
69
70
github.com/sonatard/noctx v0.0.1
70
71
github.com/sourcegraph/go-diff v0.6.1
71
72
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 {
485
485
WithLoadForGoAnalysis ().
486
486
WithPresets (linter .PresetBugs ).
487
487
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" ),
488
493
linter .NewConfig (golinters .NewForceTypeAssert ()).
489
494
WithSince ("v1.38.0" ).
490
495
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