Class: RuboCop::Cop::Lint::ShadowingOuterLocalVariable
- Inherits:
-
Base
- Object
- Base
- RuboCop::Cop::Lint::ShadowingOuterLocalVariable
show all
- Defined in:
- lib/rubocop/cop/lint/shadowing_outer_local_variable.rb
Overview
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 style is encouraged:
[source,ruby]
----
worker_id, pipe = env
Ractor.new(worker_id, pipe) do |worker_id, pipe|
end
----
Constant Summary
collapse
- MSG =
'Shadowing outer local variable - `%<variable>s`.'
Constants inherited
from Base
Base::RESTRICT_ON_SEND
Instance Attribute Summary
Attributes inherited from Base
#config, #processed_source
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Base
#active_support_extensions_enabled?, #add_global_offense, #add_offense, autocorrect_incompatible_with, badge, #begin_investigation, callbacks_needed, #callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #cop_config, cop_name, #cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, #initialize, #inspect, lint?, match?, #message, #offenses, #on_investigation_end, #on_new_investigation, #on_other_file, #parse, #ready, #relevant_file?, support_autocorrect?, support_multiple_source?, #target_rails_version, #target_ruby_version
#exclude_limit
#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #autocorrect_with_disable_uncorrectable?, #correctable?, #disable_uncorrectable?, #safe_autocorrect?
#ignore_node, #ignored_node?, #part_of_ignored_node?
Methods included from Util
silence_warnings
Class Method Details
.joining_forces ⇒ Object
53
54
55
|
# File 'lib/rubocop/cop/lint/shadowing_outer_local_variable.rb', line 53
def self.joining_forces
VariableForce
end
|
Instance Method Details
#before_declaring_variable(variable, variable_table) ⇒ Object
57
58
59
60
61
62
63
64
65
66
67
|
# File 'lib/rubocop/cop/lint/shadowing_outer_local_variable.rb', line 57
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
return if same_conditions_node_different_branch?(variable, outer_local_variable)
message = format(MSG, variable: variable.name)
add_offense(variable.declaration_node, message: message)
end
|
#find_conditional_node_from_ascendant(node) ⇒ Object
93
94
95
96
97
98
|
# File 'lib/rubocop/cop/lint/shadowing_outer_local_variable.rb', line 93
def find_conditional_node_from_ascendant(node)
return unless (parent = node.parent)
return parent if parent.conditional?
find_conditional_node_from_ascendant(parent)
end
|
#node_or_its_ascendant_conditional?(node) ⇒ Boolean
100
101
102
103
104
|
# File 'lib/rubocop/cop/lint/shadowing_outer_local_variable.rb', line 100
def node_or_its_ascendant_conditional?(node)
return true if node.conditional?
!!find_conditional_node_from_ascendant(node)
end
|
#ractor_block?(node) ⇒ Object
49
50
51
|
# File 'lib/rubocop/cop/lint/shadowing_outer_local_variable.rb', line 49
def_node_matcher :ractor_block?, <<~PATTERN
(block (send (const nil? :Ractor) :new ...) ...)
PATTERN
|
#same_conditions_node_different_branch?(variable, outer_local_variable) ⇒ Boolean
69
70
71
72
73
74
75
76
77
78
79
80
81
|
# File 'lib/rubocop/cop/lint/shadowing_outer_local_variable.rb', line 69
def same_conditions_node_different_branch?(variable, outer_local_variable)
variable_node = variable_node(variable)
return false unless node_or_its_ascendant_conditional?(variable_node)
outer_local_variable_node =
find_conditional_node_from_ascendant(outer_local_variable.declaration_node)
return true unless outer_local_variable_node
return false unless outer_local_variable_node.conditional?
return true if variable_node == outer_local_variable_node
outer_local_variable_node.if_type? &&
variable_node == outer_local_variable_node.else_branch
end
|
#variable_node(variable) ⇒ Object
83
84
85
86
87
88
89
90
91
|
# File 'lib/rubocop/cop/lint/shadowing_outer_local_variable.rb', line 83
def variable_node(variable)
parent_node = variable.scope.node.parent
if parent_node.when_type?
parent_node.parent
else
parent_node
end
end
|