forked from golangci/golangci-lint
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add nilassign linter (golangci#2131)
Co-authored-by: Fernandez Ludovic <ldez@users.noreply.github.com>
- Loading branch information
Showing
5 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package golinters | ||
|
||
import ( | ||
"github.com/sivchari/nilassign" | ||
"golang.org/x/tools/go/analysis" | ||
|
||
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis" | ||
) | ||
|
||
func NewNilAssign() *goanalysis.Linter { | ||
analyzers := []*analysis.Analyzer{ | ||
nilassign.Analyzer, | ||
} | ||
|
||
return goanalysis.NewLinter( | ||
"nilassign", | ||
"Finds that assigning to invalid memory address or nil pointer dereference.", | ||
analyzers, | ||
nil, | ||
).WithLoadMode(goanalysis.LoadModeTypesInfo) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
//args: -Enilassign | ||
package testdata | ||
|
||
var num = 1 | ||
|
||
func pvar() { | ||
var ii *int | ||
*ii = 1 // ERROR "this assignment occurs invalid memory address or nil pointer dereference" | ||
|
||
var i *int | ||
i = &num // OK | ||
_ = i // OK | ||
} | ||
|
||
func pstruct() { | ||
n := new(Node) | ||
|
||
*n.PVal = 1 // ERROR "this assignment occurs invalid memory address or nil pointer dereference" | ||
*n.ChildNode.PVal = 1 // ERROR "this assignment occurs invalid memory address or nil pointer dereference" | ||
|
||
n.ChildNode = &Node{PVal: &num} // OK | ||
n.PVal = &num // OK | ||
} | ||
|
||
type Node struct { | ||
PVal *int | ||
ChildNode *Node | ||
} |