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

FalseNegative with Performance/RegexpMatch

Open marcandre opened this issue 3 years ago • 4 comments

The following code has 3 anti-patterns:

def self.tokens(pattern)
   pattern.scan(TOKEN).reject { |token| token =~ /\A#{SEPARATORS}\Z/ }
end

This issue is to identify that token =~ regexp. I thought we had a cop suggesting to use match?, but I can't find it...

See: rubocop-hq/rubocop-ast#65

marcandre avatar Jul 13 '20 14:07 marcandre

This https://github.com/rubocop-hq/rubocop-performance/blob/master/lib/rubocop/cop/performance/regexp_match.rb ?

fatkodima avatar Jul 13 '20 18:07 fatkodima

👍

marcandre avatar Jul 13 '20 19:07 marcandre

I have the same issue with the below examples:

# frozen_string_literal: true

def valid_credentials?(response)
  response.code == 302 &&
    [*response.headers['Set-Cookie']]&.any? { |cookie| cookie =~ /wordpress_logged_in_/i }
end

def errored_response?(response)
  response.code != 200 && response.body !~ /login_error/i
end
% rubocop --require 'rubocop-performance' sample.rb
Inspecting 1 file
.

1 file inspected, no offenses detected

erwanlr avatar Jul 14 '20 20:07 erwanlr

cdd@155ed18ffbc6:/tmp/src$ rubocop -c /tmp/empty test.rb 
Inspecting 1 file
C

Offenses:

test.rb:1:1: C: [Correctable] Style/FrozenStringLiteralComment: Missing frozen string literal comment.
    render_missing if (request.path =~ /portal/) && !current_user.public_access?
^
test.rb:1:5: C: [Correctable] Layout/InitialIndentation: Indentation of first line in file detected.
    render_missing if (request.path =~ /portal/) && !current_user.public_access?
    ^^^^^^^^^^^^^^
test.rb:2:1: C: [Correctable] Layout/TrailingEmptyLines: 1 trailing blank lines detected.

1 file inspected, 3 offenses detected, 3 offenses autocorrectable

Plenty of other hits, but RegexpMatch doesn't catch request.path =~ /portal/

kwerle avatar Nov 03 '22 23:11 kwerle