Navigation Menu

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 a false positive for Style/SafeNavigation #8654

Merged
merged 1 commit into from Sep 6, 2020

Conversation

koic
Copy link
Member

@koic koic commented Sep 5, 2020

This PR fixes the following error for Style/SafeNavigation when checking foo&.empty? in a conditional.

% cat example.rb
do_something if ENV['VERSION'] && ENV['VERSION'].empty?

% bunble exec rubocop -a example.rb --only Style/SafeNavigation,Lint/SafeNavigationWithEmpty
(snip)

Inspecting 1 file
W

Offenses:

example.rb:1:17: W: [Corrected] Lint/SafeNavigationWithEmpty: Avoid
calling empty? with the safe navigation operator in conditionals.
do_something if ENV['VERSION']&.empty?
                ^^^^^^^^^^^^^^^^^^^^^^
example.rb:1:17: C: [Corrected] Style/SafeNavigation: Use safe
navigation (&.) instead of checking if an object exists before calling
the method.
do_something if ENV['VERSION'] && ENV['VERSION'].empty?
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

0 files inspected, 2 offenses detected, 2 offenses corrected
Infinite loop detected in
/Users/koic/src/github.com/koic/rubocop-issues/rails/example.rb.
/Users/koic/src/github.com/rubocop-hq/rubocop/lib/rubocop/runner.rb:289:in
`check_for_infinite_loop'
/Users/koic/src/github.com/rubocop-hq/rubocop/lib/rubocop/runner.rb:272:in
`block in iterate_until_no_changes'
/Users/koic/src/github.com/rubocop-hq/rubocop/lib/rubocop/runner.rb:271:in
`loop'
/Users/koic/src/github.com/rubocop-hq/rubocop/lib/rubocop/runner.rb:271:in
`iterate_until_no_changes'
/Users/koic/src/github.com/rubocop-hq/rubocop/lib/rubocop/runner.rb:242:in
`do_inspection_loop'

I found it with the following code.
https://github.com/rails/rails/blob/v6.0.3.2/activerecord/lib/active_record/railties/databases.rake#L120


Before submitting the PR make sure the following are checked:

  • 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.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Run bundle exec rake default. It executes all tests and RuboCop for itself, and generates the documentation.

Copy link
Collaborator

@bbatsov bbatsov 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!

@koic koic force-pushed the fix_false_positive_for_safe_navigation branch from e3c9b4c to 35ebbf9 Compare September 6, 2020 08:03
This PR fixes the following error for `Style/SafeNavigation`
when checking `foo&.empty?` in a conditional.

```console
% cat example.rb
do_something if ENV['VERSION'] && ENV['VERSION'].empty?

% bunble exec rubocop -a example.rb --only Style/SafeNavigation,Lint/SafeNavigationWithEmpty
(snip)

Inspecting 1 file
W

Offenses:

example.rb:1:17: W: [Corrected] Lint/SafeNavigationWithEmpty: Avoid
calling empty? with the safe navigation operator in conditionals.
do_something if ENV['VERSION']&.empty?
                ^^^^^^^^^^^^^^^^^^^^^^
example.rb:1:17: C: [Corrected] Style/SafeNavigation: Use safe
navigation (&.) instead of checking if an object exists before calling
the method.
do_something if ENV['VERSION'] && ENV['VERSION'].empty?
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

0 files inspected, 2 offenses detected, 2 offenses corrected
Infinite loop detected in
/Users/koic/src/github.com/koic/rubocop-issues/rails/example.rb.
/Users/koic/src/github.com/rubocop-hq/rubocop/lib/rubocop/runner.rb:289:in
`check_for_infinite_loop'
/Users/koic/src/github.com/rubocop-hq/rubocop/lib/rubocop/runner.rb:272:in
`block in iterate_until_no_changes'
/Users/koic/src/github.com/rubocop-hq/rubocop/lib/rubocop/runner.rb:271:in
`loop'
/Users/koic/src/github.com/rubocop-hq/rubocop/lib/rubocop/runner.rb:271:in
`iterate_until_no_changes'
/Users/koic/src/github.com/rubocop-hq/rubocop/lib/rubocop/runner.rb:242:in
`do_inspection_loop'
```

I found it with the following code.
https://github.com/rails/rails/blob/v6.0.3.2/activerecord/lib/active_record/railties/databases.rake#L120
@koic koic merged commit 3e304ba into rubocop:master Sep 6, 2020
@koic koic deleted the fix_false_positive_for_safe_navigation branch September 6, 2020 08:22
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.

None yet

2 participants