Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix false positive for RSpec/EmptyExampleGroup cop with iterator and simple conditional #2022

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

lovro-bikic
Copy link

@lovro-bikic lovro-bikic commented Jan 12, 2025

Fixes a false positive for RSpec/EmptyExampleGroup with iterator and simple conditional (one that doesn't have multiple statements, so it's not wrapped in a begin node).

Previously, code such as:

context 'foo' do
  [1, 2, 3].each do |x|
    if x.odd?
      it { expect(x).to be_odd }
    else
      it { expect(x).to be_even }
    end
  end
end

would register as an offense. With this patch, the offense isn't registered anymore.


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

@lovro-bikic lovro-bikic requested a review from a team as a code owner January 12, 2025 22:08
Copy link
Member

@pirj pirj 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

expect_no_offenses(<<~RUBY)
describe 'RuboCop monthly' do
[1, 2, 3].each do |page|
if RUBY_VERSION >= 2.3
Copy link
Member

Choose a reason for hiding this comment

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

Just for clarity, does it make sense to use page in the condition? Otherwise, why it's inside the loop, and it's not the loop inside the conditional block?

Copy link
Author

Choose a reason for hiding this comment

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

I guess so, I didn't think about it much because I copied this from another example. I've updated it now to use page in the expectation, as in some other examples.

@pirj pirj requested a review from a team January 13, 2025 07:26
@lovro-bikic lovro-bikic force-pushed the fix/rspec-empty-example-group-false-positive branch from c45b1df to 774b40b Compare January 13, 2025 08:16
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