1
0
mirror of https://github.com/mgechev/revive.git synced 2025-03-17 20:57:58 +02:00

rule optimize-operands-order: do not consider built-in len as a caller (#1005)

This commit is contained in:
Kostas Stamatakis 2024-07-02 09:22:13 +03:00 committed by GitHub
parent 0df1bb0860
commit 689291fbcb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 2 deletions

View File

@ -49,8 +49,17 @@ func (w lintOptimizeOperandsOrderlExpr) Visit(node ast.Node) ast.Visitor {
}
isCaller := func(n ast.Node) bool {
_, ok := n.(*ast.CallExpr)
return ok
ce, ok := n.(*ast.CallExpr)
if !ok {
return false
}
ident, isIdent := ce.Fun.(*ast.Ident)
if !isIdent {
return true
}
return ident.Name != "len" || ident.Obj != nil
}
// check if the left sub-expression contains a function call

View File

@ -27,6 +27,20 @@ func conditionalExpr(x, y bool) bool {
return caller(x, y) && y // MATCH /for better performance 'caller(x, y) && y' might be rewritten as 'y && caller(x, y)'/
}
func conditionalExprSlice(s []string) bool {
if len(s) > 0 || s[0] == "" { // should not match, not safe
return false
}
f := func() bool {
len(s) > 1
}
if f() || s[0] == "test" { // MATCH /for better performance 'f() || s[0] == "test"' might be rewritten as 's[0] == "test" || f()'/
return true
}
}
func caller(x, y bool) bool {
return true
}