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

Rails/NegateInclude should not auto correct double negation include? #460

Closed
eneagoe opened this issue Apr 13, 2021 · 5 comments
Closed

Comments

@eneagoe
Copy link

eneagoe commented Apr 13, 2021

This looks very similar to #384


Expected behavior

!!collection.include?(x) should not correct to !collection.exclude?(x)

Actual behavior

Correction was applied, but resulted in broken code - not all collections support the exclude? method.

Although In most cases !!collection.include?(x) is equivalent to collection.include?(x), there are exceptions. In particular, ruby 3.0 introduced a breaking change in SortedSet#include?: if the object is not in the sorted set, the method returns nil instead of false - hence the need for the !! trick.

RuboCop version

$ [bundle exec] rubocop -V
1.12.1 (using Parser 3.0.1.0, rubocop-ast 1.4.1, running on ruby 3.0.1 x86_64-darwin19)
  - rubocop-rails 2.9.1
@koic
Copy link
Member

koic commented Apr 13, 2021

Thank you for your feedback. I'm not sure about this is an expected behavior change. Let's take a look at answer knu/sorted_set#2.

@andyw8
Copy link
Contributor

andyw8 commented Apr 13, 2021

Yes, I don't think RuboCop should be working around what seems like a bug in Ruby or a core gem.

@eneagoe
Copy link
Author

eneagoe commented Apr 13, 2021

I'm afraid this is still a bug in rubocop - !!collection.include?(field) will be changed by rubocop -A to !collection.exclude?(field) which will throw an error: exclude? is not a method on some collection objects.

@koic
Copy link
Member

koic commented Apr 14, 2021

JFYI, rubocop -A option is for unsafe auto-correction (rubocop -a option is for safe auto-correction).
https://docs.rubocop.org/rubocop/1.3/usage/auto_correct.html

The cop is already marked as unsafe.
https://github.com/rubocop/rubocop-rails/blob/v2.9.1/config/default.yml#L410

@eneagoe
Copy link
Author

eneagoe commented Apr 14, 2021

Thank you for clarifying!

@eneagoe eneagoe closed this as completed Apr 14, 2021
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

No branches or pull requests

3 participants