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

Add new RSpec/BeNil cop #1239

Merged
merged 1 commit into from
Feb 24, 2022
Merged

Add new RSpec/BeNil cop #1239

merged 1 commit into from
Feb 24, 2022

Conversation

bquorning
Copy link
Collaborator

@bquorning bquorning commented Feb 23, 2022

Check that be_nil is used instead of be(nil).

RSpec has a built-in be_nil matcher specifically for expecting nil. For consistent specs, we recommend using that instead of be(nil).

Partially fixes #244.


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

If you have created a new cop:

  • Added the new cop to config/default.yml.
  • The cop is configured as Enabled: pending in config/default.yml.
  • The cop documents examples of good and bad code.
  • The tests assert both that bad code is reported and that good code is not reported.
  • Set VersionAdded in default/config.yml to the next minor version.

If you have modified an existing cop's configuration options:

  • Set VersionChanged in config/default.yml to the next major version.

Check that `be_nil` is used instead of `be(nil)`.

RSpec has a built-in `be_nil` matcher specifically for expecting `nil`.
For consistent specs, we recommend using that instead of `be(nil).
@bquorning bquorning marked this pull request as ready for review February 23, 2022 21:49
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.

Looks good, thank you!

@pirj pirj merged commit 0a1a7b9 into master Feb 24, 2022
@pirj pirj deleted the add-be-nil-cop branch February 24, 2022 20:29
@marcandre
Copy link
Contributor

Note that it's not 100% safe.

expect(OpenStruct.new(nil?: true)).to be_nil # => pass
expect(OpenStruct.new(nil?: true)).to be nil # => fail

More importantly, I see absolutely no reason to prefer be_nil to be nil. The later is stricter, simpler and requires less knowledge of rspec.

@pirj
Copy link
Member

pirj commented Mar 15, 2022

We couldn't figure out a real example.

@bquorning
Copy link
Collaborator Author

I see absolutely no reason to prefer be_nil to be nil. The later is stricter, simpler and requires less knowledge of rspec.

Well, that is true 😅 Should we consider making the cop configurable to users can enforce either one style or the other?

bquorning added a commit that referenced this pull request Mar 20, 2022
Make `RSpec/BeNil` cop configurable with a `be_nil` and a `be` style.

Inspired by @marcandre's comment at
#1239 (comment)

    I see absolutely no reason to prefer `be_nil` to `be nil`. The
    latter is stricter, simpler and requires less knowledge of rspec.
bquorning added a commit that referenced this pull request Mar 20, 2022
Make `RSpec/BeNil` cop configurable with a `be_nil` and a `be` style.

Inspired by @marcandre's comment at
#1239 (comment)

    I see absolutely no reason to prefer `be_nil` to `be nil`. The
    latter is stricter, simpler and requires less knowledge of rspec.
bquorning added a commit that referenced this pull request Mar 20, 2022
Make `RSpec/BeNil` cop configurable with a `be_nil` and a `be` style.

Inspired by @marcandre's comment at
#1239 (comment)

    I see absolutely no reason to prefer `be_nil` to `be nil`. The
    latter is stricter, simpler and requires less knowledge of rspec.
@bquorning bquorning mentioned this pull request Mar 20, 2022
7 tasks
bquorning added a commit that referenced this pull request Mar 20, 2022
Make `RSpec/BeNil` cop configurable with a `be_nil` and a `be` style.

Inspired by @marcandre's comment at
#1239 (comment)

    I see absolutely no reason to prefer `be_nil` to `be nil`. The
    latter is stricter, simpler and requires less knowledge of rspec.
bquorning added a commit that referenced this pull request Mar 21, 2022
Make `RSpec/BeNil` cop configurable with a `be_nil` and a `be` style.

Inspired by @marcandre's comment at
#1239 (comment)

    I see absolutely no reason to prefer `be_nil` to `be nil`. The
    latter is stricter, simpler and requires less knowledge of rspec.
bquorning added a commit that referenced this pull request Mar 21, 2022
Make `RSpec/BeNil` cop configurable with a `be_nil` and a `be` style.

Inspired by @marcandre's comment at
#1239 (comment)

    I see absolutely no reason to prefer `be_nil` to `be nil`. The
    latter is stricter, simpler and requires less knowledge of rspec.
bquorning added a commit that referenced this pull request Mar 21, 2022
Make `RSpec/BeNil` cop configurable with a `be_nil` and a `be` style.

Inspired by @marcandre's comment at
#1239 (comment)

    I see absolutely no reason to prefer `be_nil` to `be nil`. The
    latter is stricter, simpler and requires less knowledge of rspec.
bquorning added a commit that referenced this pull request Mar 21, 2022
Make `RSpec/BeNil` cop configurable with a `be_nil` and a `be` style.

Inspired by @marcandre's comment at
#1239 (comment)

    I see absolutely no reason to prefer `be_nil` to `be nil`. The
    latter is stricter, simpler and requires less knowledge of rspec.
pirj pushed a commit to rubocop/rubocop-capybara that referenced this pull request Dec 29, 2022
Make `RSpec/BeNil` cop configurable with a `be_nil` and a `be` style.

Inspired by @marcandre's comment at
rubocop/rubocop-rspec#1239 (comment)

    I see absolutely no reason to prefer `be_nil` to `be nil`. The
    latter is stricter, simpler and requires less knowledge of rspec.
ydah pushed a commit to rubocop/rubocop-factory_bot that referenced this pull request Apr 13, 2023
Make `RSpec/BeNil` cop configurable with a `be_nil` and a `be` style.

Inspired by @marcandre's comment at
rubocop/rubocop-rspec#1239 (comment)

    I see absolutely no reason to prefer `be_nil` to `be nil`. The
    latter is stricter, simpler and requires less knowledge of rspec.
ydah pushed a commit to rubocop/rubocop-rspec_rails that referenced this pull request Mar 27, 2024
Make `RSpec/BeNil` cop configurable with a `be_nil` and a `be` style.

Inspired by @marcandre's comment at
rubocop/rubocop-rspec#1239 (comment)

    I see absolutely no reason to prefer `be_nil` to `be nil`. The
    latter is stricter, simpler and requires less knowledge of rspec.
ydah pushed a commit to rubocop/rubocop-rspec_rails that referenced this pull request Mar 27, 2024
Make `RSpec/BeNil` cop configurable with a `be_nil` and a `be` style.

Inspired by @marcandre's comment at
rubocop/rubocop-rspec#1239 (comment)

    I see absolutely no reason to prefer `be_nil` to `be nil`. The
    latter is stricter, simpler and requires less knowledge of rspec.
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.

Identity comparison vs. value comparison
4 participants