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 new Style/CombinableLoops
cop
#8486
Conversation
9ac2baa
to
56258d9
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting. You didn't want to raise on offense for combinable each
with each_with_index
?
@@ -15,18 +15,14 @@ | |||
a %{op} b | |||
RUBY | |||
end | |||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I love it when you find things in this codebase 👍
56258d9
to
069fed5
Compare
Updated with suggestions. |
|
||
def collection_looping_method?(node) | ||
method_name = node.send_node.method_name | ||
method_name.match?(/^each/) || method_name.match?(/_each$/) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the || method_name.match?(/_each$/)
is still not covered by the specs, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added test for reverse_each
069fed5
to
0ee077a
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. I'm usggesting a small wording change, and maybe @bbatsov has a better idea for the cop's name?
0ee077a
to
f245bc7
Compare
I'm guessing we should make this work with |
f245bc7
to
6ea767b
Compare
Yes, added support for it.
I guess it will be better implemented as a new cop? |
Yeah, definitely. |
This cop checks for places where multiple consecutive loops over the same data can be combined into a single loop.
It is very likely that combining them will make the code more efficient and more concise.
Inspired by https://rules.sonarsource.com/java/RSPEC-3047
I ran it on ~30k files and it detected 133 offenses. So seems useful.