Skip to content

Commit

Permalink
[Fix #9080] Make Lint/ShadowingOuterVariable aware of Ractor
Browse files Browse the repository at this point in the history
Block given to Ractor.new should not access outer variables,
so shadowing the outer local variables should be encouraged.
  • Loading branch information
tejasbubane authored and marcandre committed Dec 2, 2020
1 parent e9e4c55 commit 75e2dae
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#9080](https://github.com/rubocop-hq/rubocop/issues/9080): Make `Lint/ShadowingOuterVariable` aware of `Ractor`. ([@tejasbubane][])
13 changes: 13 additions & 0 deletions lib/rubocop/cop/lint/shadowing_outer_local_variable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ module Lint
# 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
Expand All @@ -34,12 +42,17 @@ module Lint
class ShadowingOuterLocalVariable < Base
MSG = 'Shadowing outer local variable - `%<variable>s`.'

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
Expand Down
11 changes: 11 additions & 0 deletions spec/rubocop/cop/lint/shadowing_outer_local_variable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,15 @@ def some_method(foo)
RUBY
end
end

context 'with Ractor.new' do
it 'does not regiser an offense' do
expect_no_offenses(<<~RUBY)
def foo(*args)
Ractor.new(*args) do |*args|
end
end
RUBY
end
end
end

0 comments on commit 75e2dae

Please sign in to comment.