Skip to content

Commit

Permalink
[Fix #11173] Fix an incorrect autocorrect for Style/CollectionCompact
Browse files Browse the repository at this point in the history
Fixes #11173.

This PR fixes an incorrect autocorrect for `Style/CollectionCompact`
when using `reject` with block pass arg and no parentheses.
  • Loading branch information
koic authored and bbatsov committed Nov 12, 2022
1 parent dfb1ca5 commit 7b265aa
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
@@ -0,0 +1 @@
* [#11173](https://github.com/rubocop/rubocop/issues/11173): Fix an incorrect autocorrect for `Style/CollectionCompact` when using `reject` with block pass arg and no parentheses. ([@koic][])
2 changes: 1 addition & 1 deletion lib/rubocop/cop/style/collection_compact.rb
Expand Up @@ -112,7 +112,7 @@ def good_method_name(node)
end

def range(begin_pos_node, end_pos_node)
range_between(begin_pos_node.loc.selector.begin_pos, end_pos_node.loc.end.end_pos)
range_between(begin_pos_node.loc.selector.begin_pos, end_pos_node.loc.expression.end_pos)
end
end
end
Expand Down
11 changes: 11 additions & 0 deletions spec/rubocop/cop/style/collection_compact_spec.rb
Expand Up @@ -29,6 +29,17 @@
RUBY
end

it 'registers an offense and corrects when using `reject` with block pass arg and no parentheses' do
expect_offense(<<~RUBY)
array.reject &:nil?
^^^^^^^^^^^^^ Use `compact` instead of `reject &:nil?`.
RUBY

expect_correction(<<~RUBY)
array.compact
RUBY
end

it 'registers an offense and corrects when using `reject` on hash to reject nils' do
expect_offense(<<~RUBY)
hash.reject { |k, v| v.nil? }
Expand Down

0 comments on commit 7b265aa

Please sign in to comment.