From f877e02d861cb12d9d57d0c9bc49ac85b9cd616b Mon Sep 17 00:00:00 2001 From: Andrew Szczepanski Date: Mon, 31 Aug 2020 14:01:28 -0400 Subject: [PATCH] Fix `Style/CaseLikeIf` not properly handling overriden equality methods If a class overrides one of `#==`, `#eql?`, or `equal?` and the overridden method has no arguments, then CaseLikeIf#find_target_in_equality_node would error as it assumes there is at least one argument. --- CHANGELOG.md | 2 ++ lib/rubocop/cop/style/case_like_if.rb | 2 +- spec/rubocop/cop/style/case_like_if_spec.rb | 8 ++++++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a4aab64bd0d..f0bd4ea9368 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,7 @@ * [#8321](https://github.com/rubocop-hq/rubocop/issues/8321): Enable auto-correction for `Layout/{Def}EndAlignment`, `Lint/EmptyEnsure`. ([@marcandre][]) * [#8583](https://github.com/rubocop-hq/rubocop/issues/8583): Fix `Style/RedundantRegexpEscape` false positive for line continuations. ([@owst][]) * [#8593](https://github.com/rubocop-hq/rubocop/issues/8593): Fix `Style/RedundantRegexpCharacterClass` false positive for interpolated multi-line expressions. ([@owst][]) +* [#8624](https://github.com/rubocop-hq/rubocop/pull/8624): Fix an error with the `Style/CaseLikeIf` cop where it does not properly handle overridden equality methods with no arguments. ([@Skipants][]) ### Changes @@ -4821,3 +4822,4 @@ [@chocolateboy]: https://github.com/chocolateboy [@Lykos]: https://github.com/Lykos [@jaimerave]: https://github.com/jaimerave +[@Skipants]: https://github.com/Skipants diff --git a/lib/rubocop/cop/style/case_like_if.rb b/lib/rubocop/cop/style/case_like_if.rb index 16570cce578..d454c62fe10 100644 --- a/lib/rubocop/cop/style/case_like_if.rb +++ b/lib/rubocop/cop/style/case_like_if.rb @@ -116,7 +116,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 + return unless argument && receiver if argument.literal? || const_reference?(argument) receiver diff --git a/spec/rubocop/cop/style/case_like_if_spec.rb b/spec/rubocop/cop/style/case_like_if_spec.rb index 05025c8d467..00c2a23e0ee 100644 --- a/spec/rubocop/cop/style/case_like_if_spec.rb +++ b/spec/rubocop/cop/style/case_like_if_spec.rb @@ -336,4 +336,12 @@ foo if x == 1 RUBY end + + it 'does not register an offense when an object overrides `equal?` with no arity' do + expect_no_offenses(<<~RUBY) + if x.equal? + elsif y + end + RUBY + end end