Skip to content

Commit

Permalink
s1009: fix with SelectorExpr
Browse files Browse the repository at this point in the history
Also clarify the docs that this also checks maps and channels.

Fixes dominikh#1527
  • Loading branch information
arp242 committed May 26, 2024
1 parent 5275b91 commit 2fcd44d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
26 changes: 18 additions & 8 deletions simple/s1009/s1009.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ var SCAnalyzer = lint.InitializeAnalyzer(&lint.Analyzer{
Requires: []*analysis.Analyzer{inspect.Analyzer, generated.Analyzer},
},
Doc: &lint.Documentation{
Title: `Omit redundant nil check on slices`,
Text: `The \'len\' function is defined for all slices, even nil ones, which have
a length of zero. It is not necessary to check if a slice is not nil
before checking that its length is not zero.`,
Title: `Omit redundant nil check on slices, maps, and channels`,
Text: `The \'len\' function is defined for all slices, maps, and
channels, even nil ones, which have a length of zero. It is not necessary to
check for nil before checking that its length is not zero.`,
Before: `if x != nil && len(x) != 0 {}`,
After: `if len(x) != 0 {}`,
Since: "2017.1",
Expand Down Expand Up @@ -81,8 +81,13 @@ func run(pass *analysis.Pass) (interface{}, error) {
if !eqNil && x.Op != token.NEQ {
return
}
xx, ok := x.X.(*ast.Ident)
if !ok {
var xx *ast.Ident
switch s := x.X.(type) {
case *ast.Ident:
xx = s
case *ast.SelectorExpr:
xx = s.Sel
default:
return
}
if !code.IsNil(pass, x.Y) {
Expand All @@ -104,8 +109,13 @@ func run(pass *analysis.Pass) (interface{}, error) {
if !code.IsCallTo(pass, yx, "len") {
return
}
yxArg, ok := yx.Args[knowledge.Arg("len.v")].(*ast.Ident)
if !ok {
var yxArg *ast.Ident
switch s := yx.Args[knowledge.Arg("len.v")].(type) {
case *ast.Ident:
yxArg = s
case *ast.SelectorExpr:
yxArg = s.Sel
default:
return
}
if yxArg.Name != xx.Name {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,21 @@ func fn3() {
if x == nil || len(x) == 0 {
}
}

func issue1527() {
var t struct {
pa *[5]int
s []int
m map[uint64]bool
ch chan int
}

if t.s == nil || len(t.s) == 0 { //@ diag(`should omit nil check`)
}
if t.m == nil || len(t.m) == 0 { //@ diag(`should omit nil check`)
}
if t.ch == nil || len(t.ch) == 0 { //@ diag(`should omit nil check`)
}
if t.pa == nil || len(t.pa) == 0 { // nil check cannot be removed with pointer to an array
}
}

0 comments on commit 2fcd44d

Please sign in to comment.