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

Style/SafeNavigation False positive with TargetRubyVersion: 2.0 #10679

Closed
mishina2228 opened this issue May 30, 2022 · 0 comments · Fixed by #10680
Closed

Style/SafeNavigation False positive with TargetRubyVersion: 2.0 #10679

mishina2228 opened this issue May 30, 2022 · 0 comments · Fixed by #10680
Labels

Comments

@mishina2228
Copy link
Contributor

Expected behavior

No offense with Style/SafeNavigation,
because safe navigation operator was introduced in Ruby 2.3.

Actual behavior

$ rubocop --debug sample.rb
For /tmp/rubocop: configuration from /tmp/rubocop/.rubocop.yml
Default configuration from /home/owner/.rbenv/versions/3.1.0/lib/ruby/gems/3.1.0/gems/rubocop-1.30.0/config/default.yml
Use parallel by default.
Running parallel inspection
Loading cache from /home/owner/.cache/rubocop_cache/d10146b520466ece109729e7403f11d5d0a5ad94/6d7a3b621ca1730e04accd938619e4bdab66cfb1/59ebe8899ab1693cd0e5ef08914c6351047e072e
Inspecting 1 file
Scanning /tmp/rubocop/sample.rb
Loading cache from /home/owner/.cache/rubocop_cache/d10146b520466ece109729e7403f11d5d0a5ad94/6d7a3b621ca1730e04accd938619e4bdab66cfb1/59ebe8899ab1693cd0e5ef08914c6351047e072e
C

Offenses:

sample.rb:2:3: C: [Correctable] Style/SafeNavigation: Use safe navigation (&.) instead of checking if an object exists before calling the method.
  arg && arg.bar
  ^^^^^^^^^^^^^^

1 file inspected, 1 offense detected, 1 offense autocorrectable
Finished in 0.11757984200085048 seconds

Steps to reproduce the problem

.rubocop.yml

AllCops:
  TargetRubyVersion: 2.0
  NewCops: enable

sample.rb

def foo(arg)
  arg && arg.bar
end

RuboCop version

$ rubocop -V
1.30.0 (using Parser 3.1.2.0, rubocop-ast 1.18.0, running on ruby 3.1.0 x86_64-linux)
@koic koic added the bug label May 30, 2022
koic added a commit to koic/rubocop that referenced this issue May 30, 2022
Fixes rubocop#10679.

Fix a false positive for `Style/SafeNavigation` when `TargetRubyVersion: 2.2` or lower.
koic added a commit that referenced this issue May 30, 2022
…fe_navigation

[Fix #10679] Fix a false positive for `Style/SafeNavigation`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants