Parent

Files

Class/Module Index [+]

Quicksearch

RuboCop::Cop::Lint::UselessAssignment

This cop checks for every useless assignment to local variable in every scope. The basic idea for this cop was from the warning of `ruby -cw`:

assigned but unused variable - foo

Currently this cop has advanced logic that detects unreferenced reassignments and properly handles varied cases such as branch, loop, rescue, ensure, etc.

Constants

MSG

Public Instance Methods

after_leaving_scope(scope, _variable_table) click to toggle source
# File lib/rubocop/cop/lint/useless_assignment.rb, line 21
def after_leaving_scope(scope, _variable_table)
  scope.variables.each_value do |variable|
    check_for_unused_assignments(variable)
    check_for_unused_block_local_variable(variable)
  end
end
check_for_unused_assignments(variable) click to toggle source
# File lib/rubocop/cop/lint/useless_assignment.rb, line 28
def check_for_unused_assignments(variable)
  return if variable.name.to_s.start_with?('_')

  variable.assignments.each do |assignment|
    next if assignment.used?

    message = message_for_useless_assignment(assignment)

    location = if assignment.regexp_named_capture?
                 assignment.node.children.first.loc.expression
               else
                 assignment.node.loc.name
               end

    add_offense(nil, location, message)
  end
end
check_for_unused_block_local_variable(variable) click to toggle source
# File lib/rubocop/cop/lint/useless_assignment.rb, line 76
def check_for_unused_block_local_variable(variable)
  return unless variable.block_local_variable?
  return unless variable.assignments.empty?
  message = format(MSG, variable.name)
  add_offense(variable.declaration_node, :expression, message)
end
join_force?(force_class) click to toggle source
# File lib/rubocop/cop/lint/useless_assignment.rb, line 17
def join_force?(force_class)
  force_class == VariableForce
end
message_for_useless_assignment(assignment) click to toggle source
# File lib/rubocop/cop/lint/useless_assignment.rb, line 46
def message_for_useless_assignment(assignment)
  variable = assignment.variable

  message = format(MSG, variable.name)

  if assignment.multiple_assignment?
    message << " Use `_` or `_#{variable.name}` as a variable name "                         "to indicate that it won't be used."
  elsif assignment.operator_assignment?
    return_value_node = return_value_node_of_scope(variable.scope)
    if assignment.meta_assignment_node.equal?(return_value_node)
      non_assignment_operator = assignment.operator.sub(/=$/, '')
      message << " Use just operator `#{non_assignment_operator}`."
    end
  end

  message
end
return_value_node_of_scope(scope) click to toggle source

TODO: More precise handling (rescue, ensure, nested begin, etc.)

# File lib/rubocop/cop/lint/useless_assignment.rb, line 66
def return_value_node_of_scope(scope)
  body_node = scope.body_node

  if body_node.type == :begin
    body_node.children.last
  else
    body_node
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.