From 85f21d99cda6d29392fa754a75159ebc43fe26d9 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Tue, 31 Mar 2020 12:18:55 +0900 Subject: [PATCH] [Fix #7831] Fix a false positive for `Style/HashEachMethods` Fixes #7831. This PR fix a false positive for `Style/HashEachMethods` when there is no receiver for `keys` and `values`. The following is an examples. ```ruby values.each { |k, v| do_something(k, v) } keys.each { |k, v| do_something(k, v) } ``` False negatives occur in cases such as object that inherit Hash, but that is perhaps a corner case. I think it is more worthwhile to resolve the false positives with common naming of `values`. --- CHANGELOG.md | 1 + lib/rubocop/cop/style/hash_each_methods.rb | 2 ++ .../cop/style/hash_each_methods_spec.rb | 18 ++++-------------- 3 files changed, 7 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3bb05172c65..4b5d0661ea4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,7 @@ * [#7814](https://github.com/rubocop-hq/rubocop/issues/7814): Fix a false positive for `Migrate/DepartmentName` cop when inspecting an unexpected disabled comment format. ([@koic][]) * [#7728](https://github.com/rubocop-hq/rubocop/issues/7728): Fix an error for `Style/OneLineConditional` when one of the branches contains a self keyword. ([@koic][]) * [#7825](https://github.com/rubocop-hq/rubocop/issues/7825): Fix crash for `Layout/MultilineMethodCallIndentation` with key access to hash. ([@tejasbubane][]) +* [#7831](https://github.com/rubocop-hq/rubocop/issues/7831): Fix a false positive for `Style/HashEachMethods` when receiver is implicit. ([@koic][]) ### Changes diff --git a/lib/rubocop/cop/style/hash_each_methods.rb b/lib/rubocop/cop/style/hash_each_methods.rb index cadf38cd939..31e5e1e939f 100644 --- a/lib/rubocop/cop/style/hash_each_methods.rb +++ b/lib/rubocop/cop/style/hash_each_methods.rb @@ -40,6 +40,8 @@ def autocorrect(node) def register_kv_offense(node) kv_each(node) do |target, method| + return unless target.receiver.receiver + msg = format(message, prefer: "each_#{method[0..-2]}", current: "#{method}.each") diff --git a/spec/rubocop/cop/style/hash_each_methods_spec.rb b/spec/rubocop/cop/style/hash_each_methods_spec.rb index 3d8c2ddc686..c6356a60a05 100644 --- a/spec/rubocop/cop/style/hash_each_methods_spec.rb +++ b/spec/rubocop/cop/style/hash_each_methods_spec.rb @@ -69,25 +69,15 @@ end context 'when receiver is implicit' do - it 'registers an offense and auto-corrects keys.each with each_key' do - expect_offense(<<~RUBY) + it 'does not register an offense for `keys.each`' do + expect_no_offenses(<<~RUBY) keys.each { |k| p k } - ^^^^^^^^^ Use `each_key` instead of `keys.each`. - RUBY - - expect_correction(<<~RUBY) - each_key { |k| p k } RUBY end - it 'registers an offense and auto-corrects values.each with each_value' do - expect_offense(<<~RUBY) + it 'does not register an offense for `values.each`' do + expect_no_offenses(<<~RUBY) values.each { |v| p v } - ^^^^^^^^^^^ Use `each_value` instead of `values.each`. - RUBY - - expect_correction(<<~RUBY) - each_value { |v| p v } RUBY end