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

[Fix #8848] Fix a false positive for Style/CombinableLoops when using the same method with different arguments #8851

Merged
merged 1 commit into from Oct 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -16,6 +16,7 @@
* [#8354](https://github.com/rubocop-hq/rubocop/issues/8354): Detect regexp named captures in `Style/CaseLikeIf` cop. ([@dsavochkin][])
* [#8830](https://github.com/rubocop-hq/rubocop/issues/8830): Fix bad autocorrect of `Style/StringConcatenation` when string includes double quotes. ([@tleish][])
* [#8807](https://github.com/rubocop-hq/rubocop/pull/8807): Fix a false positive for `Style/RedundantCondition` when using assignment by hash key access. ([@koic][])
* [#8848](https://github.com/rubocop-hq/rubocop/issues/8848): Fix a false positive for `Style/CombinableLoops` when using the same method with different arguments. ([@dvandersluis][])

### Changes

Expand Down
6 changes: 6 additions & 0 deletions docs/modules/ROOT/pages/cops_style.adoc
Expand Up @@ -1437,6 +1437,12 @@ def method
do_something_else(item)
end
end

# good
def method
each_slice(2) { |slice| do_something(slice) }
each_slice(3) { |slice| do_something(slice) }
end
----

== Style/CommandLiteral
Expand Down
9 changes: 8 additions & 1 deletion lib/rubocop/cop/style/combinable_loops.rb
Expand Up @@ -49,6 +49,12 @@ module Style
# end
# end
#
# # good
# def method
# each_slice(2) { |slice| do_something(slice) }
# each_slice(3) { |slice| do_something(slice) }
# end
#
class CombinableLoops < Base
MSG = 'Combine this loop with the previous loop.'

Expand Down Expand Up @@ -76,7 +82,8 @@ def collection_looping_method?(node)
def same_collection_looping?(node, sibling)
sibling&.block_type? &&
sibling.send_node.method?(node.method_name) &&
sibling.send_node.receiver == node.send_node.receiver
sibling.send_node.receiver == node.send_node.receiver &&
sibling.send_node.arguments == node.send_node.arguments
end
end
end
Expand Down
7 changes: 7 additions & 0 deletions spec/rubocop/cop/style/combinable_loops_spec.rb
Expand Up @@ -53,6 +53,13 @@
end
RUBY
end

it 'does not register an offense for when the same method with different arguments' do
expect_no_offenses(<<~RUBY)
each_slice(2) { |slice| do_something(slice) }
each_slice(3) { |slice| do_something(slice) }
RUBY
end
end

context 'when for loop' do
Expand Down