mirror of
https://github.com/mgechev/revive.git
synced 2025-11-23 22:04:49 +02:00
rule optimize-operands-order: do not consider built-in len as a caller (#1005)
This commit is contained in:
committed by
GitHub
parent
0df1bb0860
commit
689291fbcb
@@ -49,8 +49,17 @@ func (w lintOptimizeOperandsOrderlExpr) Visit(node ast.Node) ast.Visitor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
isCaller := func(n ast.Node) bool {
|
isCaller := func(n ast.Node) bool {
|
||||||
_, ok := n.(*ast.CallExpr)
|
ce, ok := n.(*ast.CallExpr)
|
||||||
return ok
|
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
|
// 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)'/
|
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 {
|
func caller(x, y bool) bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user