Skip to content

Commit 3c10218

Browse files
committed
Fix RSpec/MatchWithSimpleRegex to ignore match nested inside include matchers
1 parent e70e732 commit 3c10218

3 files changed

Lines changed: 18 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- Fix false positives for `RSpec/SpecFilePathFormat` when matching spec partials. ([@ydah])
66
- Fix incorrect autocorrection for `RSpec/DescribedClass` when using nested example groups with `EnforcedStyle: explicit`. ([@ydah])
7+
- Fix `RSpec/MatchWithSimpleRegex` to ignore `match` nested inside `include` matchers. ([@ydah])
78

89
## 3.10.2 (2026-06-06)
910

lib/rubocop/cop/rspec/match_with_simple_regex.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ class MatchWithSimpleRegex < Base
3939
PATTERN
4040

4141
def on_send(node)
42+
return if include_matcher_argument?(node)
43+
4244
match_with_regexp?(node) do |regexp|
4345
next unless simple_regexp?(regexp)
4446

@@ -53,6 +55,13 @@ def on_send(node)
5355

5456
private
5557

58+
def include_matcher_argument?(node)
59+
parent = node.parent
60+
61+
parent&.send_type? && parent.receiver.nil? &&
62+
parent.method?(:include) && parent.arguments.include?(node)
63+
end
64+
5665
def simple_regexp?(node)
5766
return false if node.interpolation?
5867
return false if node.regopt.children.any?

spec/rubocop/cop/rspec/match_with_simple_regex_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@
4848
RUBY
4949
end
5050

51+
it 'does not register an offense when using match within an ' \
52+
'include matcher' do
53+
expect_no_offenses(<<~RUBY)
54+
expect(errors).to include(match(/message/))
55+
expect(errors).to include(match(/message/), match(/warning/))
56+
RUBY
57+
end
58+
5159
it 'does not register an offense when using match with anchor at start' do
5260
expect_no_offenses(<<~RUBY)
5361
expect('foobar').to match(/^foo/)

0 commit comments

Comments
 (0)