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.
# 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
# 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
# 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
# File lib/rubocop/cop/lint/useless_assignment.rb, line 17 def join_force?(force_class) force_class == VariableForce end
# 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
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
Generated with the Darkfish Rdoc Generator 2.