class RuboCop::Cop::Lint::RedundantSafeNavigation


foo&.respond_to?(:to_a)
# good - without ‘&.` this will always return `true`
end
node = node.parent
while node.is_a?(BeginNode)
# good
end
node = node.parent
while node&.is_a?(BeginNode)
# bad
do_something if attrs.respond_to?(:[])
# good
do_something if attrs&.respond_to?(:[])
# bad
@example
because `NilClass` has methods like `respond_to?` and `is_a?`.
In the example below, the safe navigation operator (`&.`) is unnecessary
could return `nil` will be auto-corrected to never return `nil`.
return type of the expression. An offending expression that previously
This cop is marked as unsafe, because auto-correction can change the
are checked by default. These are customizable with `AllowedMethods` option.
`instance_of?`, `kind_of?`, `is_a?`, `eql?`, `respond_to?`, and `equal?` methods
This cop checks for redundant safe navigation calls.

def check?(node)

def check?(node)
  parent = node.parent
  return false unless parent
  condition?(parent, node) ||
    parent.and_type? ||
    parent.or_type? ||
    (parent.send_type? && parent.negation_method?)
end

def condition?(parent, node)

def condition?(parent, node)
  (parent.conditional? || parent.post_condition_loop?) && parent.condition == node
end

def on_csend(node)

def on_csend(node)
  return unless check?(node) && allowed_method?(node.method_name)
  return if respond_to_nil_specific_method?(node)
  range = range_between(node.loc.dot.begin_pos, node.source_range.end_pos)
  add_offense(range) { |corrector| corrector.replace(node.loc.dot, '.') }
end