Skip to content

Commit

Permalink
Merge pull request rubocop#8345 from fatkodima/fix-case-like-if-8344
Browse files Browse the repository at this point in the history
Fix crash for `Style/CaseLikeIf` when checking against `equal?` and `match?` without a receiver
  • Loading branch information
koic committed Jul 15, 2020
2 parents ee123f4 + d608a11 commit a45042b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,7 @@
### Bug fixes

* [#8324](https://github.com/rubocop-hq/rubocop/issues/8324): Fix crash for `Layout/SpaceAroundMethodCallOperator` when using `Proc#call` shorthand syntax. ([@fatkodima][])
* [#8344](https://github.com/rubocop-hq/rubocop/issues/8344): Fix crash for `Style/CaseLikeIf` when checking against `equal?` and `match?` without a receiver. ([@fatkodima][])
* [#8323](https://github.com/rubocop-hq/rubocop/issues/8323): Fix a false positive for `Style/HashAsLastArrayItem` when hash is not a last array item. ([@fatkodima][])
* [#8299](https://github.com/rubocop-hq/rubocop/issues/8299): Fix an incorrect auto-correct for `Style/RedundantCondition` when using `raise`, `rescue`, or `and` without argument parentheses in `else`. ([@koic][])
* [#8335](https://github.com/rubocop-hq/rubocop/issues/8335): Fix incorrect character class detection for nested or POSIX bracket character classes in `Style/RedundantRegexpEscape`. ([@owst][])
Expand Down
2 changes: 2 additions & 0 deletions lib/rubocop/cop/style/case_like_if.rb
Expand Up @@ -112,6 +112,7 @@ def find_target_in_send_node(node)
def find_target_in_equality_node(node)
argument = node.arguments.first
receiver = node.receiver
return unless receiver

if argument.literal? || const_reference?(argument)
receiver
Expand All @@ -123,6 +124,7 @@ def find_target_in_equality_node(node)
def find_target_in_match_node(node)
argument = node.arguments.first
receiver = node.receiver
return unless receiver

if receiver.regexp_type?
argument
Expand Down
18 changes: 18 additions & 0 deletions spec/rubocop/cop/style/case_like_if_spec.rb
Expand Up @@ -86,6 +86,15 @@
RUBY
end

it 'does not register an offense when using `equal?` without a receiver' do
expect_no_offenses(<<~RUBY)
if equal?(Foo)
elsif Bar == x
else
end
RUBY
end

it 'registers an offense and corrects when using `is_a?`' do
expect_offense(<<~RUBY)
if x.is_a?(Foo)
Expand Down Expand Up @@ -131,6 +140,15 @@
RUBY
end

it 'does not register an offense when using `match?` without a receiver' do
expect_no_offenses(<<~RUBY)
if match?(/foo/)
elsif x.match?(/bar/)
else
end
RUBY
end

it 'registers an offense and corrects when using `=~`' do
expect_offense(<<~RUBY)
if /foo/ =~ x
Expand Down

0 comments on commit a45042b

Please sign in to comment.