Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

s1009: fix with SelectorExpr #1548

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}
Loading