Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Co-authored-by: SalvadorC <salvadorcavadini+github@gmail.com>
- Loading branch information
Showing
7 changed files
with
139 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package rule | ||
|
||
import ( | ||
"fmt" | ||
"go/ast" | ||
"go/token" | ||
|
||
"github.com/mgechev/revive/lint" | ||
) | ||
|
||
// OptimizeOperandsOrderRule lints given else constructs. | ||
type OptimizeOperandsOrderRule struct{} | ||
|
||
// Apply applies the rule to given file. | ||
func (r *OptimizeOperandsOrderRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { | ||
var failures []lint.Failure | ||
|
||
onFailure := func(failure lint.Failure) { | ||
failures = append(failures, failure) | ||
} | ||
w := lintOptimizeOperandsOrderlExpr{ | ||
onFailure: onFailure, | ||
} | ||
ast.Walk(w, file.AST) | ||
return failures | ||
} | ||
|
||
// Name returns the rule name. | ||
func (r *OptimizeOperandsOrderRule) Name() string { | ||
return "optimize-operands-order" | ||
} | ||
|
||
type lintOptimizeOperandsOrderlExpr struct { | ||
onFailure func(failure lint.Failure) | ||
} | ||
|
||
// Visit checks boolean AND and OR expressions to determine | ||
// if swapping their operands may result in an execution speedup. | ||
func (w lintOptimizeOperandsOrderlExpr) Visit(node ast.Node) ast.Visitor { | ||
binExpr, ok := node.(*ast.BinaryExpr) | ||
if !ok { | ||
return w | ||
} | ||
|
||
switch binExpr.Op { | ||
case token.LAND, token.LOR: | ||
default: | ||
return w | ||
} | ||
|
||
isCaller := func(n ast.Node) bool { | ||
_, ok := n.(*ast.CallExpr) | ||
return ok | ||
} | ||
|
||
// check if the left sub-expression contains a function call | ||
nodes := pick(binExpr.X, isCaller, nil) | ||
if len(nodes) < 1 { | ||
return w | ||
} | ||
|
||
// check if the right sub-expression does not contain a function call | ||
nodes = pick(binExpr.Y, isCaller, nil) | ||
if len(nodes) > 0 { | ||
return w | ||
} | ||
|
||
newExpr := ast.BinaryExpr{X: binExpr.Y, Y: binExpr.X, Op: binExpr.Op} | ||
w.onFailure(lint.Failure{ | ||
Failure: fmt.Sprintf("for better performance '%v' might be rewritten as '%v'", gofmt(binExpr), gofmt(&newExpr)), | ||
Node: node, | ||
Category: "optimization", | ||
Confidence: 0.3, | ||
}) | ||
|
||
return w | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/mgechev/revive/lint" | ||
"github.com/mgechev/revive/rule" | ||
) | ||
|
||
// Test that left and right side of Binary operators (only AND, OR) are swapable | ||
func TestOptimizeOperandsOrder(t *testing.T) { | ||
testRule(t, "optimize-operands-order", &rule.OptimizeOperandsOrderRule{}, &lint.RuleConfig{}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package fixtures | ||
|
||
func conditionalExpr(x, y bool) bool { | ||
equal := x == y // should not match, not AND or OR operators | ||
if x || y { // should not match, no caller | ||
return true | ||
} | ||
or := caller(x, y) || y // MATCH /for better performance 'caller(x, y) || y' might be rewritten as 'y || caller(x, y)'/ | ||
if caller(x, y) || y { // MATCH /for better performance 'caller(x, y) || y' might be rewritten as 'y || caller(x, y)'/ | ||
return true | ||
} | ||
|
||
switch { | ||
case x == y: | ||
return y | ||
case caller(x, y) && y: // MATCH /for better performance 'caller(x, y) && y' might be rewritten as 'y && caller(x, y)'/ | ||
return x | ||
} | ||
|
||
complexExpr := caller(caller(x, y) && y, y) || y | ||
// MATCH:20 /for better performance 'caller(caller(x, y) && y, y) || y' might be rewritten as 'y || caller(caller(x, y) && y, y)'/ | ||
// MATCH:20 /for better performance 'caller(x, y) && y' might be rewritten as 'y && caller(x, y)'/ | ||
|
||
noSwap := caller(x, y) || (x && caller(y, x)) // should not match, caller in the right operand | ||
|
||
callRight := caller(x, y) && (x && caller(y, x)) // should not match, caller in the right operand | ||
return caller(x, y) && y // MATCH /for better performance 'caller(x, y) && y' might be rewritten as 'y && caller(x, y)'/ | ||
} | ||
|
||
func caller(x, y bool) bool { | ||
return true | ||
} |