class RuboCop::Cop::Rails::SafeNavigation

foo&.bar { |e| e.baz }
foo&.bar(baz)
foo&.bar
# good
foo.try(:bar) { |e| e.baz }
foo.try(:bar, baz)
foo.try(:bar)
foo.try!(:bar) { |e| e.baz }
foo.try!(:bar, baz)
foo.try!(:bar)
# bad
# ConvertTry: true
foo&.bar { |e| e.baz }
foo&.bar(baz)
foo&.bar
foo.try(:bar) { |e| e.baz }
foo.try(:bar, baz)
foo.try(:bar)
# good
foo.try!(:[], 0)
foo.try!(:bar) { |e| e.baz }
foo.try!(:bar, baz)
foo.try!(:bar)
# bad
# ConvertTry: false
@example
target Ruby version is set to 2.3+
to convert ‘try`. It will convert code to use safe navigation if the
This cop converts usages of `try!` to `&.`. It can also be configured

def autocorrect(node)

def autocorrect(node)
  method_node, *params = *node.arguments
  method = method_node.source[1..-1]
  range = range_between(node.loc.dot.begin_pos,
                        node.loc.expression.end_pos)
  lambda do |corrector|
    corrector.replace(range, replacement(method, params))
  end
end

def on_send(node)

def on_send(node)
  try_call(node) do |try_method, dispatch|
    return if try_method == :try && !cop_config['ConvertTry']
    return unless dispatch.sym_type? && dispatch.value =~ /\w+[=!?]?/
    add_offense(node, message: format(MSG, try: try_method))
  end
end

def replacement(method, params)

def replacement(method, params)
  new_params = params.map(&:source).join(', ')
  if method.end_with?('=')
    "&.#{method[0...-1]} = #{new_params}"
  elsif params.empty?
    "&.#{method}"
  else
    "&.#{method}(#{new_params})"
  end
end