From 904b5beec21a6ba6896b736093158d660ab0d4f7 Mon Sep 17 00:00:00 2001 From: Vitalii Elenhaupt Date: Sun, 27 Oct 2019 21:32:55 +0200 Subject: [PATCH] Lint/UnusedArgument: fails to recognized used argument inside macro closes #117 --- spec/ameba/rule/lint/unused_argument_spec.cr | 15 +++++++++++++++ src/ameba/rule/lint/unused_argument.cr | 2 +- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/spec/ameba/rule/lint/unused_argument_spec.cr b/spec/ameba/rule/lint/unused_argument_spec.cr index df533c52..69bac6ea 100644 --- a/spec/ameba/rule/lint/unused_argument_spec.cr +++ b/spec/ameba/rule/lint/unused_argument_spec.cr @@ -228,6 +228,21 @@ module Ameba::Rule::Lint ) subject.catch(s).should be_valid end + + it "doesn't report used args in macro literals" do + s = Source.new %( + def print(f : Array(U)) forall U + f.size.times do |i| + {% if U == Float64 %} + puts f[i].round(3) + {% else %} + puts f[i] + {% end %} + end + end + ) + subject.catch(s).should be_valid + end end context "properties" do diff --git a/src/ameba/rule/lint/unused_argument.cr b/src/ameba/rule/lint/unused_argument.cr index 6abadca8..d4d9a1b9 100644 --- a/src/ameba/rule/lint/unused_argument.cr +++ b/src/ameba/rule/lint/unused_argument.cr @@ -55,7 +55,7 @@ module Ameba::Rule::Lint private def find_unused_arguments(source, scope) scope.arguments.each do |argument| - next if argument.ignored? || scope.references?(argument.variable) + next if argument.ignored? || scope.references?(argument.variable) || argument.variable.used_in_macro? name_suggestion = scope.node.is_a?(Crystal::Block) ? '_' : "_#{argument.name}" issue_for argument.node, MSG % {argument.name, name_suggestion}