Skip to content

Catch argumentless matchers in RSpec/ExpectActual#2171

Open
cvx wants to merge 2 commits into
rubocop:masterfrom
cvx:argumentless
Open

Catch argumentless matchers in RSpec/ExpectActual#2171
cvx wants to merge 2 commits into
rubocop:masterfrom
cvx:argumentless

Conversation

@cvx
Copy link
Copy Markdown

@cvx cvx commented May 3, 2026

Flags e.g. expect(".foo").to be_present and expect(1).to be.


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).

@cvx cvx requested a review from a team as a code owner May 3, 2026 23:01

it 'ignores `be` with no argument' do
expect_no_offenses(<<~RUBY)
it 'flags `be` with no argument' do
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

This case wasn't supported only to simplify implementation afaict (see #858)

Comment thread docs/modules/ROOT/pages/cops_rspec.adoc Outdated
@cvx cvx force-pushed the argumentless branch from 2db0520 to 5deae14 Compare May 4, 2026 01:18
Copy link
Copy Markdown
Collaborator

@bquorning bquorning left a comment

Choose a reason for hiding this comment

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

Thank you for the PR, I think it will be a useful addition. I have added a few comments.

Also, I would prefer if the first commit was extracting the method #register_offense (without next if expected.nil?) so that the 2nd commit clearly shows what the actual change is.

Comment thread lib/rubocop/cop/rspec/expect_actual.rb Outdated
Comment thread lib/rubocop/cop/rspec/expect_actual.rb Outdated
Comment thread spec/rubocop/cop/rspec/expect_actual_spec.rb Outdated
@cvx cvx force-pushed the argumentless branch from 5deae14 to 77a5f55 Compare May 5, 2026 08:46
@cvx
Copy link
Copy Markdown
Author

cvx commented May 5, 2026

All done!

Comment thread docs/modules/ROOT/pages/cops_rspec.adoc Outdated
@cvx cvx force-pushed the argumentless branch from 77a5f55 to aba9244 Compare May 5, 2026 09:05
@bquorning bquorning requested a review from ydah May 5, 2026 10:12
@bquorning
Copy link
Copy Markdown
Collaborator

@cvx Could I ask you to rebase on top of the latest master branch please?

cvx added 2 commits May 26, 2026 13:47
Flags e.g. `expect(".foo").to be_present` and `expect(1).to be`.
@cvx
Copy link
Copy Markdown
Author

cvx commented May 26, 2026

Done!

@pirj
Copy link
Copy Markdown
Member

pirj commented May 26, 2026

Are there real life examples of this?
How those offences would be handled by the user?

@bquorning
Copy link
Copy Markdown
Collaborator

My guess is that offenses would reveal typos, where people are testing literals instead of variables. E.g. they would change expect(5).to be_odd to expect(foo).to be_odd.

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.

4 participants