Skip to content

Commit 12339c5

Browse files
committed
[Fix rubocop#216] Fix a false positive for Performance/RedundantSplitRegexpArgument
Fixes rubocop#216. This PR fixes a false positive for `Performance/RedundantSplitRegexpArgument` when using split method with ignore case regexp option.
1 parent f35ffd0 commit 12339c5

File tree

3 files changed

+17
-15
lines changed

3 files changed

+17
-15
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Bug fixes
66

77
* [#214](https://github.com/rubocop/rubocop-performance/issues/214): Fix a false positive for `Performance/RedundantEqualityComparisonBlock` when using multiple block arguments. ([@koic][])
8+
* [#216](https://github.com/rubocop/rubocop-performance/issues/216): Fix a false positive for `Performance/RedundantSplitRegexpArgument` when using split method with ignore case regexp option. ([@koic][])
89

910
## 1.10.0 (2021-03-01)
1011

lib/rubocop/cop/performance/redundant_split_regexp_argument.rb

+12-15
Original file line numberDiff line numberDiff line change
@@ -21,32 +21,29 @@ class RedundantSplitRegexpArgument < Base
2121
STR_SPECIAL_CHARS = %w[\n \" \' \\\\ \t \b \f \r].freeze
2222

2323
def_node_matcher :split_call_with_regexp?, <<~PATTERN
24-
{(send !nil? :split {regexp})}
24+
{(send !nil? :split $regexp)}
2525
PATTERN
2626

2727
def on_send(node)
28-
return unless split_call_with_regexp?(node)
29-
return unless determinist_regexp?(node.first_argument)
28+
return unless (regexp_node = split_call_with_regexp?(node))
29+
return if regexp_node.ignore_case?
30+
return unless determinist_regexp?(regexp_node)
3031

31-
add_offense(node.first_argument) do |corrector|
32-
autocorrect(corrector, node)
32+
add_offense(regexp_node) do |corrector|
33+
new_argument = replacement(regexp_node)
34+
35+
corrector.replace(regexp_node, "\"#{new_argument}\"")
3336
end
3437
end
3538

3639
private
3740

38-
def determinist_regexp?(first_argument)
39-
DETERMINISTIC_REGEX.match?(first_argument.source)
40-
end
41-
42-
def autocorrect(corrector, node)
43-
new_argument = replacement(node)
44-
45-
corrector.replace(node.first_argument, "\"#{new_argument}\"")
41+
def determinist_regexp?(regexp_node)
42+
DETERMINISTIC_REGEX.match?(regexp_node.source)
4643
end
4744

48-
def replacement(node)
49-
regexp_content = node.first_argument.content
45+
def replacement(regexp_node)
46+
regexp_content = regexp_node.content
5047
stack = []
5148
chars = regexp_content.chars.each_with_object([]) do |char, strings|
5249
if stack.empty? && char == '\\'

spec/rubocop/cop/performance/redundant_split_regexp_argument_spec.rb

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
expect_no_offenses("'a,b,c'.split(/,+/)")
1414
end
1515

16+
it 'accepts when using split method with ignorecase regexp option' do
17+
expect_no_offenses("'fooSplitbar'.split(/split/i)")
18+
end
19+
1620
it 'registers an offense when the method is split and correctly removes escaping characters' do
1721
expect_offense(<<~RUBY)
1822
'a,b,c'.split(/\\./)

0 commit comments

Comments
 (0)