-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
A cop to check erroneous next within reduce. #2581
Conversation
@@ -170,6 +170,7 @@ | |||
* `Style/Documentation` recognizes 'Constant = Class.new' as a class definition. ([@alexdowad][]) | |||
* [#1608](https://github.com/bbatsov/rubocop/issues/1608): Add new 'align_braces' style for `Style/IndentHash`. ([@alexdowad][]) | |||
* `Style/Next` can autocorrect. ([@alexdowad][]) | |||
* [#????](https://github.com/bbatsov/rubocop/pull/????): New cop `Lint/NextWithoutAccumulator` finds bare `next` in `reduce`/`inject` blocks which assigns `nil` to the accumulator. ([@mvidner][]) |
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.
???? needs to be fixed.
Thanks for the suggestions! |
5ba9138
to
104dd77
Compare
Overall the cop looks good. Just squash those commits together and update the final message to match our commit message style. (and rebase of top of |
describe RuboCop::Cop::Lint::NextWithoutAccumulator do | ||
subject(:cop) { described_class.new } | ||
|
||
context 'given a reduce block' do |
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.
You can use shared examples for reduce
and inject
.
104dd77
to
0bff3d2
Compare
Drop the |
0bff3d2
to
4dbd360
Compare
Now the build seems to be failing. |
4dbd360
to
ea29985
Compare
In a reduce (aka inject), the accumulator value must be maintained. If you want to short-circuit an iteration and do it with a `next` the accumulator will become `nil`. You should use `next acc`. For reference, here is a real bug reported in my team about such code: http://lists.opensuse.org/yast-devel/2015-11/msg00094.html
ea29985
to
1804933
Compare
I've fixed the build. Thanks for the comments! |
A cop to check erroneous next within reduce.
In a reduce (aka inject), the accumulator value must be maintained.
If you want to short-circuit an iteration and do it with a
next
the accumulator will become
nil
. You should usenext acc
.For reference, here is a real bug reported in my team about such code:
http://lists.opensuse.org/yast-devel/2015-11/msg00094.html