From 3f4bf548ca404002232740c330b118c261211ab1 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Mon, 7 Nov 2022 22:13:39 +0900 Subject: [PATCH] [Fix #11161] Fix a false positive for `Style/HashAsLastArrayItem` Fixes #11161. This PR fixes a false positive for `Style/HashAsLastArrayItem` when using double splat operator. --- ...ix_a_false_positive_for_style_hash_as_last_array_item.md | 1 + lib/rubocop/cop/style/hash_as_last_array_item.rb | 1 + spec/rubocop/cop/style/hash_as_last_array_item_spec.rb | 6 ++++++ 3 files changed, 8 insertions(+) create mode 100644 changelog/fix_a_false_positive_for_style_hash_as_last_array_item.md diff --git a/changelog/fix_a_false_positive_for_style_hash_as_last_array_item.md b/changelog/fix_a_false_positive_for_style_hash_as_last_array_item.md new file mode 100644 index 00000000000..32cbc4fb9ce --- /dev/null +++ b/changelog/fix_a_false_positive_for_style_hash_as_last_array_item.md @@ -0,0 +1 @@ +* [#11161](https://github.com/rubocop/rubocop/issues/11161): Fix a false positive for `Style/HashAsLastArrayItem` when using double splat operator. ([@koic][]) diff --git a/lib/rubocop/cop/style/hash_as_last_array_item.rb b/lib/rubocop/cop/style/hash_as_last_array_item.rb index 985254a8d83..d6eed3a1af6 100644 --- a/lib/rubocop/cop/style/hash_as_last_array_item.rb +++ b/lib/rubocop/cop/style/hash_as_last_array_item.rb @@ -34,6 +34,7 @@ class HashAsLastArrayItem < Base extend AutoCorrector def on_hash(node) + return if node.children.first&.kwsplat_type? return unless (array = containing_array(node)) return unless last_array_item?(array, node) && explicit_array?(array) diff --git a/spec/rubocop/cop/style/hash_as_last_array_item_spec.rb b/spec/rubocop/cop/style/hash_as_last_array_item_spec.rb index 50518a661b1..e370040d11b 100644 --- a/spec/rubocop/cop/style/hash_as_last_array_item_spec.rb +++ b/spec/rubocop/cop/style/hash_as_last_array_item_spec.rb @@ -38,6 +38,12 @@ [1, {}] RUBY end + + it 'does not register an offense when using double splat operator' do + expect_no_offenses(<<~RUBY) + [1, **options] + RUBY + end end context 'when EnforcedStyle is no_braces' do