mirror of
https://github.com/mgechev/revive.git
synced 2025-07-01 00:24:54 +02:00
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 <sarkar.subhams2@gmail.com> * tiny modification in comments Co-authored-by: chavacava <salvadorcavadini+github@gmail.com>
This commit is contained in:
@ -1,9 +1,10 @@
|
|||||||
package rule
|
package rule
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/mgechev/revive/lint"
|
|
||||||
"go/ast"
|
"go/ast"
|
||||||
"go/token"
|
"go/token"
|
||||||
|
|
||||||
|
"github.com/mgechev/revive/lint"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ConstantLogicalExprRule warns on constant logical expressions.
|
// ConstantLogicalExprRule warns on constant logical expressions.
|
||||||
@ -44,11 +45,13 @@ func (w *lintConstantLogicalExpr) Visit(node ast.Node) ast.Visitor {
|
|||||||
return w
|
return w
|
||||||
}
|
}
|
||||||
|
|
||||||
if n.Op == token.EQL {
|
// Handles cases like: a <= a, a == a, a >= a
|
||||||
|
if w.isEqualityOperator(n.Op) {
|
||||||
w.newFailure(n, "expression always evaluates to true")
|
w.newFailure(n, "expression always evaluates to true")
|
||||||
return w
|
return w
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Handles cases like: a < a, a > a, a != a
|
||||||
if w.isInequalityOperator(n.Op) {
|
if w.isInequalityOperator(n.Op) {
|
||||||
w.newFailure(n, "expression always evaluates to false")
|
w.newFailure(n, "expression always evaluates to false")
|
||||||
return w
|
return w
|
||||||
@ -69,9 +72,18 @@ func (w *lintConstantLogicalExpr) isOperatorWithLogicalResult(t token.Token) boo
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (w *lintConstantLogicalExpr) isEqualityOperator(t token.Token) bool {
|
||||||
|
switch t {
|
||||||
|
case token.EQL, token.LEQ, token.GEQ:
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
func (w *lintConstantLogicalExpr) isInequalityOperator(t token.Token) bool {
|
func (w *lintConstantLogicalExpr) isInequalityOperator(t token.Token) bool {
|
||||||
switch t {
|
switch t {
|
||||||
case token.LSS, token.GTR, token.NEQ, token.LEQ, token.GEQ:
|
case token.LSS, token.GTR, token.NEQ:
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
6
testdata/constant-logical-expr.go
vendored
6
testdata/constant-logical-expr.go
vendored
@ -1,5 +1,7 @@
|
|||||||
package fixtures
|
package fixtures
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
// from github.com/ugorji/go/codec/helper.go
|
// from github.com/ugorji/go/codec/helper.go
|
||||||
func isNaN(f float64) bool { return f != f } // MATCH /expression always evaluates to false/
|
func isNaN(f float64) bool { return f != f } // MATCH /expression always evaluates to false/
|
||||||
|
|
||||||
@ -9,9 +11,9 @@ func foo1(f float64) bool { return foo2(2.) > foo2(2.) } // MATCH /expression al
|
|||||||
|
|
||||||
func foo2(f float64) bool { return f < f } // MATCH /expression always evaluates to false/
|
func foo2(f float64) bool { return f < f } // MATCH /expression always evaluates to false/
|
||||||
|
|
||||||
func foo3(f float64) bool { return f <= f } // MATCH /expression always evaluates to false/
|
func foo3(f float64) bool { return f <= f } // MATCH /expression always evaluates to true/
|
||||||
|
|
||||||
func foo4(f float64) bool { return f >= f } // MATCH /expression always evaluates to false/
|
func foo4(f float64) bool { return f >= f } // MATCH /expression always evaluates to true/
|
||||||
|
|
||||||
func foo5(f float64) bool { return f == f } // MATCH /expression always evaluates to true/
|
func foo5(f float64) bool { return f == f } // MATCH /expression always evaluates to true/
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user