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

Cop Lint/RedundantSafeNavigation with the autocorrect option fixes the code in a suboptimal way #11918

Closed
ydakuka opened this issue Jun 2, 2023 · 10 comments

Comments

@ydakuka
Copy link

ydakuka commented Jun 2, 2023

Actual behavior

I have the code:

# frozen_string_literal: true

class HomeController < ApplicationController
  def options
    params[:key]&.to_h || {}
  end
end

I run rubocop with the autocorrect option:

ydakuka@yauhenid:~/Work/project$ brd rubocop -A app/controllers/home_controller.rb
Inspecting 1 file
W

Offenses:

app/controllers/home_controller.rb:5:31: W: [Corrected] Lint/RedundantSafeNavigation: Redundant safe navigation detected.
    params[:key]&.to_h || {}
                              ^^^^^^

1 file inspected, 1 offense detected, 1 offense corrected

I got the code:

# frozen_string_literal: true

class HomeController < ApplicationController
  def options
    params[:key].to_h || {}
  end
end

and I run rubocop again:

ydakuka@yauhenid:~/Work/project$ bin/rails_docker rubocop app/controllers/home_controller.rb
Inspecting 1 file
.

1 file inspected, no offenses detected

Expected behavior

I expected to receive the following code:

# frozen_string_literal: true

class HomeController < ApplicationController
  def options
    params[:key].to_h
  end
end

RuboCop version

ydakuka@yauhenid:~/Work/project$ bin/rails_docker rubocop -V
1.52.0 (using Parser 3.2.2.1, rubocop-ast 1.28.1, running on ruby 2.7.8) [x86_64-linux]
  - rubocop-capybara 2.18.0
  - rubocop-factory_bot 2.23.1
  - rubocop-performance 1.17.1
  - rubocop-rails 2.19.1
  - rubocop-rake 0.6.0
  - rubocop-rspec 2.22.0
  - rubocop-thread_safety 0.5.1
@koic
Copy link
Member

koic commented Jun 2, 2023

This is an unexpected false positive by Lint/RedundantSafeNavigation. #11915 prevents to false detected. OTOH, It might be possible to handle such detection apart from Lint/RedundantSafeNavigation.

@alex-damian-negru
Copy link

There's also param[:key]&.to_i || 42 which is flagged as an offense, despite the safe navigation not being redundant in such cases.

@mingan
Copy link

mingan commented Jun 5, 2023

I'm seeing v&.to_f || 0 and super&.to_i || start_date&.year marked (and auto-corrected) after 1.51.0 -> 1.52.0 update despite the safe-navigation being meaningful here.

@rnestler
Copy link

rnestler commented Jun 5, 2023

There's also param[:key]&.to_i || 42 which is flagged as an offense, despite the safe navigation not being redundant in such cases.

I also hit this with version = request.headers['Api-Version']&.to_i || 1 which would actually break stuff, since it would suddenly return 0 if Api-Version is not set.

@edsu
Copy link

edsu commented Jun 5, 2023

I'm seeing this as well with code that previously could return nil or an integer, but now returns 0 instead of nil: presence&.to_i.

edsu added a commit to sul-dlss/happy-heron that referenced this issue Jun 5, 2023
Since we need some of these methods to be able to return `nil` instead of
`0` the rubocop rule preventing this usage with `to_i` has been disabled
until: rubocop/rubocop#11918 is resolved.
edsu added a commit to sul-dlss/cocina-models that referenced this issue Jun 5, 2023
Disable Lint/RedundantSafeNavigation until this is resolved: rubocop/rubocop#11918

Previously this code could return `nil` with the safe navigation and if
this rubocop advice was followed `''` would be returned instead.
@ngan
Copy link

ngan commented Jun 7, 2023

There's also param[:key]&.to_i || 42 which is flagged as an offense, despite the safe navigation not being redundant in such cases.

This is also the case for to_s:

foo = something&.to_s || "hello"
# DOES NOT EQUAL...
foo = something.to_s || "hello"

@ngan
Copy link

ngan commented Jun 7, 2023

This cop also seems to flag this incorrectly:

foo&.public_send(:bar)

@edsu
Copy link

edsu commented Jun 12, 2023

I think this can be closed since #8867 has been resolved?

@bbatsov bbatsov closed this as completed Jun 12, 2023
@bquorning
Copy link
Contributor

I think this can be closed since #8867 has been resolved?

I am confused. #8867 was closed over 2 years ago.

@edsu
Copy link

edsu commented Jun 12, 2023

Sorry @bquorning I meant #11915

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

9 participants