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

A cop to check erroneous next within reduce. #2581

Merged
merged 1 commit into from
Jan 13, 2016

Conversation

mvidner
Copy link
Contributor

@mvidner mvidner commented Jan 5, 2016

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

@@ -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][])
Copy link
Contributor

Choose a reason for hiding this comment

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

???? needs to be fixed.

@mvidner
Copy link
Contributor Author

mvidner commented Jan 5, 2016

Thanks for the suggestions!

@bbatsov
Copy link
Collaborator

bbatsov commented Jan 10, 2016

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 master)

describe RuboCop::Cop::Lint::NextWithoutAccumulator do
subject(:cop) { described_class.new }

context 'given a reduce block' do
Copy link
Collaborator

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.

@bbatsov
Copy link
Collaborator

bbatsov commented Jan 12, 2016

Drop the . from the commit message's title, please. Titles are not complete sentences.

@bbatsov
Copy link
Collaborator

bbatsov commented Jan 13, 2016

Now the build seems to be failing.

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
@mvidner
Copy link
Contributor Author

mvidner commented Jan 13, 2016

I've fixed the build. Thanks for the comments!

bbatsov added a commit that referenced this pull request Jan 13, 2016
A cop to check erroneous next within reduce.
@bbatsov bbatsov merged commit 9732d37 into rubocop:master Jan 13, 2016
@mvidner mvidner deleted the next-without-accumulator branch January 13, 2016 11:53
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.

3 participants