File tree Expand file tree Collapse file tree 3 files changed +15
-14
lines changed
lib/rubocop/cop/performance
spec/rubocop/cop/performance Expand file tree Collapse file tree 3 files changed +15
-14
lines changed Original file line number Diff line number Diff line change
1
+ * [ #332 ] ( https://github.com/rubocop/rubocop-performance/pull/332 ) : Register offenses for variables against regexes in ` Performance/StringInclude ` . ([ @fatkodima ] [ ] )
Original file line number Diff line number Diff line change @@ -6,19 +6,19 @@ module Performance
6
6
# Identifies unnecessary use of a regex where `String#include?` would suffice.
7
7
#
8
8
# @safety
9
- # This cop's offenses are not safe to autocorrect if a receiver is nil.
9
+ # This cop's offenses are not safe to autocorrect if a receiver is nil or a Symbol .
10
10
#
11
11
# @example
12
12
# # bad
13
- # 'abc' .match?(/ab/)
14
- # /ab/.match?('abc' )
15
- # 'abc' =~ /ab/
16
- # /ab/ =~ 'abc'
17
- # 'abc' .match(/ab/)
18
- # /ab/.match('abc' )
13
+ # str .match?(/ab/)
14
+ # /ab/.match?(str )
15
+ # str =~ /ab/
16
+ # /ab/ =~ str
17
+ # str .match(/ab/)
18
+ # /ab/.match(str )
19
19
#
20
20
# # good
21
- # 'abc' .include?('ab')
21
+ # str .include?('ab')
22
22
class StringInclude < Base
23
23
extend AutoCorrector
24
24
@@ -27,7 +27,7 @@ class StringInclude < Base
27
27
28
28
def_node_matcher :redundant_regex? , <<~PATTERN
29
29
{(send $!nil? {:match :=~ :!~ :match?} (regexp (str $#literal?) (regopt)))
30
- (send (regexp (str $#literal?) (regopt)) {:match :match?} $str )
30
+ (send (regexp (str $#literal?) (regopt)) {:match :match?} $_ )
31
31
(match-with-lvasgn (regexp (str $#literal?) (regopt)) $_)}
32
32
PATTERN
33
33
Original file line number Diff line number Diff line change 151
151
expect_no_offenses ( 'expect(subject.spin).to match(/\A\n/)' )
152
152
end
153
153
154
- # Symbol object does not have `include ?` method.
155
- # A variable possible to be a symbol object, so if `match?` argument is
156
- # a variable, accept it.
157
- it 'allows argument of `match ?` is not a string literal' do
158
- expect_no_offenses ( '/ /.match?(content_as_symbol)' )
154
+ it 'registers an offense and corrects when argument of `match ?` is not a string literal' do
155
+ expect_offense ( <<~RUBY )
156
+ / /.match?(content)
157
+ ^^^^^^^^^^^^^^^^^^^ Use `String#include ?` instead of a regex match with literal-only pattern.
158
+ RUBY
159
159
end
160
160
161
161
it 'registers an offense and corrects when using `!~`' do
You can’t perform that action at this time.
0 commit comments