rubocop-performance icon indicating copy to clipboard operation
rubocop-performance copied to clipboard

Performance/StringInclude doesn't consider assignment context

Open forthrin opened this issue 4 months ago • 1 comments

Steps to reproduce the problem

items = %w[A B]
one = items.map { it.match(/A/)&.to_s }
# Use `String#include?` instead of a regex match with literal-only pattern. [[Correctable] Performance/StringInclude]
two = items.map { it.include?('A') } # "Whatever you say" -- Bugs Bunny
# Much, much later ...
pp [one, two] # [["A", nil], [true, false]]

Expected behavior

We're assigning the result to a variable in order to do unmentionable stuff with the match later if it was found.

Actual behavior

Cop doesn't see the broader picture and writes us a ticket thinking we just want to test for existence.

RuboCop version

ruby      2025-08-01 30a20bc16
rubocop   2025-10-04 a08091813
System Version: macOS 15.6 (24G84)
Model Identifier: MacBookAir10,1

forthrin avatar Oct 08 '25 07:10 forthrin

@rrosenblum: Your opinion on this one?

forthrin avatar Dec 09 '25 14:12 forthrin

Performance comes secondary to working code. This seems like an issue where it isn't safe to flag code with assignment or method chaining

rrosenblum avatar Dec 14 '25 20:12 rrosenblum