From d771985cb24bc7b0fc204f8b375e12a9f6607c33 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Wed, 12 Aug 2020 20:05:52 +0900 Subject: [PATCH] Prevent an incorrect auto-correction for `Style/CaseEquality` cop Follow https://github.com/rubocop-hq/rubocop/pull/8322#discussion_r468672970 This PR prevents an incorrect auto-correction for `Style/CaseEquality` cop when comparing with `===` against a regular expression receiver. OTOH, the automatic correction from `a === b` to `a.match?(b)` needs to consider `Regexp.last_match?`, `$~`, `$1`, and etc. This correction is expected to be supported by `Performance/Regexp` cop. See: https://github.com/rubocop-hq/rubocop-performance/issues/152 --- CHANGELOG.md | 1 + docs/modules/ROOT/pages/cops_style.adoc | 4 ++-- lib/rubocop/cop/style/case_equality.rb | 11 ++++++++--- spec/rubocop/cop/style/case_equality_spec.rb | 5 ++--- 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index af71e100979..0ae96f92158 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ * [#8534](https://github.com/rubocop-hq/rubocop/issues/8534): Fix `Lint/BinaryOperatorWithIdenticalOperands` for binary operators used as unary operators. ([@marcandre][]) * [#8537](https://github.com/rubocop-hq/rubocop/pull/8537): Allow a trailing comment as a description comment for `Bundler/GemComment`. ([@pocke][]) * [#8507](https://github.com/rubocop-hq/rubocop/issues/8507): Fix `Style/RescueModifier` to handle parentheses around rescue modifiers. ([@dsavochkin][]) +* [#8527](https://github.com/rubocop-hq/rubocop/pull/8527): Prevent an incorrect auto-correction for `Style/CaseEquality` cop when comparing with `===` against a regular expression receiver. ([@koic][]) ### Changes diff --git a/docs/modules/ROOT/pages/cops_style.adoc b/docs/modules/ROOT/pages/cops_style.adoc index 38dee96c9da..4956268110f 100644 --- a/docs/modules/ROOT/pages/cops_style.adoc +++ b/docs/modules/ROOT/pages/cops_style.adoc @@ -825,7 +825,7 @@ Array === something # good something.is_a?(Array) (1..100).include?(7) -some_string =~ /something/ +/something/.match?(some_string) ---- ==== AllowOnConstant @@ -842,7 +842,7 @@ some_string =~ /something/ # good Array === something (1..100).include?(7) -some_string =~ /something/ +/something/.match?(some_string) ---- === Configurable attributes diff --git a/lib/rubocop/cop/style/case_equality.rb b/lib/rubocop/cop/style/case_equality.rb index a7492a5d92b..2a9c8e85287 100644 --- a/lib/rubocop/cop/style/case_equality.rb +++ b/lib/rubocop/cop/style/case_equality.rb @@ -14,7 +14,7 @@ module Style # # good # something.is_a?(Array) # (1..100).include?(7) - # some_string =~ /something/ + # /something/.match?(some_string) # # @example AllowOnConstant # # Style/CaseEquality: @@ -27,7 +27,7 @@ module Style # # good # Array === something # (1..100).include?(7) - # some_string =~ /something/ + # /something/.match?(some_string) # class CaseEquality < Base extend AutoCorrector @@ -58,7 +58,12 @@ def const?(node) def replacement(lhs, rhs) case lhs.type when :regexp - "#{rhs.source} =~ #{lhs.source}" + # The automatic correction from `a === b` to `a.match?(b)` needs to + # consider `Regexp.last_match?`, `$~`, `$1`, and etc. + # This correction is expected to be supported by `Performance/Regexp` cop. + # See: https://github.com/rubocop-hq/rubocop-performance/issues/152 + # + # So here is noop. when :begin child = lhs.children.first "#{lhs.source}.include?(#{rhs.source})" if child&.range_type? diff --git a/spec/rubocop/cop/style/case_equality_spec.rb b/spec/rubocop/cop/style/case_equality_spec.rb index 148b794be85..09766aafaa5 100644 --- a/spec/rubocop/cop/style/case_equality_spec.rb +++ b/spec/rubocop/cop/style/case_equality_spec.rb @@ -14,9 +14,8 @@ ^^^ Avoid the use of the case equality operator `===`. RUBY - expect_correction(<<~RUBY) - var =~ /OMG/ - RUBY + # This correction is expected to be supported by `Performance/Regexp`. + expect_no_corrections end it 'registers an offense and corrects for === when the receiver is a range' do