diff options
author | chriseth <c@ethdev.com> | 2016-06-21 20:36:23 +0800 |
---|---|---|
committer | chriseth <c@ethdev.com> | 2016-06-26 19:53:32 +0800 |
commit | cc6314cd019be80a9212f6bd5b0c86206be16c88 (patch) | |
tree | a03cbb68fa9cb55683bdfabb794eccabab4aa6d4 | |
parent | 460ed095f8f6c8e457ef6e284abc513c9e8ca765 (diff) | |
download | dexon-solidity-cc6314cd019be80a9212f6bd5b0c86206be16c88.tar dexon-solidity-cc6314cd019be80a9212f6bd5b0c86206be16c88.tar.gz dexon-solidity-cc6314cd019be80a9212f6bd5b0c86206be16c88.tar.bz2 dexon-solidity-cc6314cd019be80a9212f6bd5b0c86206be16c88.tar.lz dexon-solidity-cc6314cd019be80a9212f6bd5b0c86206be16c88.tar.xz dexon-solidity-cc6314cd019be80a9212f6bd5b0c86206be16c88.tar.zst dexon-solidity-cc6314cd019be80a9212f6bd5b0c86206be16c88.zip |
Warn about unused return values.
-rw-r--r-- | libsolidity/analysis/TypeChecker.cpp | 17 | ||||
-rw-r--r-- | libsolidity/analysis/TypeChecker.h | 3 | ||||
-rw-r--r-- | test/libsolidity/SolidityNameAndTypeResolution.cpp | 25 |
3 files changed, 45 insertions, 0 deletions
diff --git a/libsolidity/analysis/TypeChecker.cpp b/libsolidity/analysis/TypeChecker.cpp index ce55de00..c2fd3e7e 100644 --- a/libsolidity/analysis/TypeChecker.cpp +++ b/libsolidity/analysis/TypeChecker.cpp @@ -830,6 +830,13 @@ void TypeChecker::endVisit(ExpressionStatement const& _statement) if (type(_statement.expression())->category() == Type::Category::RationalNumber) if (!dynamic_cast<RationalNumberType const&>(*type(_statement.expression())).mobileType()) typeError(_statement.expression().location(), "Invalid rational number."); + + bool unusedReturnValue = true; + if (auto t = dynamic_cast<TupleType const*>(type(_statement.expression()).get())) + if (t->components().empty()) + unusedReturnValue = false; + if (unusedReturnValue) + warning(_statement.location(), "Unused return value."); } bool TypeChecker::visit(Conditional const& _conditional) @@ -1570,6 +1577,16 @@ void TypeChecker::typeError(SourceLocation const& _location, string const& _desc m_errors.push_back(err); } +void TypeChecker::warning(SourceLocation const& _location, string const& _description) +{ + auto err = make_shared<Error>(Error::Type::Warning); + *err << + errinfo_sourceLocation(_location) << + errinfo_comment(_description); + + m_errors.push_back(err); +} + void TypeChecker::fatalTypeError(SourceLocation const& _location, string const& _description) { typeError(_location, _description); diff --git a/libsolidity/analysis/TypeChecker.h b/libsolidity/analysis/TypeChecker.h index 48f8285a..be1e02be 100644 --- a/libsolidity/analysis/TypeChecker.h +++ b/libsolidity/analysis/TypeChecker.h @@ -59,6 +59,9 @@ private: /// Adds a new error to the list of errors. void typeError(SourceLocation const& _location, std::string const& _description); + /// Adds a new warning to the list of errors. + void warning(SourceLocation const& _location, std::string const& _description); + /// Adds a new error to the list of errors and throws to abort type checking. void fatalTypeError(SourceLocation const& _location, std::string const& _description); diff --git a/test/libsolidity/SolidityNameAndTypeResolution.cpp b/test/libsolidity/SolidityNameAndTypeResolution.cpp index 697b3fa9..1f42c23e 100644 --- a/test/libsolidity/SolidityNameAndTypeResolution.cpp +++ b/test/libsolidity/SolidityNameAndTypeResolution.cpp @@ -3750,6 +3750,31 @@ BOOST_AUTO_TEST_CASE(one_divided_by_three_integer_conversion) BOOST_CHECK(!success(text)); } +BOOST_AUTO_TEST_CASE(unused_return_value) +{ + char const* text = R"( + contract test { + function g() returns (uint) {} + function f() { + g(); + } + } + )"; + BOOST_CHECK(expectError(text, true) == Error::Type::Warning); +} + +BOOST_AUTO_TEST_CASE(unused_return_value_send) +{ + char const* text = R"( + contract test { + function f() { + address(0x12).send(1); + } + } + )"; + BOOST_CHECK(expectError(text, true) == Error::Type::Warning); +} + BOOST_AUTO_TEST_SUITE_END() } |