From 10d290cb9b131f620c56c0bc84a9ab5bcf50368b Mon Sep 17 00:00:00 2001 From: Suman Date: Tue, 29 Aug 2017 11:58:38 -0400 Subject: [PATCH] Display helpful warning for unused function arguments/return parameters --- Changelog.md | 1 + libsolidity/analysis/StaticAnalyzer.cpp | 11 ++++++++++- test/libsolidity/SolidityNameAndTypeResolution.cpp | 14 +++++++------- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/Changelog.md b/Changelog.md index f61bb560c..d36275652 100644 --- a/Changelog.md +++ b/Changelog.md @@ -2,6 +2,7 @@ Features: * Optimizer: Add new optimization step to remove unused ``JUMPDEST``s. + * Type Checker: Display helpful warning for unused function arguments/return parameters. * Type Checker: Do not show the same error multiple times for events. * Type Checker: Warn on using literals as tight packing parameters in ``keccak256``, ``sha3``, ``sha256`` and ``ripemd160``. diff --git a/libsolidity/analysis/StaticAnalyzer.cpp b/libsolidity/analysis/StaticAnalyzer.cpp index 2f1304146..d012c25d3 100644 --- a/libsolidity/analysis/StaticAnalyzer.cpp +++ b/libsolidity/analysis/StaticAnalyzer.cpp @@ -69,7 +69,16 @@ void StaticAnalyzer::endVisit(FunctionDefinition const&) m_constructor = false; for (auto const& var: m_localVarUseCount) if (var.second == 0) - m_errorReporter.warning(var.first->location(), "Unused local variable"); + { + if (var.first->isCallableParameter()) + m_errorReporter.warning( + var.first->location(), + "Unused function parameter. Remove or comment out the variable name to silence this warning." + ); + else + m_errorReporter.warning(var.first->location(), "Unused local variable."); + } + m_localVarUseCount.clear(); } diff --git a/test/libsolidity/SolidityNameAndTypeResolution.cpp b/test/libsolidity/SolidityNameAndTypeResolution.cpp index 7bef8fec7..1fbc55a27 100644 --- a/test/libsolidity/SolidityNameAndTypeResolution.cpp +++ b/test/libsolidity/SolidityNameAndTypeResolution.cpp @@ -5816,7 +5816,7 @@ BOOST_AUTO_TEST_CASE(warn_unused_local) } } )"; - CHECK_WARNING(text, "Unused"); + CHECK_WARNING(text, "Unused local variable."); } BOOST_AUTO_TEST_CASE(warn_unused_local_assigned) @@ -5828,10 +5828,10 @@ BOOST_AUTO_TEST_CASE(warn_unused_local_assigned) } } )"; - CHECK_WARNING(text, "Unused"); + CHECK_WARNING(text, "Unused local variable."); } -BOOST_AUTO_TEST_CASE(warn_unused_param) +BOOST_AUTO_TEST_CASE(warn_unused_function_parameter) { char const* text = R"( contract C { @@ -5839,7 +5839,7 @@ BOOST_AUTO_TEST_CASE(warn_unused_param) } } )"; - CHECK_WARNING(text, "Unused"); + CHECK_WARNING(text, "Unused function parameter. Remove or comment out the variable name to silence this warning."); text = R"( contract C { function f(uint a) { @@ -5849,7 +5849,7 @@ BOOST_AUTO_TEST_CASE(warn_unused_param) success(text); } -BOOST_AUTO_TEST_CASE(warn_unused_return_param) +BOOST_AUTO_TEST_CASE(warn_unused_return_parameter) { char const* text = R"( contract C { @@ -5857,7 +5857,7 @@ BOOST_AUTO_TEST_CASE(warn_unused_return_param) } } )"; - CHECK_WARNING(text, "Unused"); + CHECK_WARNING(text, "Unused function parameter. Remove or comment out the variable name to silence this warning."); text = R"( contract C { function f() returns (uint a) { @@ -5865,7 +5865,7 @@ BOOST_AUTO_TEST_CASE(warn_unused_return_param) } } )"; - CHECK_WARNING(text, "Unused"); + CHECK_WARNING(text, "Unused function parameter. Remove or comment out the variable name to silence this warning."); text = R"( contract C { function f() returns (uint) {