Skip to content

Commit 54d9a09

Browse files
shmsrchavacava
andauthored
Incorrect handling of token.{LEQ,GEQ} for constant-logical-expr (#642)
* fix: incorrect handling of token.{LEQ,GEQ} for constant-logical-expr lint Signed-off-by: subham sarkar <[email protected]> * tiny modification in comments Co-authored-by: chavacava <[email protected]>
1 parent d4fbc92 commit 54d9a09

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

rule/constant-logical-expr.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package rule
22

33
import (
4-
"github.com/mgechev/revive/lint"
54
"go/ast"
65
"go/token"
6+
7+
"github.com/mgechev/revive/lint"
78
)
89

910
// ConstantLogicalExprRule warns on constant logical expressions.
@@ -44,11 +45,13 @@ func (w *lintConstantLogicalExpr) Visit(node ast.Node) ast.Visitor {
4445
return w
4546
}
4647

47-
if n.Op == token.EQL {
48+
// Handles cases like: a <= a, a == a, a >= a
49+
if w.isEqualityOperator(n.Op) {
4850
w.newFailure(n, "expression always evaluates to true")
4951
return w
5052
}
5153

54+
// Handles cases like: a < a, a > a, a != a
5255
if w.isInequalityOperator(n.Op) {
5356
w.newFailure(n, "expression always evaluates to false")
5457
return w
@@ -69,9 +72,18 @@ func (w *lintConstantLogicalExpr) isOperatorWithLogicalResult(t token.Token) boo
6972
return false
7073
}
7174

75+
func (w *lintConstantLogicalExpr) isEqualityOperator(t token.Token) bool {
76+
switch t {
77+
case token.EQL, token.LEQ, token.GEQ:
78+
return true
79+
}
80+
81+
return false
82+
}
83+
7284
func (w *lintConstantLogicalExpr) isInequalityOperator(t token.Token) bool {
7385
switch t {
74-
case token.LSS, token.GTR, token.NEQ, token.LEQ, token.GEQ:
86+
case token.LSS, token.GTR, token.NEQ:
7587
return true
7688
}
7789

testdata/constant-logical-expr.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package fixtures
22

3+
import "fmt"
4+
35
// from github.com/ugorji/go/codec/helper.go
46
func isNaN(f float64) bool { return f != f } // MATCH /expression always evaluates to false/
57

@@ -9,9 +11,9 @@ func foo1(f float64) bool { return foo2(2.) > foo2(2.) } // MATCH /expression al
911

1012
func foo2(f float64) bool { return f < f } // MATCH /expression always evaluates to false/
1113

12-
func foo3(f float64) bool { return f <= f } // MATCH /expression always evaluates to false/
14+
func foo3(f float64) bool { return f <= f } // MATCH /expression always evaluates to true/
1315

14-
func foo4(f float64) bool { return f >= f } // MATCH /expression always evaluates to false/
16+
func foo4(f float64) bool { return f >= f } // MATCH /expression always evaluates to true/
1517

1618
func foo5(f float64) bool { return f == f } // MATCH /expression always evaluates to true/
1719

0 commit comments

Comments
 (0)