From d608a11e83c164d26f59b97debf026101e324c24 Mon Sep 17 00:00:00 2001 From: fatkodima Date: Wed, 15 Jul 2020 14:16:09 +0300 Subject: [PATCH] Fix crash for `Style/CaseLikeIf` when checking against `equal?` and `match?` without a receiver --- CHANGELOG.md | 1 + lib/rubocop/cop/style/case_like_if.rb | 2 ++ spec/rubocop/cop/style/case_like_if_spec.rb | 18 ++++++++++++++++++ 3 files changed, 21 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ee16d441fda..098044809fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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][]) diff --git a/lib/rubocop/cop/style/case_like_if.rb b/lib/rubocop/cop/style/case_like_if.rb index d5e31f18b17..6e35350a497 100644 --- a/lib/rubocop/cop/style/case_like_if.rb +++ b/lib/rubocop/cop/style/case_like_if.rb @@ -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 @@ -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 diff --git a/spec/rubocop/cop/style/case_like_if_spec.rb b/spec/rubocop/cop/style/case_like_if_spec.rb index 89a75c1bd2e..18ce2f19e5f 100644 --- a/spec/rubocop/cop/style/case_like_if_spec.rb +++ b/spec/rubocop/cop/style/case_like_if_spec.rb @@ -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) @@ -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