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

Make Lint/RedundantSafeNavigation aware of constant receiver #12246

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12299](https://github.com/rubocop/rubocop/issues/12299): Make `Lint/RedundantSafeNavigation` aware of constant receiver. ([@koic][])
20 changes: 16 additions & 4 deletions lib/rubocop/cop/lint/redundant_safe_navigation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ module RuboCop
module Cop
module Lint
# Checks for redundant safe navigation calls.
# `instance_of?`, `kind_of?`, `is_a?`, `eql?`, `respond_to?`, and `equal?` methods
# are checked by default. These are customizable with `AllowedMethods` option.
# Use cases where a constant is `nil` are rare and an offense is detected
# when the receiver is a constant.
#
# For all receivers, the `instance_of?`, `kind_of?`, `is_a?`, `eql?`, `respond_to?`,
# and `equal?` methods are checked by default.
# These are customizable with `AllowedMethods` option.
#
# The `AllowedMethods` option specifies nil-safe methods,
# in other words, it is a method that is allowed to skip safe navigation.
Expand All @@ -22,6 +26,9 @@ module Lint
#
# @example
# # bad
# Const&.do_something
#
# # bad
# do_something if attrs&.respond_to?(:[])
#
# # good
Expand All @@ -33,6 +40,9 @@ module Lint
# end
#
# # good
# Const.do_something
#
# # good
# while node.is_a?(BeginNode)
# node = node.parent
# end
Expand Down Expand Up @@ -63,8 +73,10 @@ class RedundantSafeNavigation < Base
PATTERN

def on_csend(node)
return unless check?(node) && allowed_method?(node.method_name)
return if respond_to_nil_specific_method?(node)
unless node.receiver.const_type?
return unless check?(node) && allowed_method?(node.method_name)
return if respond_to_nil_specific_method?(node)
end

range = range_between(node.loc.dot.begin_pos, node.source_range.end_pos)
add_offense(range) { |corrector| corrector.replace(node.loc.dot, '.') }
Expand Down
11 changes: 11 additions & 0 deletions spec/rubocop/cop/lint/redundant_safe_navigation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@
RSpec.describe RuboCop::Cop::Lint::RedundantSafeNavigation, :config do
let(:cop_config) { { 'AllowedMethods' => %w[respond_to?] } }

it 'registers an offense and corrects when `&.` is used for const receiver' do
expect_offense(<<~RUBY)
Foo&.do_something
^^^^^^^^^^^^^^ Redundant safe navigation detected.
RUBY

expect_correction(<<~RUBY)
Foo.do_something
RUBY
end

it 'registers an offense and corrects when `&.` is used inside `if` condition' do
expect_offense(<<~RUBY)
if foo&.respond_to?(:bar)
Expand Down