Skip to content

Commit

Permalink
[Fix rubocop#216] Fix a false positive for `Performance/RedundantSpli…
Browse files Browse the repository at this point in the history
…tRegexpArgument`

Fixes rubocop#216.

This PR fixes a false positive for `Performance/RedundantSplitRegexpArgument`
when using split method with ignore case regexp option.
  • Loading branch information
koic committed Mar 1, 2021
1 parent f35ffd0 commit 12339c5
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Bug fixes

* [#214](https://github.com/rubocop/rubocop-performance/issues/214): Fix a false positive for `Performance/RedundantEqualityComparisonBlock` when using multiple block arguments. ([@koic][])
* [#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][])

## 1.10.0 (2021-03-01)

Expand Down
27 changes: 12 additions & 15 deletions lib/rubocop/cop/performance/redundant_split_regexp_argument.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,29 @@ class RedundantSplitRegexpArgument < Base
STR_SPECIAL_CHARS = %w[\n \" \' \\\\ \t \b \f \r].freeze

def_node_matcher :split_call_with_regexp?, <<~PATTERN
{(send !nil? :split {regexp})}
{(send !nil? :split $regexp)}
PATTERN

def on_send(node)
return unless split_call_with_regexp?(node)
return unless determinist_regexp?(node.first_argument)
return unless (regexp_node = split_call_with_regexp?(node))
return if regexp_node.ignore_case?
return unless determinist_regexp?(regexp_node)

add_offense(node.first_argument) do |corrector|
autocorrect(corrector, node)
add_offense(regexp_node) do |corrector|
new_argument = replacement(regexp_node)

corrector.replace(regexp_node, "\"#{new_argument}\"")
end
end

private

def determinist_regexp?(first_argument)
DETERMINISTIC_REGEX.match?(first_argument.source)
end

def autocorrect(corrector, node)
new_argument = replacement(node)

corrector.replace(node.first_argument, "\"#{new_argument}\"")
def determinist_regexp?(regexp_node)
DETERMINISTIC_REGEX.match?(regexp_node.source)
end

def replacement(node)
regexp_content = node.first_argument.content
def replacement(regexp_node)
regexp_content = regexp_node.content
stack = []
chars = regexp_content.chars.each_with_object([]) do |char, strings|
if stack.empty? && char == '\\'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
expect_no_offenses("'a,b,c'.split(/,+/)")
end

it 'accepts when using split method with ignorecase regexp option' do
expect_no_offenses("'fooSplitbar'.split(/split/i)")
end

it 'registers an offense when the method is split and correctly removes escaping characters' do
expect_offense(<<~RUBY)
'a,b,c'.split(/\\./)
Expand Down

0 comments on commit 12339c5

Please sign in to comment.