Skip to content

Commit

Permalink
Merge pull request #8527 from koic/fix_incorrect_autocorrect_for_styl…
Browse files Browse the repository at this point in the history
…e_case_equality

Prevent an incorrect auto-correction for `Style/CaseEquality` cop
  • Loading branch information
koic committed Aug 17, 2020
2 parents 2845f8d + d771985 commit a2c6a94
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -14,6 +14,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

Expand Down
4 changes: 2 additions & 2 deletions docs/modules/ROOT/pages/cops_style.adoc
Expand Up @@ -825,7 +825,7 @@ Array === something
# good
something.is_a?(Array)
(1..100).include?(7)
some_string =~ /something/
/something/.match?(some_string)
----

==== AllowOnConstant
Expand All @@ -842,7 +842,7 @@ some_string =~ /something/
# good
Array === something
(1..100).include?(7)
some_string =~ /something/
/something/.match?(some_string)
----

=== Configurable attributes
Expand Down
11 changes: 8 additions & 3 deletions lib/rubocop/cop/style/case_equality.rb
Expand Up @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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?
Expand Down
5 changes: 2 additions & 3 deletions spec/rubocop/cop/style/case_equality_spec.rb
Expand Up @@ -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
Expand Down

0 comments on commit a2c6a94

Please sign in to comment.