Skip macro scopes in ShadowingOuterLocalVar

This commit is contained in:
Sijawusz Pur Rahnama 2022-04-04 01:36:44 +02:00
parent 2e9ef7fcb2
commit 614fcfa6a8
2 changed files with 22 additions and 4 deletions

View file

@ -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

View file

@ -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)