Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent an incorrect auto-correction for Style/CaseEquality cop #8527

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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

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