Class: Rubocop::Cop::Lint::UselessAssignment
- Includes:
- VariableInspector
- Defined in:
- lib/rubocop/cop/lint/useless_assignment.rb
Overview
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.
Constant Summary collapse
- MSG =
'Useless assignment to variable - %s'
Constants included from VariableInspector
VariableInspector::ARGUMENT_DECLARATION_TYPES, VariableInspector::BLOCK_ARGUMENT_DECLARATION_TYPE, VariableInspector::BLOCK_LOCAL_VARIABLE_DECLARATION_TYPE, VariableInspector::DECLARATION_TYPES, VariableInspector::LOGICAL_OPERATOR_ASSIGNMENT_TYPES, VariableInspector::LOOP_TYPES, VariableInspector::METHOD_ARGUMENT_DECLARATION_TYPES, VariableInspector::MULTIPLE_ASSIGNMENT_TYPE, VariableInspector::OPERATOR_ASSIGNMENT_TYPES, VariableInspector::POST_CONDITION_LOOP_TYPES, VariableInspector::REGEXP_NAMED_CAPTURE_TYPE, VariableInspector::RESCUE_TYPE, VariableInspector::SCOPE_TYPES, VariableInspector::TWISTED_SCOPE_TYPES, VariableInspector::VARIABLE_ASSIGNMENT_TYPE, VariableInspector::VARIABLE_ASSIGNMENT_TYPES, VariableInspector::VARIABLE_REFERENCE_TYPE, VariableInspector::ZERO_ARITY_SUPER_TYPE
Constants inherited from Cop
Instance Attribute Summary
Attributes inherited from Cop
#config, #corrections, #offences, #processed_source
Instance Method Summary collapse
- #after_leaving_scope(scope) ⇒ Object
- #check_for_unused_assignments(variable) ⇒ Object
- #check_for_unused_block_local_variable(variable) ⇒ Object
- #investigate(processed_source) ⇒ Object
- #message_for_useless_assignment(assignment) ⇒ Object
-
#return_value_node_of_scope(scope) ⇒ Object
TODO: More precise handling (rescue, ensure, nested begin, etc.).
Methods included from VariableInspector
#after_declaring_variable, #after_entering_scope, #before_declaring_variable, #before_entering_scope, #before_leaving_scope, #dispatch_node, #inspect_variables, #inspect_variables_in_scope, #mark_assignments_as_referenced_in_loop, #process_children, #process_loop, #process_node, #process_regexp_named_captures, #process_rescue, #process_scope, #process_variable_assignment, #process_variable_declaration, #process_variable_multiple_assignment, #process_variable_operator_assignment, #process_variable_referencing, #process_zero_arity_super, #scan, #scanned_node?, #scanned_nodes, #skip_children!, #variable_table
Methods inherited from Cop
#add_offence, all, #autocorrect?, #convention, #cop_config, cop_name, #cop_name, cop_type, #debug?, #ignore_node, inherited, #initialize, lint?, #message, non_rails, rails?, style?, #support_autocorrect?, #warning
Constructor Details
This class inherits a constructor from Rubocop::Cop::Cop
Instance Method Details
#after_leaving_scope(scope) ⇒ Object
24 25 26 27 28 29 |
# File 'lib/rubocop/cop/lint/useless_assignment.rb', line 24 def after_leaving_scope(scope) 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) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/rubocop/cop/lint/useless_assignment.rb', line 31 def check_for_unused_assignments(variable) return if variable.name.to_s.start_with?('_') variable.assignments.each do |assignment| next if assignment.used? = (assignment) location = if assignment.regexp_named_capture? assignment.node.children.first.loc.expression else assignment.node.loc.name end warning(nil, location, ) end end |
#check_for_unused_block_local_variable(variable) ⇒ Object
79 80 81 82 83 84 |
# File 'lib/rubocop/cop/lint/useless_assignment.rb', line 79 def check_for_unused_block_local_variable(variable) return unless variable.block_local_variable? return unless variable.assignments.empty? = sprintf(MSG, variable.name) warning(variable.declaration_node, :expression, ) end |
#investigate(processed_source) ⇒ Object
20 21 22 |
# File 'lib/rubocop/cop/lint/useless_assignment.rb', line 20 def investigate(processed_source) inspect_variables(processed_source.ast) end |
#message_for_useless_assignment(assignment) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/rubocop/cop/lint/useless_assignment.rb', line 49 def (assignment) variable = assignment.variable = sprintf(MSG, variable.name) if assignment.multiple_assignment? << ". 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..equal?(return_value_node) non_assignment_operator = assignment.operator.sub(/=$/, '') << ". Use just operator #{non_assignment_operator}." end end end |
#return_value_node_of_scope(scope) ⇒ Object
TODO: More precise handling (rescue, ensure, nested begin, etc.)
69 70 71 72 73 74 75 76 77 |
# File 'lib/rubocop/cop/lint/useless_assignment.rb', line 69 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 |