mirror of
https://github.com/scratchfoundation/golangci-lint.git
synced 2025-08-28 22:28:43 -04:00
add nilassign linter (#2131)
Co-authored-by: Fernandez Ludovic <ldez@users.noreply.github.com>
This commit is contained in:
parent
245257b8fb
commit
bbf0450265
5 changed files with 76 additions and 0 deletions
28
test/testdata/nilassign.go
vendored
Normal file
28
test/testdata/nilassign.go
vendored
Normal file
|
@ -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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue