From e034945658b081c7fdeabe59c7851eb0b94eb01a Mon Sep 17 00:00:00 2001 From: kshitiz sinha Date: Sun, 8 Nov 2026 23:54:43 +0530 Subject: [PATCH] [Fix #519] Fix false positives for `Performance/StringInclude` when `match` result is used `match` returns `MatchData` or `nil`, so code such as `str.match(/abc/)&.to_s` uses the match itself, not a boolean. Replacing it with `include?` changes the result. Skip `match` when a method is called on its result. --- ...erformance_string_include_with_chained_match.md | 1 + lib/rubocop/cop/performance/string_include.rb | 14 +++++++++++++- .../rubocop/cop/performance/string_include_spec.rb | 8 ++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 changelog/fix_false_positive_for_performance_string_include_with_chained_match.md diff --git a/changelog/fix_false_positive_for_performance_string_include_with_chained_match.md b/changelog/fix_false_positive_for_performance_string_include_with_chained_match.md new file mode 100644 index 000000000..a737b09c6 --- /dev/null +++ b/changelog/fix_false_positive_for_performance_string_include_with_chained_match.md @@ -0,0 +1 @@ +* [#519](https://github.com/rubocop/rubocop-performance/issues/519): Fix false positives for `Performance/StringInclude` when a method is called on the result of `match`. ([@kshtzkr][]) diff --git a/lib/rubocop/cop/performance/string_include.rb b/lib/rubocop/cop/performance/string_include.rb index 475dd2431..9b73f4d01 100644 --- a/lib/rubocop/cop/performance/string_include.rb +++ b/lib/rubocop/cop/performance/string_include.rb @@ -36,8 +36,9 @@ class StringInclude < Base # rubocop:disable-next Metrics/AbcSize def on_send(node) return unless (receiver, regex_str = redundant_regex?(node)) + return if match_result_used?(node) - negation = node.send_type? && node.method?(:!~) + negation = negation?(node) message = format(MSG, negation: ('!' if negation)) add_offense(node, message: message) do |corrector| @@ -55,6 +56,17 @@ def on_send(node) private + # `match` returns `MatchData`, so a call on its result is not a predicate. + def match_result_used?(node) + return false unless node.call_type? && node.method?(:match) + + node.parent&.call_type? && node.parent.receiver == node + end + + def negation?(node) + node.send_type? && node.method?(:!~) + end + def literal?(regex_str) regex_str.match?(/\A#{Util::LITERAL_REGEX}+\z/o) end diff --git a/spec/rubocop/cop/performance/string_include_spec.rb b/spec/rubocop/cop/performance/string_include_spec.rb index 959ef4cc6..662e77a4c 100644 --- a/spec/rubocop/cop/performance/string_include_spec.rb +++ b/spec/rubocop/cop/performance/string_include_spec.rb @@ -174,6 +174,14 @@ expect_no_offenses('expect(subject.spin).to match(/\A\n/)') end + it 'does not register an offense when a method is called on the result of `match`' do + expect_no_offenses(<<~RUBY) + str.match(/abc/).to_s + str.match(/abc/)&.to_s + /abc/.match(str)[0] + RUBY + end + it 'registers an offense and corrects when argument of `match?` is not a string literal' do expect_offense(<<~RUBY) / /.match?(content)