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:
parent
0df1bb0860
commit
689291fbcb
@ -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
|
||||
|
14
testdata/optimize-operands-order.go
vendored
14
testdata/optimize-operands-order.go
vendored
@ -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
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user