From 252fe51fb6a144747187bc2078c7f039dd523c8f Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Wed, 12 Aug 2020 19:37:22 +0900 Subject: [PATCH] Fix a false positive for `Style/CaseEquality` cop Follow https://github.com/rubocop-hq/rubocop/pull/8322#discussion_r468443896 Fix a false positive for `Style/CaseEquality` cop when the receiver is not a camel cased constant. The following constant should not be replaced with `is_a?`. What kind of object is actually assigned depends on the constant. e.g.: ```ruby REGEXP_CONSTANT === something #=> does not register an offense. ``` This PR will only consider camel-cased constant as class name. e.g.: ```ruby Array === something #=> auto-correct to `something.is_a?(Array)` ``` --- CHANGELOG.md | 1 + lib/rubocop/cop/style/case_equality.rb | 2 ++ rubocop.gemspec | 2 +- spec/rubocop/cop/style/case_equality_spec.rb | 6 ++++++ 4 files changed, 10 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e16628a2302..313afc9b9ce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,7 @@ * [#8698](https://github.com/rubocop-hq/rubocop/pull/8698): Fix cache to avoid encoding exception. ([@marcandre][]) * [#8704](https://github.com/rubocop-hq/rubocop/issues/8704): Fix an error for `Lint/AmbiguousOperator` when using safe navigation operator with a unary operator. ([@koic][]) * [#8661](https://github.com/rubocop-hq/rubocop/pull/8661): Fix an incorrect auto-correct for `Style/MultilineTernaryOperator` when returning a multiline ternary operator expression. ([@koic][]) +* [#8526](https://github.com/rubocop-hq/rubocop/pull/8526): Fix a false positive for `Style/CaseEquality` cop when the receiver is not a camel cased constant. ([@koic][]) ### Changes diff --git a/lib/rubocop/cop/style/case_equality.rb b/lib/rubocop/cop/style/case_equality.rb index 8188ff2edc5..39115d423f5 100644 --- a/lib/rubocop/cop/style/case_equality.rb +++ b/lib/rubocop/cop/style/case_equality.rb @@ -39,6 +39,8 @@ class CaseEquality < Base def on_send(node) case_equality?(node) do |lhs, rhs| + return if lhs.const_type? && !lhs.module_name? + add_offense(node.loc.selector) do |corrector| replacement = replacement(lhs, rhs) corrector.replace(node, replacement) if replacement diff --git a/rubocop.gemspec b/rubocop.gemspec index 97c70e7e3bc..6e90c7aff7e 100644 --- a/rubocop.gemspec +++ b/rubocop.gemspec @@ -38,7 +38,7 @@ Gem::Specification.new do |s| s.add_runtime_dependency('rainbow', '>= 2.2.2', '< 4.0') s.add_runtime_dependency('regexp_parser', '>= 1.7') s.add_runtime_dependency('rexml') - s.add_runtime_dependency('rubocop-ast', '>= 0.3.0', '< 1.0') + s.add_runtime_dependency('rubocop-ast', '>= 0.4.0', '< 1.0') s.add_runtime_dependency('ruby-progressbar', '~> 1.7') s.add_runtime_dependency('unicode-display_width', '>= 1.4.0', '< 2.0') diff --git a/spec/rubocop/cop/style/case_equality_spec.rb b/spec/rubocop/cop/style/case_equality_spec.rb index 09766aafaa5..4ee8c09d4dd 100644 --- a/spec/rubocop/cop/style/case_equality_spec.rb +++ b/spec/rubocop/cop/style/case_equality_spec.rb @@ -8,6 +8,12 @@ RUBY end + it 'does not register an offense for === when the receiver is not a camel cased constant' do + expect_no_offenses(<<~RUBY) + REGEXP_CONSTANT === var + RUBY + end + it 'registers an offense and corrects for === when the receiver is a regexp' do expect_offense(<<~RUBY) /OMG/ === var