Parent

Files

Class/Module Index [+]

Quicksearch

RuboCop::Cop::Lint::UnusedBlockArgument

This cop checks for unused block arguments.

@example

do_something do |used, unused, _unused_but_allowed|
  puts used
end

Public Instance Methods

check_argument(variable) click to toggle source
# File lib/rubocop/cop/lint/unused_block_argument.rb, line 16
def check_argument(variable)
  return unless variable.block_argument?
  super
end
message(variable) click to toggle source
# File lib/rubocop/cop/lint/unused_block_argument.rb, line 21
def message(variable)
  message = "Unused block argument - `#{variable.name}`. "

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

  if lambda?(scope.node)
    message << message_for_lambda(variable, all_arguments)
  else
    message << message_for_normal_block(variable, all_arguments)
  end

  message
end
message_for_lambda(variable, all_arguments) click to toggle source
# File lib/rubocop/cop/lint/unused_block_argument.rb, line 48
def message_for_lambda(variable, all_arguments)
  message = message_for_underscore_prefix(variable)

  if all_arguments.none?(&:referenced?)
    message << ' Also consider using a proc without arguments '                         'instead of a lambda if you want it '                          "to accept any arguments but don't care about them."
  end

  message
end
message_for_normal_block(variable, all_arguments) click to toggle source
# File lib/rubocop/cop/lint/unused_block_argument.rb, line 36
def message_for_normal_block(variable, all_arguments)
  if all_arguments.none?(&:referenced?)
    if all_arguments.count > 1
      "You can omit all the arguments if you don't care about them."
    else
      "You can omit the argument if you don't care about it."
    end
  else
    message_for_underscore_prefix(variable)
  end
end
message_for_underscore_prefix(variable) click to toggle source
# File lib/rubocop/cop/lint/unused_block_argument.rb, line 60
def message_for_underscore_prefix(variable)
  "If it's necessary, use `_` or `_#{variable.name}` "            "as an argument name to indicate that it won't be used."
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.