2018-08-23 20:10:17 +02:00
|
|
|
package rule
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"go/ast"
|
|
|
|
|
"go/token"
|
2018-09-15 14:33:28 -07:00
|
|
|
|
|
|
|
|
"github.com/mgechev/revive/lint"
|
2018-08-23 20:10:17 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// BoolLiteralRule warns when logic expressions contains Boolean literals.
|
|
|
|
|
type BoolLiteralRule struct{}
|
|
|
|
|
|
|
|
|
|
// Apply applies the rule to given file.
|
2022-04-10 11:55:13 +02:00
|
|
|
func (*BoolLiteralRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
|
2018-08-23 20:10:17 +02:00
|
|
|
var failures []lint.Failure
|
|
|
|
|
|
|
|
|
|
onFailure := func(failure lint.Failure) {
|
|
|
|
|
failures = append(failures, failure)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
astFile := file.AST
|
|
|
|
|
w := &lintBoolLiteral{astFile, onFailure}
|
|
|
|
|
ast.Walk(w, astFile)
|
|
|
|
|
|
|
|
|
|
return failures
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Name returns the rule name.
|
2022-04-10 11:55:13 +02:00
|
|
|
func (*BoolLiteralRule) Name() string {
|
2018-08-23 20:10:17 +02:00
|
|
|
return "bool-literal-in-expr"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type lintBoolLiteral struct {
|
|
|
|
|
file *ast.File
|
|
|
|
|
onFailure func(lint.Failure)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (w *lintBoolLiteral) Visit(node ast.Node) ast.Visitor {
|
2025-05-01 12:40:29 +03:00
|
|
|
if n, ok := node.(*ast.BinaryExpr); ok {
|
2018-08-23 20:10:17 +02:00
|
|
|
if !isBoolOp(n.Op) {
|
|
|
|
|
return w
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lexeme, ok := isExprABooleanLit(n.X)
|
|
|
|
|
if !ok {
|
|
|
|
|
lexeme, ok = isExprABooleanLit(n.Y)
|
|
|
|
|
if !ok {
|
|
|
|
|
return w
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
isConstant := (n.Op == token.LAND && lexeme == "false") || (n.Op == token.LOR && lexeme == "true")
|
|
|
|
|
|
|
|
|
|
if isConstant {
|
2025-01-18 13:16:19 +02:00
|
|
|
w.addFailure(n, "Boolean expression seems to always evaluate to "+lexeme, lint.FailureCategoryLogic)
|
2018-08-23 20:10:17 +02:00
|
|
|
} else {
|
2025-01-18 13:16:19 +02:00
|
|
|
w.addFailure(n, "omit Boolean literal in expression", lint.FailureCategoryStyle)
|
2018-08-23 20:10:17 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return w
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-18 13:16:19 +02:00
|
|
|
func (w lintBoolLiteral) addFailure(node ast.Node, msg string, cat lint.FailureCategory) {
|
2018-08-23 20:10:17 +02:00
|
|
|
w.onFailure(lint.Failure{
|
|
|
|
|
Confidence: 1,
|
|
|
|
|
Node: node,
|
|
|
|
|
Category: cat,
|
|
|
|
|
Failure: msg,
|
|
|
|
|
})
|
|
|
|
|
}
|
2024-11-19 23:58:14 +02:00
|
|
|
|
|
|
|
|
// isBoolOp returns true if the given token corresponds to a bool operator.
|
|
|
|
|
func isBoolOp(t token.Token) bool {
|
|
|
|
|
switch t {
|
|
|
|
|
case token.LAND, token.LOR, token.EQL, token.NEQ:
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func isExprABooleanLit(n ast.Node) (lexeme string, ok bool) {
|
|
|
|
|
oper, ok := n.(*ast.Ident)
|
|
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
|
return "", false
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-31 12:33:51 +01:00
|
|
|
return oper.Name, oper.Name == "true" || oper.Name == "false"
|
2024-11-19 23:58:14 +02:00
|
|
|
}
|