Class: RuboCop::Cop::Lint::ShadowingOuterLocalVariable
- 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
Instance Attribute Summary
Attributes inherited from Base
Class Method Summary collapse
Instance Method Summary collapse
- #before_declaring_variable(variable, variable_table) ⇒ Object
- #find_conditional_node_from_ascendant(node) ⇒ Object
- #node_or_its_ascendant_conditional?(node) ⇒ Boolean
- #ractor_block?(node) ⇒ Object
- #same_conditions_node_different_branch?(variable, outer_local_variable) ⇒ Boolean
- #variable_node(variable) ⇒ Object
Methods inherited from Base
#active_support_extensions_enabled?, #add_global_offense, #add_offense, #always_autocorrect?, autocorrect_incompatible_with, badge, #begin_investigation, callbacks_needed, #callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #contextual_autocorrect?, #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, #parser_engine, #ready, #relevant_file?, requires_gem, #string_literals_frozen_by_default?, support_autocorrect?, support_multiple_source?, #target_rails_version, #target_ruby_version
Methods included from ExcludeLimit
Methods included from AutocorrectLogic
#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #autocorrect_with_disable_uncorrectable?, #correctable?, #disable_uncorrectable?, #safe_autocorrect?
Methods included from IgnoredNode
#ignore_node, #ignored_node?, #part_of_ignored_node?
Methods included from Util
Constructor Details
This class inherits a constructor from RuboCop::Cop::Base
Class Method Details
.joining_forces ⇒ Object
49 50 51 |
# File 'lib/rubocop/cop/lint/shadowing_outer_local_variable.rb', line 49 def self.joining_forces VariableForce end |
Instance Method Details
#before_declaring_variable(variable, variable_table) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/rubocop/cop/lint/shadowing_outer_local_variable.rb', line 53 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) = format(MSG, variable: variable.name) add_offense(variable.declaration_node, message: ) end |
#find_conditional_node_from_ascendant(node) ⇒ Object
89 90 91 92 93 94 |
# File 'lib/rubocop/cop/lint/shadowing_outer_local_variable.rb', line 89 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
96 97 98 99 100 |
# File 'lib/rubocop/cop/lint/shadowing_outer_local_variable.rb', line 96 def node_or_its_ascendant_conditional?(node) return true if node.conditional? !!find_conditional_node_from_ascendant(node) end |
#ractor_block?(node) ⇒ Object
45 46 47 |
# File 'lib/rubocop/cop/lint/shadowing_outer_local_variable.rb', line 45 def_node_matcher :ractor_block?, <<~PATTERN (block (send (const nil? :Ractor) :new ...) ...) PATTERN |
#same_conditions_node_different_branch?(variable, outer_local_variable) ⇒ Boolean
65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/rubocop/cop/lint/shadowing_outer_local_variable.rb', line 65 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
79 80 81 82 83 84 85 86 87 |
# File 'lib/rubocop/cop/lint/shadowing_outer_local_variable.rb', line 79 def variable_node(variable) parent_node = variable.scope.node.parent if parent_node.when_type? parent_node.parent else parent_node end end |