aboutsummaryrefslogtreecommitdiffstats
path: root/libsolidity/analysis/StaticAnalyzer.cpp
diff options
context:
space:
mode:
authorRhett Aultman <roadriverrail@gmail.com>2017-04-14 22:48:59 +0800
committerchriseth <chris@ethereum.org>2017-05-03 17:26:21 +0800
commita40c8cfb68f75c22c0082714d99e9c3a24a31051 (patch)
tree4bf4be5f3a42924fbd91cda02906ad3ef201be3e /libsolidity/analysis/StaticAnalyzer.cpp
parentc09f071ff693f52a2a1434cfd6963b2d1818776e (diff)
downloaddexon-solidity-a40c8cfb68f75c22c0082714d99e9c3a24a31051.tar.gz
dexon-solidity-a40c8cfb68f75c22c0082714d99e9c3a24a31051.tar.zst
dexon-solidity-a40c8cfb68f75c22c0082714d99e9c3a24a31051.zip
Warn on unused local variables
Analyze functions for all local variables, parameters, and named return variables which are never used in the function, and issue a warning.
Diffstat (limited to 'libsolidity/analysis/StaticAnalyzer.cpp')
-rw-r--r--libsolidity/analysis/StaticAnalyzer.cpp39
1 files changed, 39 insertions, 0 deletions
diff --git a/libsolidity/analysis/StaticAnalyzer.cpp b/libsolidity/analysis/StaticAnalyzer.cpp
index 190d2420..55e7cb59 100644
--- a/libsolidity/analysis/StaticAnalyzer.cpp
+++ b/libsolidity/analysis/StaticAnalyzer.cpp
@@ -48,13 +48,52 @@ void StaticAnalyzer::endVisit(ContractDefinition const&)
bool StaticAnalyzer::visit(FunctionDefinition const& _function)
{
+ if (_function.isImplemented())
+ m_inFunction = true;
+ m_localVarUseCount.clear();
m_nonPayablePublic = _function.isPublic() && !_function.isPayable();
return true;
}
void StaticAnalyzer::endVisit(FunctionDefinition const&)
{
+ m_inFunction = false;
m_nonPayablePublic = false;
+ for (auto const& var: m_localVarUseCount)
+ if (var.second == 0)
+ warning(var.first->location(), "Unused local variable");
+}
+
+bool StaticAnalyzer::visit(Identifier const& _identifier)
+{
+ if (m_inFunction)
+ {
+ if (auto var = dynamic_cast<VariableDeclaration const*>(_identifier.annotation().referencedDeclaration))
+ {
+ solAssert(!var->name().empty(), "");
+ if (var->isLocalVariable())
+ m_localVarUseCount[var] += 1;
+ }
+ }
+ return true;
+}
+
+bool StaticAnalyzer::visit(VariableDeclaration const& _variable)
+{
+ if (m_inFunction)
+ {
+ solAssert(_variable.isLocalVariable(), "");
+ if (_variable.name() != "")
+ {
+ // The variable may have been used before reaching the
+ // declaration. If it was, we must not reset the counter,
+ // but since [] will insert the default 0, we really just
+ // need to access the map here and let it do the rest on its
+ // own.
+ m_localVarUseCount[&_variable];
+ }
+ }
+ return true;
}
bool StaticAnalyzer::visit(ExpressionStatement const& _statement)