diff --git a/CHANGELOG.md b/CHANGELOG.md index 794606bb74..144683c5b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/lib/rubocop/cop/performance/redundant_split_regexp_argument.rb b/lib/rubocop/cop/performance/redundant_split_regexp_argument.rb index e6a77a1df4..6113c1e4cb 100644 --- a/lib/rubocop/cop/performance/redundant_split_regexp_argument.rb +++ b/lib/rubocop/cop/performance/redundant_split_regexp_argument.rb @@ -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 == '\\' 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 a9b76e835d..6fc97337d6 100644 --- a/spec/rubocop/cop/performance/redundant_split_regexp_argument_spec.rb +++ b/spec/rubocop/cop/performance/redundant_split_regexp_argument_spec.rb @@ -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(/\\./)