From 09d46fa2dabe9d22d0f2a0f23708e012f232d4bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Thu, 16 Apr 2020 18:02:00 -0400 Subject: [PATCH] Fix `Style/CaseEquality` when `AllowOnConstant` is `true` When the method receiver is implicit it was failing with a NoMethodError in `nil` error. --- CHANGELOG.md | 4 ++++ lib/rubocop/cop/style/case_equality.rb | 2 +- spec/rubocop/cop/style/case_equality_spec.rb | 6 ++++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 309475ebb38..c721e57105e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## master (unreleased) +### Bug fixes + +* [#7882](https://github.com/rubocop-hq/rubocop/pull/7882): Fix `Style/CaseEquality` when `AllowOnConstant` is `true` and the method receiver is implicit. ([@rafaelfranca][]) + ## 0.82.0 (2020-04-16) ### New features diff --git a/lib/rubocop/cop/style/case_equality.rb b/lib/rubocop/cop/style/case_equality.rb index 5069e948eda..e289e6d882d 100644 --- a/lib/rubocop/cop/style/case_equality.rb +++ b/lib/rubocop/cop/style/case_equality.rb @@ -42,7 +42,7 @@ def on_send(node) def const?(node) if cop_config.fetch('AllowOnConstant', false) - !node.const_type? + !node&.const_type? else true end diff --git a/spec/rubocop/cop/style/case_equality_spec.rb b/spec/rubocop/cop/style/case_equality_spec.rb index cd65cdbfe24..e6cfc8e80b5 100644 --- a/spec/rubocop/cop/style/case_equality_spec.rb +++ b/spec/rubocop/cop/style/case_equality_spec.rb @@ -21,6 +21,12 @@ ) end + it 'does not fail when the receiver is implicit' do + expect_no_offenses(<<~RUBY) + puts "No offense" + RUBY + end + it 'does not register an offense for === when the receiver is a constant' do expect_no_offenses(<<~RUBY) Array === var