From 614fcfa6a87a206a448c64299919128a18deda87 Mon Sep 17 00:00:00 2001 From: Sijawusz Pur Rahnama Date: Mon, 4 Apr 2022 01:36:44 +0200 Subject: [PATCH] Skip macro scopes in `ShadowingOuterLocalVar` --- .../rule/lint/shadowing_outer_local_var_spec.cr | 17 +++++++++++++++++ .../rule/lint/shadowing_outer_local_var.cr | 9 +++++---- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/spec/ameba/rule/lint/shadowing_outer_local_var_spec.cr b/spec/ameba/rule/lint/shadowing_outer_local_var_spec.cr index 25bea9b0..5706841f 100644 --- a/spec/ameba/rule/lint/shadowing_outer_local_var_spec.cr +++ b/spec/ameba/rule/lint/shadowing_outer_local_var_spec.cr @@ -235,6 +235,23 @@ module Ameba::Rule::Lint end CRYSTAL end + + # https://github.com/crystal-ameba/ameba/issues/224#issuecomment-822245167 + it "does not report scoped vars to MacroFor (2)" do + expect_no_issues subject, <<-CRYSTAL + struct Test + def test + {% begin %} + {% for ivar in @type.instance_vars %} + {% var_type = ivar %} + {% end %} + + {% ["a", "b"].map { |ivar| puts ivar } %} + {% end %} + end + end + CRYSTAL + end end end end diff --git a/src/ameba/rule/lint/shadowing_outer_local_var.cr b/src/ameba/rule/lint/shadowing_outer_local_var.cr index 8f70a285..d9abaec8 100644 --- a/src/ameba/rule/lint/shadowing_outer_local_var.cr +++ b/src/ameba/rule/lint/shadowing_outer_local_var.cr @@ -39,7 +39,10 @@ module Ameba::Rule::Lint MSG = "Shadowing outer local variable `%s`" def test(source) - AST::ScopeVisitor.new self, source + AST::ScopeVisitor.new self, source, skip: [ + Crystal::Macro, + Crystal::MacroFor, + ] end def test(source, node : Crystal::ProcLiteral, scope : AST::Scope) @@ -51,9 +54,7 @@ module Ameba::Rule::Lint end private def find_shadowing(source, scope) - outer_scope = scope.outer_scope - - return if outer_scope.nil? || outer_scope.in_macro? + return unless outer_scope = scope.outer_scope scope.arguments.reject(&.ignored?).each do |arg| variable = outer_scope.find_variable(arg.name)