Skip to content

Commit

Permalink
Merge pull request rubocop#13185 from vlad-pisanov/vp_map_into_array_2
Browse files Browse the repository at this point in the history
Fix false negatives in `Style/MapIntoArray` autocorrection
  • Loading branch information
koic committed Sep 3, 2024
2 parents 3277118 + d18b3c9 commit 5e65a39
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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][])
4 changes: 1 addition & 3 deletions lib/rubocop/cop/style/map_into_array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 16 additions & 0 deletions spec/rubocop/cop/style/map_into_array_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit 5e65a39

Please sign in to comment.