rubocop-performance
rubocop-performance copied to clipboard
FalseNegative with Performance/RegexpMatch
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
This https://github.com/rubocop-hq/rubocop-performance/blob/master/lib/rubocop/cop/performance/regexp_match.rb ?
👍
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
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/