Skip to content

Commit

Permalink
[Fix #222] Fix a false positive for `Performance/RedundantSplitRegexp…
Browse files Browse the repository at this point in the history
…Argument`

Fixes #222.

This PR fixes a false positive for `Performance/RedundantSplitRegexpArgument`
when `split` method argument is exactly one spece regexp `/ /`.
  • Loading branch information
koic committed Mar 20, 2021
1 parent 56b2ebe commit fedc010
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* [#36](https://github.com/rubocop/rubocop-performance/issues/36): Fix a false positive for `Performance/ReverseEach` when `each` is called on `reverse` and using the result value. ([@koic][])
* [#224](https://github.com/rubocop/rubocop-performance/pull/224): Fix a false positive for `Style/RedundantEqualityComparisonBlock` when using one argument with comma separator in block argument. ([@koic][])
* [#225](https://github.com/rubocop/rubocop-performance/issues/225): Fix a false positive for `Style/RedundantEqualityComparisonBlock` when using `any?` with `===` comparison block and block argument is not used as a receiver for `===`. ([@koic][])
* [#222](https://github.com/rubocop/rubocop-performance/issues/222): Fix a false positive for `Performance/RedundantSplitRegexpArgument` when `split` method argument is exactly one spece regexp `/ /`. ([@koic][])

## 1.10.1 (2021-03-02)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class RedundantSplitRegexpArgument < Base

def on_send(node)
return unless (regexp_node = split_call_with_regexp?(node))
return if regexp_node.ignore_case?
return if regexp_node.ignore_case? || regexp_node.content == ' '
return unless determinist_regexp?(regexp_node)

add_offense(regexp_node) do |corrector|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
expect_no_offenses("'fooSplitbar'.split(/split/i)")
end

it 'accepts when `split` method argument is exactly one spece regexp `/ /`' do
expect_no_offenses(<<~RUBY)
'foo bar'.split(/ /)
RUBY
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 Expand Up @@ -82,4 +88,15 @@
'a,b,c'.split(",")
RUBY
end

it 'registers and corrects an offense when `split` method argument is two or more speces regexp `/ /`' do
expect_offense(<<~RUBY)
'foo bar'.split(/ /)
^^^^ Use string as argument instead of regexp.
RUBY

expect_correction(<<~RUBY)
'foo bar'.split(" ")
RUBY
end
end

0 comments on commit fedc010

Please sign in to comment.