From 15b86bf159744fd8e642988476d042d3eba4e467 Mon Sep 17 00:00:00 2001 From: Basile Burg Date: Wed, 21 Dec 2016 21:16:42 +0100 Subject: [PATCH] fix #380, templated enum seen as unused variable --- src/analysis/unused.d | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/src/analysis/unused.d b/src/analysis/unused.d index fe3fb15..5c9fd38 100644 --- a/src/analysis/unused.d +++ b/src/analysis/unused.d @@ -10,7 +10,7 @@ import analysis.base; import std.container; import std.regex : Regex, regex, matchAll; import dsymbol.scope_ : Scope; -import std.algorithm : map; +import std.algorithm.iteration : map; /** * Checks for unused variables. @@ -207,10 +207,14 @@ class UnusedVariableCheck : BaseAnalyzer { if (interestDepth > 0) { - if (primary.identifierOrTemplateInstance !is null - && primary.identifierOrTemplateInstance.identifier != tok!"") + const IdentifierOrTemplateInstance idt = primary.identifierOrTemplateInstance; + + if (idt !is null) { - variableUsed(primary.identifierOrTemplateInstance.identifier.text); + if (idt.identifier != tok!"") + variableUsed(idt.identifier.text); + else if (idt.templateInstance && idt.templateInstance.identifier != tok!"") + variableUsed(idt.templateInstance.identifier.text); } if (mixinDepth > 0 && primary.primary == tok!"stringLiteral" || primary.primary == tok!"wstringLiteral" @@ -430,6 +434,20 @@ unittest int a; // [warn]: Variable a is never used. } + // Issue 380 + int templatedEnum() + { + enum a(T) = T.init; + return a!int; + } + + // Issue 380 + int otherTemplatedEnum() + { + auto a(T) = T.init; // [warn]: Variable a is never used. + return 0; + } + void doStuff(int a, int b) // [warn]: Parameter b is never used. { return a;