Skip to content

Fix RSpec/MatchWithSimpleRegex to ignore match nested inside include matchers#2195

Open
ydah wants to merge 1 commit into
masterfrom
match-with-simple-regex-nested-include
Open

Fix RSpec/MatchWithSimpleRegex to ignore match nested inside include matchers#2195
ydah wants to merge 1 commit into
masterfrom
match-with-simple-regex-nested-include

Conversation

@ydah

@ydah ydah commented Jun 23, 2026

Copy link
Copy Markdown
Member

Fix RSpec/MatchWithSimpleRegex to ignore match when it is already nested inside include matcher arguments.

Before this change, include(match(/message/)) was detected and autocorrected to include(include('message')). The nested include form is valid matcher composition, but it is not clearer than the original expression and goes against the cop's purpose.

This keeps the direct expect(body).to match(/message/) correction, while leaving nested include(match(...)) unchanged.


Before submitting the PR make sure the following are checked:

  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Updated documentation.
  • Added an entry to the CHANGELOG.md if the new code introduces user-observable changes.
  • The build (bundle exec rake) passes (be sure to run this locally, since it may produce updated documentation that you will need to commit).

@ydah ydah changed the title Fix RSpec/MatchWithSimpleRegex to ignore match nested inside include matchers Fix RSpec/MatchWithSimpleRegex to ignore match nested inside include matchers Jun 23, 2026
@ydah ydah force-pushed the match-with-simple-regex-nested-include branch from 3c10218 to 0286913 Compare June 23, 2026 04:18
@ydah ydah marked this pull request as ready for review June 23, 2026 04:19
@ydah ydah requested a review from a team as a code owner June 23, 2026 04:19
@ydah ydah force-pushed the match-with-simple-regex-nested-include branch from 0286913 to ae8f7a8 Compare June 23, 2026 04:21
'without a parent node' do
expect_offense(<<~RUBY)
match(/foo/)
^^^^^^^^^^^^ Prefer using `include('foo')` when the regex is a simple string literal.

@bquorning bquorning Jun 23, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is wrong behavior, right? We should only match inside examples I think.

It's not something you added by the way. The cop was implemented wrong (by me) from the beginning.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

follow up: #2196

@ydah ydah force-pushed the match-with-simple-regex-nested-include branch from ae8f7a8 to cde547c Compare June 23, 2026 07:41
@ydah ydah requested a review from bquorning June 23, 2026 07:47
'include matcher' do
expect_no_offenses(<<~RUBY)
expect(errors).to include(match(/message/))
expect(errors).to include(match(/message/), match(/warning/))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is a valid use case for an example like this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants