From fedc01062272c1212444ac0aae6fa989e6a77191 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Sat, 13 Mar 2021 16:22:09 +0900 Subject: [PATCH] [Fix #222] Fix a false positive for `Performance/RedundantSplitRegexpArgument` Fixes #222. This PR fixes a false positive for `Performance/RedundantSplitRegexpArgument` when `split` method argument is exactly one spece regexp `/ /`. --- CHANGELOG.md | 1 + .../redundant_split_regexp_argument.rb | 2 +- .../redundant_split_regexp_argument_spec.rb | 17 +++++++++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5b9ea7cb97..306e39787d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/lib/rubocop/cop/performance/redundant_split_regexp_argument.rb b/lib/rubocop/cop/performance/redundant_split_regexp_argument.rb index 6113c1e4cb..57ef30e556 100644 --- a/lib/rubocop/cop/performance/redundant_split_regexp_argument.rb +++ b/lib/rubocop/cop/performance/redundant_split_regexp_argument.rb @@ -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| diff --git a/spec/rubocop/cop/performance/redundant_split_regexp_argument_spec.rb b/spec/rubocop/cop/performance/redundant_split_regexp_argument_spec.rb index 6fc97337d6..3f1ba38415 100644 --- a/spec/rubocop/cop/performance/redundant_split_regexp_argument_spec.rb +++ b/spec/rubocop/cop/performance/redundant_split_regexp_argument_spec.rb @@ -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(/\\./) @@ -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