Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optimize-operands-order rule (#599) #603

Merged

Conversation

doniacld
Copy link
Contributor

@doniacld doniacld commented Oct 18, 2021

Description

Add swap conditional expression rule that closes #599.

Test

Add a unit test with several cases that should be spotted and other not.
Tested on a repository, here is the output:

cmd/kidlectl/create.go
  (24, 10)  https://revive.run/r#swap-conditional-expr  swap 'tito(y)' and 'y' around '&&' operator  
  (25, 9)   https://revive.run/r#swap-conditional-expr  swap 'tito(y)' and 'y' around '||' operator  

Closes #599

@doniacld doniacld force-pushed the issue-599-add-conditional-exp-swipe-rule branch from ff23a0a to 851a057 Compare October 18, 2021 21:02
Copy link
Collaborator

@chavacava chavacava left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR @doniacld, I've made some comments

rule/swap-conditional-expr.go Outdated Show resolved Hide resolved
rule/swap-conditional-expr.go Outdated Show resolved Hide resolved
rule/swap-conditional-expr.go Outdated Show resolved Hide resolved
rule/swap-conditional-expr.go Outdated Show resolved Hide resolved
rule/swap-conditional-expr.go Outdated Show resolved Hide resolved
testdata/swap-conditional-expr.go Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
@doniacld
Copy link
Contributor Author

@chavacava Thanks for the code review.
Until we did not agree on a better name, please do not merge. :)

@chavacava
Copy link
Collaborator

chavacava commented Oct 20, 2021

I've tested the new rule on revive's code. There are some good warnings:

rule/defer.go:91:6

but also some false positives:

rule/var-naming.go:148:6

False positives are generated in cases where the right subexpression contains a function call, for example

caller(x, y) && (x && caller(y, x))

The conditions in

// check left expression is a caller
x, xCaller := binExpr.X.(*ast.CallExpr)
if !xCaller {
return w
}
// check right expression is not a caller
_, yCaller := binExpr.Y.(*ast.CallExpr)
if yCaller {
return w
}

should check if the sub expression tree has a function call (the current check is only on the root of the sub-expression tree) To do that you can use

revive/rule/utils.go

Lines 112 to 116 in 0ee7866

// pick yields a list of nodes by picking them from a sub-ast with root node n.
// Nodes are selected by applying the fselect function
// f function is applied to each selected node before inseting it in the final result.
// If f==nil then it defaults to the identity function (ie it returns the node itself)
func pick(n ast.Node, fselect func(n ast.Node) bool, f func(n ast.Node) []ast.Node) []ast.Node {

to pick function calls in the sub expression and then check if there is >1 (left) and 0 (right)

@doniacld doniacld force-pushed the issue-599-add-conditional-exp-swipe-rule branch from 5eaf79c to b1ace60 Compare October 21, 2021 18:36
@chavacava
Copy link
Collaborator

I've tested the rule over some open source repositories and it looks great!
I've modified the failure's message, let me know if you are OK with it, we just need to find a good name for the rule :)

@doniacld doniacld force-pushed the issue-599-add-conditional-exp-swipe-rule branch from ed3f8bd to 0b66738 Compare October 22, 2021 20:56
@chavacava chavacava merged commit 5d04216 into mgechev:master Oct 23, 2021
@doniacld doniacld changed the title Add swap-conditional-expr rule (#599) Add optimize-operands-order rule (#599) Oct 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rule to spot (possibly) inefficient conditional expressions
2 participants