Skip to content

Commit

Permalink
simple/s1009: improve diagnostic message
Browse files Browse the repository at this point in the history
We made the message significantly worse in
5de5bd3 by printing something like "len
for []int is defined as zero", which is wrong and not helpful. This
change restores the original message, "len for nil slices is defined as
zero".
  • Loading branch information
dominikh committed Sep 20, 2024
1 parent 9f4b51e commit 9c18844
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
6 changes: 5 additions & 1 deletion simple/s1009/s1009.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,17 @@ func run(pass *analysis.Pass) (interface{}, error) {
// finally check that xx type is one of array, slice, map or chan
// this is to prevent false positive in case if xx is a pointer to an array
typ := pass.TypesInfo.TypeOf(xx)
var nilType string
ok = typeutil.All(typ, func(term *types.Term) bool {
switch term.Type().Underlying().(type) {
case *types.Slice:
nilType = "nil slices"
return true
case *types.Map:
nilType = "nil maps"
return true
case *types.Chan:
nilType = "nil channels"
return true
case *types.Pointer:
return false
Expand All @@ -178,7 +182,7 @@ func run(pass *analysis.Pass) (interface{}, error) {
return
}

report.Report(pass, expr, fmt.Sprintf("should omit nil check; len() for %s is defined as zero", typ), report.FilterGenerated())
report.Report(pass, expr, fmt.Sprintf("should omit nil check; len() for %s is defined as zero", nilType), report.FilterGenerated())
}
code.Preorder(pass, fn, (*ast.BinaryExpr)(nil))
return nil, nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ func fn() {
var m map[int]int
var ch chan int

if s == nil || len(s) == 0 { //@ diag(`should omit nil check`)
if s == nil || len(s) == 0 { //@ diag(re`should omit nil check.+for nil slices`)
}
if m == nil || len(m) == 0 { //@ diag(`should omit nil check`)
if m == nil || len(m) == 0 { //@ diag(re`should omit nil check.+for nil maps`)
}
if ch == nil || len(ch) == 0 { //@ diag(`should omit nil check`)
if ch == nil || len(ch) == 0 { //@ diag(re`should omit nil check.+for nil channels`)
}

if s != nil && len(s) != 0 { //@ diag(`should omit nil check`)
Expand Down

0 comments on commit 9c18844

Please sign in to comment.