Sha256: 4fb82dcf177b2d15341016d5f80c81011900d7ccd64e6f936ef3cae36b489876

Contents?: true

Size: 1.31 KB

Versions: 23

Compression:

Stored size: 1.31 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # This cop checks for the use of local variable names from an outer scope
      # in block arguments or block-local variables. This mirrors the warning
      # given by `ruby -cw` prior to Ruby 2.6:
      # "shadowing outer local variable - foo".
      #
      # @example
      #
      #   # bad
      #
      #   def some_method
      #     foo = 1
      #
      #     2.times do |foo| # shadowing outer `foo`
      #       do_something(foo)
      #     end
      #   end
      #
      # @example
      #
      #   # good
      #
      #   def some_method
      #     foo = 1
      #
      #     2.times do |bar|
      #       do_something(bar)
      #     end
      #   end
      class ShadowingOuterLocalVariable < Base
        MSG = 'Shadowing outer local variable - `%<variable>s`.'

        def self.joining_forces
          VariableForce
        end

        def before_declaring_variable(variable, variable_table)
          return if variable.should_be_unused?

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

          message = format(MSG, variable: variable.name)
          add_offense(variable.declaration_node, message: message)
        end
      end
    end
  end
end

Version data entries

23 entries across 23 versions & 3 rubygems

Version Path
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb
plaid-14.10.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb
plaid-14.7.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb
rubocop-1.5.1 lib/rubocop/cop/lint/shadowing_outer_local_variable.rb
rubocop-1.5.0 lib/rubocop/cop/lint/shadowing_outer_local_variable.rb
rubocop-1.4.2 lib/rubocop/cop/lint/shadowing_outer_local_variable.rb
rubocop-1.4.1 lib/rubocop/cop/lint/shadowing_outer_local_variable.rb
rubocop-1.4.0 lib/rubocop/cop/lint/shadowing_outer_local_variable.rb
rubocop-1.3.1 lib/rubocop/cop/lint/shadowing_outer_local_variable.rb
rubocop-1.3.0 lib/rubocop/cop/lint/shadowing_outer_local_variable.rb
rubocop-1.2.0 lib/rubocop/cop/lint/shadowing_outer_local_variable.rb
rubocop-1.1.0 lib/rubocop/cop/lint/shadowing_outer_local_variable.rb
rubocop-1.0.0 lib/rubocop/cop/lint/shadowing_outer_local_variable.rb
rubocop-0.93.1 lib/rubocop/cop/lint/shadowing_outer_local_variable.rb
rubocop-0.93.0 lib/rubocop/cop/lint/shadowing_outer_local_variable.rb
rubocop-0.92.0 lib/rubocop/cop/lint/shadowing_outer_local_variable.rb
rubocop-0.91.1 lib/rubocop/cop/lint/shadowing_outer_local_variable.rb