From d18b3c9445422d992c2369f451fe9634c82594be Mon Sep 17 00:00:00 2001 From: Vlad Pisanov Date: Mon, 2 Sep 2024 21:54:59 -0400 Subject: [PATCH] Fix false negatives in `Style/MapIntoArray` autocorrection when using `ensure`, `def`, `defs` and `for` --- ...style_map_into_array_autocorrect_detection.md | 1 + lib/rubocop/cop/style/map_into_array.rb | 4 +--- spec/rubocop/cop/style/map_into_array_spec.rb | 16 ++++++++++++++++ 3 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 changelog/fix_style_map_into_array_autocorrect_detection.md diff --git a/changelog/fix_style_map_into_array_autocorrect_detection.md b/changelog/fix_style_map_into_array_autocorrect_detection.md new file mode 100644 index 000000000000..a150b43e143c --- /dev/null +++ b/changelog/fix_style_map_into_array_autocorrect_detection.md @@ -0,0 +1 @@ +* [#13185](https://github.com/rubocop/rubocop/pull/13185): Fix false negatives in `Style/MapIntoArray` autocorrection when using `ensure`, `def`, `defs` and `for`. ([@vlad-pisanov][]) diff --git a/lib/rubocop/cop/style/map_into_array.rb b/lib/rubocop/cop/style/map_into_array.rb index e9ec17c21d35..1c5eff8a2bf0 100644 --- a/lib/rubocop/cop/style/map_into_array.rb +++ b/lib/rubocop/cop/style/map_into_array.rb @@ -147,10 +147,8 @@ def return_value_used?(node) false when :begin, :kwbegin !node.right_sibling && return_value_used?(parent) - when :block, :numblock - !parent.void_context? else - true + !parent.respond_to?(:void_context?) || !parent.void_context? end end diff --git a/spec/rubocop/cop/style/map_into_array_spec.rb b/spec/rubocop/cop/style/map_into_array_spec.rb index 1ca49c24334e..6a675084880d 100644 --- a/spec/rubocop/cop/style/map_into_array_spec.rb +++ b/spec/rubocop/cop/style/map_into_array_spec.rb @@ -374,6 +374,10 @@ it_behaves_like 'corrects', template: 'begin; %s end' end end + + context 'in an ensure' do + it_behaves_like 'corrects', template: 'begin; ensure; %s end' + end end context 'in a block' do @@ -404,6 +408,18 @@ context 'at the end' do it_behaves_like 'skip correcting', template: 'def foo; %s end' end + + context 'in a constructor' do + it_behaves_like 'corrects', template: 'def initialize; %s; end' + end + + context 'in an assignment method' do + it_behaves_like 'corrects', template: 'def foo=(value); %s; end' + end + end + + context 'in a for loop' do + it_behaves_like 'corrects', template: 'for i in foo; %s; end' end end end