diff --git a/libsolidity/analysis/TypeChecker.cpp b/libsolidity/analysis/TypeChecker.cpp index ce55de000..6b2c1cb8e 100644 --- a/libsolidity/analysis/TypeChecker.cpp +++ b/libsolidity/analysis/TypeChecker.cpp @@ -830,6 +830,22 @@ void TypeChecker::endVisit(ExpressionStatement const& _statement) if (type(_statement.expression())->category() == Type::Category::RationalNumber) if (!dynamic_cast(*type(_statement.expression())).mobileType()) typeError(_statement.expression().location(), "Invalid rational number."); + + if (auto call = dynamic_cast(&_statement.expression())) + { + if (auto callType = dynamic_cast(type(call->expression()).get())) + { + using Location = FunctionType::Location; + Location location = callType->location(); + if ( + location == Location::Bare || + location == Location::BareCallCode || + location == Location::BareDelegateCall || + location == Location::Send + ) + warning(_statement.location(), "Return value of low-level calls not used."); + } + } } bool TypeChecker::visit(Conditional const& _conditional) @@ -1570,6 +1586,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::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 48f8285a3..be1e02be5 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 697b3fa98..7e81bd7ec 100644 --- a/test/libsolidity/SolidityNameAndTypeResolution.cpp +++ b/test/libsolidity/SolidityNameAndTypeResolution.cpp @@ -3750,6 +3750,79 @@ 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(success(text)); +} + +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_CASE(unused_return_value_call) +{ + char const* text = R"( + contract test { + function f() { + address(0x12).call("abc"); + } + } + )"; + BOOST_CHECK(expectError(text, true) == Error::Type::Warning); +} + +BOOST_AUTO_TEST_CASE(unused_return_value_call_value) +{ + char const* text = R"( + contract test { + function f() { + address(0x12).call.value(2)("abc"); + } + } + )"; + BOOST_CHECK(expectError(text, true) == Error::Type::Warning); +} + +BOOST_AUTO_TEST_CASE(unused_return_value_callcode) +{ + char const* text = R"( + contract test { + function f() { + address(0x12).callcode("abc"); + } + } + )"; + BOOST_CHECK(expectError(text, true) == Error::Type::Warning); +} + +BOOST_AUTO_TEST_CASE(unused_return_value_delegatecall) +{ + char const* text = R"( + contract test { + function f() { + address(0x12).delegatecall("abc"); + } + } + )"; + BOOST_CHECK(expectError(text, true) == Error::Type::Warning); +} + BOOST_AUTO_TEST_SUITE_END() }