class RubyLint::Analysis::ShadowingVariables

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

Public Instance Methods

on_block(node) click to toggle source

@param [RubyLint::AST::Node] node

Calls superclass method
# 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

Private Instance Methods

validate_argument(node) click to toggle source

@param [RubyLint::AST::Node] node

# 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