Sha256: e3527e068ff2e5213e422aa067968ac04f546144c1bb1f213318df7fe491fe98

Contents?: true

Size: 919 Bytes

Versions: 4

Compression:

Stored size: 919 Bytes

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 investigate(processed_source)
          inspect_variables(processed_source.ast)
        end

        def before_declaring_variable(variable)
          return if variable.name.to_s.start_with?('_')

          outer_local_variable = variable_table.find_variable(variable.name)
          return unless outer_local_variable

          message = sprintf(MSG, variable.name)
          add_offence(variable.declaration_node, :expression, message)
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rubocop-0.18.1 lib/rubocop/cop/lint/shadowing_outer_local_variable.rb
rubocop-0.18.0 lib/rubocop/cop/lint/shadowing_outer_local_variable.rb
rubocop-0.17.0 lib/rubocop/cop/lint/shadowing_outer_local_variable.rb
rubocop-0.16.0 lib/rubocop/cop/lint/shadowing_outer_local_variable.rb