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 #13766] Fix false positives for Style/InverseMethods #13927

Merged

Conversation

koic
Copy link
Member

@koic koic commented Feb 28, 2025

This PR fixes false positives for Style/InverseMethods when using any? or none? with safe navigation operator.
Additionally, some specs for the existing incorrect behavior has been updated.

Fixes #13766.


Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.

@koic koic force-pushed the fix_false_positive_for_style_inverse_methods branch from e3fd2c3 to fd4f559 Compare February 28, 2025 05:19
Comment on lines 171 to 170
def incompatible_method?(node)
return false if node.send_type?

INCOMPATIBLE_INVERSE_METHODS.include?(node.method_name)
end
Copy link
Member

Choose a reason for hiding this comment

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

These are only incompatible with safe navigation, correct? I think it'd make sense to make the method name more descriptive.

Copy link
Member Author

Choose a reason for hiding this comment

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

Finding a better name is difficult 😅 but incompatible_context? has been chosen.

@koic koic force-pushed the fix_false_positive_for_style_inverse_methods branch 2 times, most recently from 026b4dc to 63c778b Compare February 28, 2025 10:30
@bbatsov
Copy link
Collaborator

bbatsov commented Mar 4, 2025

@koic You should probably add a note in the docs about this case, so it's obvious we have special handling for it.

@@ -46,6 +46,7 @@ class InverseMethods < Base

MSG = 'Use `%<inverse>s` instead of inverting `%<method>s`.'
CLASS_COMPARISON_METHODS = %i[<= >= < >].freeze
INCOMPATIBLE_INVERSE_METHODS = %i[any? none?].freeze
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd probably add a comment here mentioning they are incompatible only in the context of safe navigation.

This PR fixes false positives for `Style/InverseMethods`
when using `any?` or `none?` with safe navigation operator.

Additionally, some specs for the existing incorrect behavior has been updated.

Fixes rubocop#13766.
@koic koic force-pushed the fix_false_positive_for_style_inverse_methods branch from 63c778b to eff6547 Compare March 4, 2025 06:34
@koic
Copy link
Member Author

koic commented Mar 4, 2025

I adjusted the naming and logic based on the background of safe navigation instead of adding comments. I hope the naming appropriately reflects the background, considering the amount of code changes. If there is still any lack of clarity, I will add comments, so please let me know.

@bbatsov bbatsov merged commit ba9d953 into rubocop:master Mar 4, 2025
24 checks passed
@bbatsov
Copy link
Collaborator

bbatsov commented Mar 4, 2025

I think that's enough for now.

@koic koic deleted the fix_false_positive_for_style_inverse_methods branch March 4, 2025 07:24
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.

Incorrect auto-correct on Style/InverseMethods and safe navigation.
3 participants