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

Incorrect autocorrect for Performance/RedundantMatch cop #370

Closed
ymap opened this issue Sep 14, 2023 · 0 comments · Fixed by #371
Closed

Incorrect autocorrect for Performance/RedundantMatch cop #370

ymap opened this issue Sep 14, 2023 · 0 comments · Fixed by #371

Comments

@ymap
Copy link
Contributor

ymap commented Sep 14, 2023

I've noticed that the cop's autocorrection is incorrect when using operators with lower precedence than =~ as an argument to match.

Here's the reproducible code:

arg = nil
puts "pass" if /regex/.match(arg || "default")

Expected behavior

To preserve the original behavior, parentheses should be added around the RHS.

arg = nil
puts "pass" if /regex/ =~ (arg || "default")

This code produces no output.

Actual behavior

Parentheses are missing, which results in the condition being evaluated as (/regex/ =~ arg) || "default".

arg = nil
puts "pass" if /regex/ =~ arg || "default"

This code outputs "pass".

Steps to reproduce the problem

  1. Save the provided code snippet into a file named example.rb.
  2. Execute bundle exec rubocop --only Performance/RedundantMatch -a example.rb.

RuboCop version

$ bundle exec rubocop -V
1.56.1 (using Parser 3.2.2.3, rubocop-ast 1.29.0, running on ruby 3.0.2) [
x86_64-linux]
  - rubocop-performance 1.19.0
  - rubocop-rspec 2.22.0
ymap added a commit to ymap/rubocop-performance that referenced this issue Sep 14, 2023
…dantMatch`

Fixes rubocop#370.

This PR fixes an incorrect autocorrect for `Performance/RedundantMatch`
when expressions with lower precedence than `=~` are used as an argument.
ymap added a commit to ymap/rubocop-performance that referenced this issue Sep 15, 2023
…dantMatch`

Fixes rubocop#370.

This PR fixes an incorrect autocorrect for `Performance/RedundantMatch`
when expressions with lower precedence than `=~` are used as an argument.
ymap added a commit to ymap/rubocop-performance that referenced this issue Sep 15, 2023
…dantMatch`

Fixes rubocop#370.

This PR fixes an incorrect autocorrect for `Performance/RedundantMatch`
when expressions with lower precedence than `=~` are used as an argument.
@koic koic closed this as completed in #371 Sep 15, 2023
koic added a commit that referenced this issue Sep 15, 2023
…rformance_redundant_match

[Fix #370] Fix an incorrect autocorrect for `Performance/RedundantMatch`
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 a pull request may close this issue.

1 participant