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

Conversation

koic
Copy link
Member

@koic koic commented Aug 12, 2020

Follow #8322 (comment)

This PR fixes an incorrect auto-correction for Style/CaseEquality cop when comparing with === against a regular expression receiver.

This PR auto-correct === with match? to keep compatibility of returning a boolean value.


Before submitting the PR make sure the following are checked:

  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Run bundle exec rake default. It executes all tests and RuboCop for itself, and generates the documentation.

@@ -58,7 +58,7 @@ def const?(node)
def replacement(lhs, rhs)
case lhs.type
when :regexp
"#{rhs.source} =~ #{lhs.source}"
"#{lhs.source}.match?(#{rhs.source})"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before doing this, you need to check if the subsequent code does not refer to the match data (Regexp.last_match, $~, $1, etc.).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure! I have a idea. Performance/Regexp cop detects Regexp.last_match, and etc. It is expected that a === b will be auto-corrected to a.match?(b) in future.
Reelated issue: rubocop/rubocop-performance#152

It currently doesn't support unsafe auto-correction, so it may be better to delegate it to Perfomance/Regexp cop.

@koic koic changed the title Fix an incorrect auto-correction for Style/CaseEquality cop Prevent an incorrect auto-correction for Style/CaseEquality cop Aug 12, 2020
@koic koic force-pushed the fix_incorrect_autocorrect_for_style_case_equality branch from 8edcd50 to 9ef6bb7 Compare August 12, 2020 14:29
Follow rubocop#8322 (comment)

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: rubocop/rubocop-performance#152
@koic koic force-pushed the fix_incorrect_autocorrect_for_style_case_equality branch from 9ef6bb7 to d771985 Compare August 17, 2020 07:14
@koic koic merged commit a2c6a94 into rubocop:master Aug 17, 2020
@koic koic deleted the fix_incorrect_autocorrect_for_style_case_equality branch August 17, 2020 08:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants