Remove more false positives in unused variable detection
This commit is contained in:
parent
f71b1191c1
commit
b105a0f6a2
|
@ -15,7 +15,7 @@ struct Message
|
||||||
|
|
||||||
enum comparitor = q{ a.line < b.line || a.line < b.line };
|
enum comparitor = q{ a.line < b.line || a.line < b.line };
|
||||||
|
|
||||||
alias MessageSet = RedBlackTree!(Message, comparitor);
|
alias MessageSet = RedBlackTree!(Message, comparitor, true);
|
||||||
|
|
||||||
abstract class BaseAnalyzer : ASTVisitor
|
abstract class BaseAnalyzer : ASTVisitor
|
||||||
{
|
{
|
||||||
|
|
|
@ -20,7 +20,13 @@ class UnusedVariableCheck : BaseAnalyzer
|
||||||
this(string fileName)
|
this(string fileName)
|
||||||
{
|
{
|
||||||
super(fileName);
|
super(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
override void visit(const Module mod)
|
||||||
|
{
|
||||||
pushScope();
|
pushScope();
|
||||||
|
mod.accept(this);
|
||||||
|
popScope();
|
||||||
}
|
}
|
||||||
|
|
||||||
override void visit(const Declaration declaration)
|
override void visit(const Declaration declaration)
|
||||||
|
@ -40,8 +46,6 @@ class UnusedVariableCheck : BaseAnalyzer
|
||||||
else if (!isOverride)
|
else if (!isOverride)
|
||||||
{
|
{
|
||||||
pushScope();
|
pushScope();
|
||||||
foreach (parameter; functionDec.parameters.parameters)
|
|
||||||
visit(parameter);
|
|
||||||
functionDec.accept(this);
|
functionDec.accept(this);
|
||||||
popScope();
|
popScope();
|
||||||
}
|
}
|
||||||
|
@ -61,6 +65,20 @@ class UnusedVariableCheck : BaseAnalyzer
|
||||||
interestDepth--;
|
interestDepth--;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override void visit(const NewExpression newExpression)
|
||||||
|
{
|
||||||
|
interestDepth++;
|
||||||
|
newExpression.accept(this);
|
||||||
|
interestDepth--;
|
||||||
|
}
|
||||||
|
|
||||||
|
override void visit(const TemplateArgumentList argumentList)
|
||||||
|
{
|
||||||
|
interestDepth++;
|
||||||
|
argumentList.accept(this);
|
||||||
|
interestDepth--;
|
||||||
|
}
|
||||||
|
|
||||||
override void visit(const SwitchStatement switchStatement)
|
override void visit(const SwitchStatement switchStatement)
|
||||||
{
|
{
|
||||||
if (switchStatement.expression !is null)
|
if (switchStatement.expression !is null)
|
||||||
|
@ -164,6 +182,28 @@ class UnusedVariableCheck : BaseAnalyzer
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override void visit(const TemplateDeclaration templateDeclaration)
|
||||||
|
{
|
||||||
|
bool addScope = templateDeclaration.declarations.length > 0;
|
||||||
|
auto inAgg = inAggregateScope;
|
||||||
|
inAggregateScope = true;
|
||||||
|
templateDeclaration.accept(this);
|
||||||
|
inAggregateScope = inAgg;
|
||||||
|
}
|
||||||
|
|
||||||
|
override void visit(const IdentifierOrTemplateChain chain)
|
||||||
|
{
|
||||||
|
if (interestDepth > 0 && chain.identifiersOrTemplateInstances[0].identifier != tok!"")
|
||||||
|
variableUsed(chain.identifiersOrTemplateInstances[0].identifier.text);
|
||||||
|
chain.accept(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
override void visit(const TemplateSingleArgument single)
|
||||||
|
{
|
||||||
|
if (single.token != tok!"")
|
||||||
|
variableUsed(single.token.text);
|
||||||
|
}
|
||||||
|
|
||||||
override void visit(const PrimaryExpression primary)
|
override void visit(const PrimaryExpression primary)
|
||||||
{
|
{
|
||||||
if (interestDepth > 0 && primary.identifierOrTemplateInstance !is null
|
if (interestDepth > 0 && primary.identifierOrTemplateInstance !is null
|
||||||
|
@ -188,10 +228,12 @@ class UnusedVariableCheck : BaseAnalyzer
|
||||||
{
|
{
|
||||||
bool sb = inAggregateScope;
|
bool sb = inAggregateScope;
|
||||||
inAggregateScope = false;
|
inAggregateScope = false;
|
||||||
|
if (blockStatementIntroducesScope)
|
||||||
pushScope();
|
pushScope();
|
||||||
blockStatement.accept(this);
|
blockStatement.accept(this);
|
||||||
|
if (blockStatementIntroducesScope)
|
||||||
popScope();
|
popScope();
|
||||||
inAggregateScope = true;
|
inAggregateScope = sb;
|
||||||
}
|
}
|
||||||
|
|
||||||
override void visit(const VariableDeclaration variableDeclaration)
|
override void visit(const VariableDeclaration variableDeclaration)
|
||||||
|
@ -201,6 +243,13 @@ class UnusedVariableCheck : BaseAnalyzer
|
||||||
variableDeclaration.accept(this);
|
variableDeclaration.accept(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override void visit(const SliceExpression sliceExpression)
|
||||||
|
{
|
||||||
|
interestDepth++;
|
||||||
|
sliceExpression.accept(this);
|
||||||
|
interestDepth--;
|
||||||
|
}
|
||||||
|
|
||||||
override void visit(const AutoDeclaration autoDeclaration)
|
override void visit(const AutoDeclaration autoDeclaration)
|
||||||
{
|
{
|
||||||
foreach (t; autoDeclaration.identifiers)
|
foreach (t; autoDeclaration.identifiers)
|
||||||
|
@ -212,11 +261,15 @@ class UnusedVariableCheck : BaseAnalyzer
|
||||||
{
|
{
|
||||||
import std.algorithm;
|
import std.algorithm;
|
||||||
import std.array;
|
import std.array;
|
||||||
|
// import std.stdio;
|
||||||
if (parameter.name != tok!"")
|
if (parameter.name != tok!"")
|
||||||
|
{
|
||||||
|
// stderr.writeln("Adding parameter ", parameter.name.text);
|
||||||
variableDeclared(parameter.name.text, parameter.name.line,
|
variableDeclared(parameter.name.text, parameter.name.line,
|
||||||
parameter.name.column, true, canFind(parameter.parameterAttributes,
|
parameter.name.column, true, canFind(parameter.parameterAttributes,
|
||||||
cast(IdType) tok!"ref"));
|
cast(IdType) tok!"ref"));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
override void visit(const StructBody structBody)
|
override void visit(const StructBody structBody)
|
||||||
{
|
{
|
||||||
|
@ -227,11 +280,21 @@ class UnusedVariableCheck : BaseAnalyzer
|
||||||
inAggregateScope = sb;
|
inAggregateScope = sb;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override void visit(const ConditionalStatement conditionalStatement)
|
||||||
|
{
|
||||||
|
bool cs = blockStatementIntroducesScope;
|
||||||
|
blockStatementIntroducesScope = false;
|
||||||
|
conditionalStatement.accept(this);
|
||||||
|
blockStatementIntroducesScope = cs;
|
||||||
|
}
|
||||||
|
|
||||||
void variableDeclared(string name, size_t line, size_t column,
|
void variableDeclared(string name, size_t line, size_t column,
|
||||||
bool isParameter, bool isRef)
|
bool isParameter, bool isRef)
|
||||||
{
|
{
|
||||||
|
// import std.stdio;
|
||||||
if (inAggregateScope)
|
if (inAggregateScope)
|
||||||
return;
|
return;
|
||||||
|
// stderr.writeln("Adding ", name, " ", isParameter, " ", isRef);
|
||||||
tree[$ - 1].insert(new UnUsed(name, line, column, isParameter, isRef));
|
tree[$ - 1].insert(new UnUsed(name, line, column, isParameter, isRef));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -280,4 +343,6 @@ class UnusedVariableCheck : BaseAnalyzer
|
||||||
bool isOverride;
|
bool isOverride;
|
||||||
|
|
||||||
bool inAggregateScope;
|
bool inAggregateScope;
|
||||||
|
|
||||||
|
bool blockStatementIntroducesScope = true;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue