Skip to content

Commit ae8f7a8

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

3 files changed

Lines changed: 28 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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,14 @@ class MatchWithSimpleRegex < Base
3838
(send nil? :match $regexp)
3939
PATTERN
4040

41+
# @!method include_matcher_argument?(node)
42+
def_node_matcher :include_matcher_argument?, <<~PATTERN
43+
^(send nil? :include ...)
44+
PATTERN
45+
4146
def on_send(node)
47+
return if include_matcher_argument?(node)
48+
4249
match_with_regexp?(node) do |regexp|
4350
next unless simple_regexp?(regexp)
4451

spec/rubocop/cop/rspec/match_with_simple_regex_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@
1212
RUBY
1313
end
1414

15+
it 'registers an offense when using match with simple string regex ' \
16+
'without a parent node' do
17+
expect_offense(<<~RUBY)
18+
match(/foo/)
19+
^^^^^^^^^^^^ Prefer using `include('foo')` when the regex is a simple string literal.
20+
RUBY
21+
22+
expect_correction(<<~RUBY)
23+
include('foo')
24+
RUBY
25+
end
26+
1527
it 'registers an offense when using match with escaped URL regex' do
1628
expect_offense(<<~'RUBY')
1729
expect(response.body).to match(/http:\/\/example\.com/)
@@ -48,6 +60,14 @@
4860
RUBY
4961
end
5062

63+
it 'does not register an offense when using match within an ' \
64+
'include matcher' do
65+
expect_no_offenses(<<~RUBY)
66+
expect(errors).to include(match(/message/))
67+
expect(errors).to include(match(/message/), match(/warning/))
68+
RUBY
69+
end
70+
5171
it 'does not register an offense when using match with anchor at start' do
5272
expect_no_offenses(<<~RUBY)
5373
expect('foobar').to match(/^foo/)

0 commit comments

Comments
 (0)