Class: RubyLint::Analysis::ShadowingVariables

Inherits:
Base
  • Object
show all
Defined in:
lib/ruby-lint/analysis/shadowing_variables.rb

Overview

The ShadowingVariables class checks for the use of variables in a block that shadow outer variables. “Shadowing” means that a variable is used with the same name as a variable in the surrounding scope. A simple example:

number = 10

[10, 20, 30].each do |number|
  puts number # `number` is being shadowed
end

Constant Summary

Constants inherited from Base

Base::SCOPES

Instance Attribute Summary

Attributes inherited from Base

#config, #report, #vm

Attributes inherited from Iterator

#arity_cache, #arity_cache Hash containing the amount of arguments for

Instance Method Summary collapse

Methods inherited from Base

#add_message, #after_initialize, analyze?, #current_scope, #error, #info, #previous_scope, register, #set_current_scope, #set_previous_scope, #warning

Methods included from MethodEvaluation

#unpack_block

Methods inherited from Iterator

#execute_callback, #initialize, #iterate, #skip_child_nodes!

Constructor Details

This class inherits a constructor from RubyLint::Iterator

Instance Method Details

#on_block(node) ⇒ Object

Parameters:



21
22
23
24
25
26
27
28
29
# File 'lib/ruby-lint/analysis/shadowing_variables.rb', line 21

def on_block(node)
  arguments = node.children[1].children

  arguments.each do |arg|
    validate_argument(arg)
  end

  super
end

#validate_argument(node) ⇒ Object (private)

Parameters:



36
37
38
39
40
# File 'lib/ruby-lint/analysis/shadowing_variables.rb', line 36

def validate_argument(node)
  if current_scope.has_definition?(:lvar, node.name)
    warning("shadowing outer local variable #{node.name}", node)
  end
end