Skip to content

Performance/RegexpMatch: Wording pitfall will negate code expression #510

@forthrin

Description

@forthrin

Steps to reproduce the problem

puts 1 if foo !~ /fo./

Actual behavior

Use match? instead of !~ when MatchData is not used. [[Correctable] Performance/RegexpMatch]

Pitfall: Changing the code according to the exact wording will actually negate the expression.

Expected behavior

A correct rewording would be something like:

  • Use !match? instead of !~ when ...
  • Use match? instead of !~ and negate the expression when ...
  • Use match? instead of !~ and replace if with unless when ...

RuboCop version

rubocop-performance (1.25.0)
ruby      2025-08-01 30a20bc16
rubocop   2025-08-11 c595cf6e2
System Version: macOS 15.6 (24G84)
Model Identifier: MacBookAir10,1

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions