From e95eb54511b4c2f449487496c0f30001ce57bff5 Mon Sep 17 00:00:00 2001 From: Daniel Vandersluis Date: Sat, 3 Oct 2020 22:39:02 -0400 Subject: [PATCH] [Fix #8848] Fix a false positive for `Style/CombinableLoops` when using the same method with different arguments. --- CHANGELOG.md | 1 + docs/modules/ROOT/pages/cops_style.adoc | 6 ++++++ lib/rubocop/cop/style/combinable_loops.rb | 9 ++++++++- spec/rubocop/cop/style/combinable_loops_spec.rb | 7 +++++++ 4 files changed, 22 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 460a457cf2a..58ddf298f5b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/docs/modules/ROOT/pages/cops_style.adoc b/docs/modules/ROOT/pages/cops_style.adoc index 2522c37cd9d..53e3b5a10fd 100644 --- a/docs/modules/ROOT/pages/cops_style.adoc +++ b/docs/modules/ROOT/pages/cops_style.adoc @@ -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 diff --git a/lib/rubocop/cop/style/combinable_loops.rb b/lib/rubocop/cop/style/combinable_loops.rb index 2277e5d94e6..7099436f8ee 100644 --- a/lib/rubocop/cop/style/combinable_loops.rb +++ b/lib/rubocop/cop/style/combinable_loops.rb @@ -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.' @@ -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 diff --git a/spec/rubocop/cop/style/combinable_loops_spec.rb b/spec/rubocop/cop/style/combinable_loops_spec.rb index 0de1450d8b7..e1f6b9ad2e7 100644 --- a/spec/rubocop/cop/style/combinable_loops_spec.rb +++ b/spec/rubocop/cop/style/combinable_loops_spec.rb @@ -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