Skip to content

Incorrect autocorrect for Performance/RedundantMatch cop #370

@ymap

Description

@ymap

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions