Skip to content
This repository has been archived by the owner on Dec 30, 2018. It is now read-only.

Commit

Permalink
SA1002: do not suggest double negation
Browse files Browse the repository at this point in the history
Closes gh-33
  • Loading branch information
dominikh committed Jan 20, 2017
1 parent 4fca917 commit 39e531f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lint.go
Expand Up @@ -186,8 +186,13 @@ func LintIfBoolCmp(f *lint.File) {
if (expr.Op == token.EQL && !val) || (expr.Op == token.NEQ && val) {
op = "!"
}
f.Errorf(expr, "should omit comparison to bool constant, can be simplified to %s%s",
op, f.Render(other))
r := op + f.Render(other)
l1 := len(r)
r = strings.TrimLeft(r, "!")
if (l1-len(r))%2 == 1 {
r = "!" + r
}
f.Errorf(expr, "should omit comparison to bool constant, can be simplified to %s", r)
return true
}
f.Walk(fn)
Expand Down
22 changes: 22 additions & 0 deletions testdata/bool-cmp.go
Expand Up @@ -26,6 +26,28 @@ func fn() {
var y bool
for y != true { // MATCH /simplified to !y/
}
if !y == true { // MATCH /simplified to !y/
}
if !y == false { // MATCH /simplified to y/
}
if !y != true { // MATCH /simplified to y/
}
if !y != false { // MATCH /simplified to !y/
}
if !!y == false { // MATCH /simplified to !y/
}
if !!!y == false { // MATCH /simplified to y/
}
if !!y == true { // MATCH /simplified to y/
}
if !!!y == true { // MATCH /simplified to !y/
}
if !!y != true { // MATCH /simplified to !y/
}
if !!!y != true { // MATCH /simplified to y/
}
if !y == !false { // not matched because we expect true/false on one side, not !false
}

var z interface{}
if z == true {
Expand Down

0 comments on commit 39e531f

Please sign in to comment.