Sha256: 4b2ad043102c115fc358fff3c2100da200175da4c7b44102c9c419da3c22f6e8

Contents?: true

Size: 1.05 KB

Versions: 1

Compression:

Stored size: 1.05 KB

Contents

# encoding: utf-8

module Rubocop
  module Cop
    module Lint
      # 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`.
      class ShadowingOuterLocalVariable < Cop
        include VariableInspector

        MSG = 'Shadowing outer local variable - %s'

        def inspect(source_buffer, source, tokens, ast, comments)
          inspect_variables(ast)
        end

        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)

          outer_local_variable = variable_table.find_variable_entry(entry.name)
          return unless outer_local_variable

          message = sprintf(MSG, entry.name)
          add_offence(:warning, entry.node.loc.expression, message)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rubocop-0.9.0 lib/rubocop/cop/lint/shadowing_outer_local_variable.rb