Skip to content

Commit

Permalink
Merge pull request #8851 from dvandersluis/issue/8848
Browse files Browse the repository at this point in the history
[Fix #8848] Fix a false positive for `Style/CombinableLoops` when using the same method with different arguments
  • Loading branch information
koic committed Oct 4, 2020
2 parents 0203a76 + e95eb54 commit 99818f6
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -17,6 +17,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

0 comments on commit 99818f6

Please sign in to comment.