Sha256: 0ff1d23847ca60090a5af386ffc75935e2ea3567ea99a303580434f2d17a655a

Contents?: true

Size: 1.82 KB

Versions: 20

Compression:

Stored size: 1.82 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".
      #
      # NOTE: Shadowing of variables in block passed to `Ractor.new` is allowed
      # because `Ractor` should not access outer variables.
      # eg. following syle is encouraged:
      #
      #   worker_id, pipe = env
      #   Ractor.new(worker_id, pipe) do |worker_id, pipe|
      #   end
      #
      # @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`.'

        # @!method ractor_block?(node)
        def_node_matcher :ractor_block?, <<~PATTERN
          (block (send (const nil? :Ractor) :new ...) ...)
        PATTERN

        def self.joining_forces
          VariableForce
        end

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

          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

20 entries across 20 versions & 3 rubygems

Version Path
rubocop-1.21.0 lib/rubocop/cop/lint/shadowing_outer_local_variable.rb
rubocop-1.20.0 lib/rubocop/cop/lint/shadowing_outer_local_variable.rb
rubocop-1.19.1 lib/rubocop/cop/lint/shadowing_outer_local_variable.rb
rails_mini_profiler-0.2.0 vendor/bundle/ruby/3.0.0/gems/rubocop-1.18.3/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb
rubocop-1.19.0 lib/rubocop/cop/lint/shadowing_outer_local_variable.rb
rubocop-1.18.4 lib/rubocop/cop/lint/shadowing_outer_local_variable.rb
rubocop-1.18.3 lib/rubocop/cop/lint/shadowing_outer_local_variable.rb
rubocop-1.18.2 lib/rubocop/cop/lint/shadowing_outer_local_variable.rb
rubocop-1.18.1 lib/rubocop/cop/lint/shadowing_outer_local_variable.rb
rubocop-1.18.0 lib/rubocop/cop/lint/shadowing_outer_local_variable.rb
rubocop-1.17.0 lib/rubocop/cop/lint/shadowing_outer_local_variable.rb
rubocop-1.16.1 lib/rubocop/cop/lint/shadowing_outer_local_variable.rb
rubocop-1.16.0 lib/rubocop/cop/lint/shadowing_outer_local_variable.rb
rubocop-1.15.0 lib/rubocop/cop/lint/shadowing_outer_local_variable.rb
cocRb-0.1.0 .bundle/ruby/3.0.0/gems/rubocop-1.14.0/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb
rubocop-1.14.0 lib/rubocop/cop/lint/shadowing_outer_local_variable.rb
rubocop-1.13.0 lib/rubocop/cop/lint/shadowing_outer_local_variable.rb
rubocop-1.12.1 lib/rubocop/cop/lint/shadowing_outer_local_variable.rb
rubocop-1.12.0 lib/rubocop/cop/lint/shadowing_outer_local_variable.rb
rubocop-1.11.0 lib/rubocop/cop/lint/shadowing_outer_local_variable.rb