class RuboCop::Cop::Style::RedundantArgument

A.foo
string.chomp!
string.chomp
“first second”.split
string.split
exit!
exit
array.sum
[1, 2, 3].join
array.join
# good
A.foo(2)
string.chomp!(“n”)
string.chomp(“n”)
“firstnsecond”.split(“ ”)
string.split(“ ”)
exit!(false)
exit(true)
array.sum(0)
[1, 2, 3].join(“”)
array.join(”)
# bad
@example
global is changed, ‘”` is no longer a redundant argument.
argument to join is `$OUTPUT_FIELD_SEPARATOR` (or `$,`) rather than `”`, and if that
That depends on the nature of the target methods, of course. For example, the default
2. This cop may be unsafe if certain special global variables (e.g. `$;`, `$/`) are set.
methods with same name in different classes.
1. This cop matches by method names only and hence cannot tell apart
This cop is unsafe because of the following limitations:
@safety
—-
foo: 2
chomp!: “n”
chomp: “n”
split: ’ ‘
sum: 0
join: ”
Method names and their redundant arguments can be configured like this:
NOTE: This cop is limited to methods with single parameter.
Checks for a redundant argument passed to certain methods.

def argument_matched?(target_argument, redundant_argument)

def argument_matched?(target_argument, redundant_argument)
  argument = if target_argument.is_a?(AST::Node)
               target_argument.source
             elsif exclude_cntrl_character?(target_argument, redundant_argument)
               target_argument.inspect
             else
               target_argument.to_s
             end
  argument == redundant_argument
end

def argument_range(node)

def argument_range(node)
  if node.parenthesized?
    range_between(node.loc.begin.begin_pos, node.loc.end.end_pos)
  else
    range_with_surrounding_space(node.first_argument.source_range, newlines: false)
  end
end

def exclude_cntrl_character?(target_argument, redundant_argument)

def exclude_cntrl_character?(target_argument, redundant_argument)
  !target_argument.to_s.sub(/\A'/, '"').sub(/'\z/, '"').match?(/[[:cntrl:]]/) ||
    !redundant_argument.match?(/[[:cntrl:]]/)
end

def on_send(node)

def on_send(node)
  return if !NO_RECEIVER_METHODS.include?(node.method_name) && node.receiver.nil?
  return if node.arguments.count != 1
  return unless redundant_argument?(node)
  offense_range = argument_range(node)
  message = format(MSG, arg: node.first_argument.source)
  add_offense(offense_range, message: message) do |corrector|
    corrector.remove(offense_range)
  end
end

def redundant_arg_for_method(method_name)

def redundant_arg_for_method(method_name)
  arg = cop_config['Methods'].fetch(method_name) { return }
  @mem ||= {}
  @mem[method_name] ||= arg.inspect
end

def redundant_argument?(node)

def redundant_argument?(node)
  redundant_argument = redundant_arg_for_method(node.method_name.to_s)
  return false if redundant_argument.nil?
  target_argument = if node.first_argument.respond_to?(:value)
                      node.first_argument.value
                    else
                      node.first_argument
                    end
  argument_matched?(target_argument, redundant_argument)
end