class RuboCop::Cop::Lint::UnusedMethodArgument

Checks for unused method arguments.

@example

# bad
def some_method(used, unused, _unused_but_allowed)
  puts used
end

# good
def some_method(used, _unused, _unused_but_allowed)
  puts used
end

@example AllowUnusedKeywordArguments: false (default)

# bad
def do_something(used, unused: 42)
  used
end

@example AllowUnusedKeywordArguments: true

# good
def do_something(used, unused: 42)
  used
end

@example IgnoreEmptyMethods: true (default)

# good
def do_something(unused)
end

@example IgnoreEmptyMethods: false

# bad
def do_something(unused)
end

@example IgnoreNotImplementedMethods: true (default)

# good
def do_something(unused)
  raise NotImplementedError
end

def do_something_else(unused)
  fail "TODO"
end

@example IgnoreNotImplementedMethods: false

# bad
def do_something(unused)
  raise NotImplementedError
end

def do_something_else(unused)
  fail "TODO"
end

Public Class Methods

autocorrect_incompatible_with() click to toggle source
# File lib/rubocop/cop/lint/unused_method_argument.rb, line 71
def self.autocorrect_incompatible_with
  [Style::ExplicitBlockArgument]
end
joining_forces() click to toggle source
# File lib/rubocop/cop/lint/unused_method_argument.rb, line 75
def self.joining_forces
  VariableForce
end

Private Instance Methods

autocorrect(corrector, node) click to toggle source
# File lib/rubocop/cop/lint/unused_method_argument.rb, line 81
def autocorrect(corrector, node)
  UnusedArgCorrector.correct(corrector, processed_source, node)
end
check_argument(variable) click to toggle source
# File lib/rubocop/cop/lint/unused_method_argument.rb, line 85
def check_argument(variable)
  return unless variable.method_argument?
  return if variable.keyword_argument? && cop_config['AllowUnusedKeywordArguments']
  return if ignored_method?(variable.scope.node.body)

  super
end
ignored_method?(body) click to toggle source
# File lib/rubocop/cop/lint/unused_method_argument.rb, line 93
def ignored_method?(body)
  (cop_config['IgnoreEmptyMethods'] && body.nil?) ||
    (cop_config['IgnoreNotImplementedMethods'] && not_implemented?(body))
end
message(variable) click to toggle source
# File lib/rubocop/cop/lint/unused_method_argument.rb, line 98
def message(variable)
  message = +"Unused method argument - `#{variable.name}`."

  unless variable.keyword_argument?
    message << " If it's necessary, use `_` or `_#{variable.name}` " \
               "as an argument name to indicate that it won't be used. " \
               "If it's unnecessary, remove it."
  end

  scope = variable.scope
  all_arguments = scope.variables.each_value.select(&:method_argument?)

  if all_arguments.none?(&:referenced?)
    message << " You can also write as `#{scope.name}(*)` " \
               'if you want the method to accept any arguments ' \
               "but don't care about them."
  end

  message
end