mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Treat returns with expressions as return param use
There are many cases of code where the return parameters exist mostly as a form of documentation. This change ensures that they do not have to be used in the function body so long as there is a return supplying values
This commit is contained in:
parent
a40c8cfb68
commit
a6faa5acf3
@ -50,6 +50,7 @@ bool StaticAnalyzer::visit(FunctionDefinition const& _function)
|
||||
{
|
||||
if (_function.isImplemented())
|
||||
m_inFunction = true;
|
||||
m_currentFunction = &_function;
|
||||
m_localVarUseCount.clear();
|
||||
m_nonPayablePublic = _function.isPublic() && !_function.isPayable();
|
||||
return true;
|
||||
@ -96,6 +97,17 @@ bool StaticAnalyzer::visit(VariableDeclaration const& _variable)
|
||||
return true;
|
||||
}
|
||||
|
||||
bool StaticAnalyzer::visit(Return const& _return)
|
||||
{
|
||||
// If the return has an expression, it counts as
|
||||
// a "use" of the return parameters.
|
||||
if (m_inFunction && _return.expression() != NULL)
|
||||
for (auto const& var: m_currentFunction->returnParameterList()->parameters())
|
||||
if (var->name() != "")
|
||||
m_localVarUseCount[var.get()] += 1;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool StaticAnalyzer::visit(ExpressionStatement const& _statement)
|
||||
{
|
||||
if (_statement.expression().annotation().isPure)
|
||||
|
@ -63,7 +63,7 @@ private:
|
||||
virtual bool visit(ExpressionStatement const& _statement) override;
|
||||
virtual bool visit(VariableDeclaration const& _variable) override;
|
||||
virtual bool visit(Identifier const& _identifier) override;
|
||||
|
||||
virtual bool visit(Return const& _return) override;
|
||||
virtual bool visit(MemberAccess const& _memberAccess) override;
|
||||
|
||||
ErrorList& m_errors;
|
||||
@ -76,6 +76,7 @@ private:
|
||||
|
||||
std::map<VariableDeclaration const*, int> m_localVarUseCount;
|
||||
|
||||
const FunctionDefinition *m_currentFunction;
|
||||
bool m_inFunction = false;
|
||||
};
|
||||
|
||||
|
@ -5663,6 +5663,22 @@ BOOST_AUTO_TEST_CASE(warn_unused_return_param)
|
||||
}
|
||||
)";
|
||||
success(text);
|
||||
text = R"(
|
||||
contract C {
|
||||
function f() returns (uint a) {
|
||||
a = 1;
|
||||
}
|
||||
}
|
||||
)";
|
||||
success(text);
|
||||
text = R"(
|
||||
contract C {
|
||||
function f() returns (uint a) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
)";
|
||||
success(text);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(no_unused_warnings)
|
||||
|
Loading…
Reference in New Issue
Block a user