Class: Rubocop::Cop::Lint::ShadowingOuterLocalVariable
- Includes:
- VariableInspector
- Defined in:
- lib/rubocop/cop/lint/shadowing_outer_local_variable.rb
Overview
This cop looks for use of the same name as outer local variables
for block arguments or block local variables.
This is a mimic of the warning
"shadowing outer local variable - foo" from ruby -cw
.
Constant Summary collapse
- MSG =
'Shadowing outer local variable - %s'
Constants included from VariableInspector
VariableInspector::ARGUMENT_DECLARATION_TYPES, VariableInspector::SCOPE_TYPES, VariableInspector::VARIABLE_ASSIGNMENT_TYPES, VariableInspector::VARIABLE_DECLARATION_TYPES, VariableInspector::VARIABLE_USE_TYPES
Instance Attribute Summary
Attributes inherited from Cop
#autocorrect, #corrections, #debug, #disabled_lines, #offences
Instance Method Summary collapse
Methods included from VariableInspector
#after_declaring_variable, #after_entering_scope, #after_leaving_scope, #before_entering_scope, #before_leaving_scope, #inspect_variables, #inspect_variables_in_scope, #process_named_captures, #process_node, #process_variable_assignment, #variable_table
Methods inherited from Cop
#add_offence, all, #autocorrect_action, cop_name, cop_type, #do_autocorrect, #ignore_node, inherited, #initialize, lint?, #name, rails?, style?
Constructor Details
This class inherits a constructor from Rubocop::Cop::Cop
Instance Method Details
#before_declaring_variable(entry) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/rubocop/cop/lint/shadowing_outer_local_variable.rb', line 19 def before_declaring_variable(entry) # Only block scope can reference outer local variables. return unless variable_table.current_scope.node.type == :block return unless ARGUMENT_DECLARATION_TYPES.include?(entry.node.type) return if entry.name.to_s.start_with?('_') outer_local_variable = variable_table.find_variable_entry(entry.name) return unless outer_local_variable = sprintf(MSG, entry.name) add_offence(:warning, entry.node.loc.expression, ) end |
#investigate(processed_source) ⇒ Object
15 16 17 |
# File 'lib/rubocop/cop/lint/shadowing_outer_local_variable.rb', line 15 def investigate(processed_source) inspect_variables(processed_source.ast) end |