diff --git a/Changelog.md b/Changelog.md index 3fa7c9dec..153cb63c7 100644 --- a/Changelog.md +++ b/Changelog.md @@ -4,6 +4,7 @@ Language Features: Compiler Features: + * Static Analyzer: Warn about expressions with custom types when they have no effect. Bugfixes: diff --git a/libsolidity/analysis/TypeChecker.cpp b/libsolidity/analysis/TypeChecker.cpp index 8c89f249d..9fb3392b1 100644 --- a/libsolidity/analysis/TypeChecker.cpp +++ b/libsolidity/analysis/TypeChecker.cpp @@ -2320,8 +2320,13 @@ bool TypeChecker::visit(Identifier const& _identifier) if (auto variableDeclaration = dynamic_cast(annotation.referencedDeclaration)) annotation.isPure = annotation.isConstant = variableDeclaration->isConstant(); else if (dynamic_cast(annotation.referencedDeclaration)) + { if (dynamic_cast(annotation.type.get())) annotation.isPure = true; + } + else if (dynamic_cast(annotation.type.get())) + annotation.isPure = true; + // Check for deprecated function names. // The check is done here for the case without an actual function call. diff --git a/test/libsolidity/syntaxTests/metaTypes/typeOfContract.sol b/test/libsolidity/syntaxTests/metaTypes/typeOfContract.sol index 036c36e8f..c0d2577e2 100644 --- a/test/libsolidity/syntaxTests/metaTypes/typeOfContract.sol +++ b/test/libsolidity/syntaxTests/metaTypes/typeOfContract.sol @@ -4,3 +4,4 @@ contract Test { } } // ---- +// Warning: (78-88): Statement has no effect. diff --git a/test/libsolidity/syntaxTests/nameAndTypeResolution/001_name_references.sol b/test/libsolidity/syntaxTests/nameAndTypeResolution/001_name_references.sol index dc304a1dc..ae4671381 100644 --- a/test/libsolidity/syntaxTests/nameAndTypeResolution/001_name_references.sol +++ b/test/libsolidity/syntaxTests/nameAndTypeResolution/001_name_references.sol @@ -2,3 +2,5 @@ contract test { uint256 variable; function f(uint256) public returns (uint out) { f(variable); test; out; } } +// ---- +// Warning: (103-107): Statement has no effect. diff --git a/test/libsolidity/syntaxTests/nameAndTypeResolution/no_effect_statements.sol b/test/libsolidity/syntaxTests/nameAndTypeResolution/no_effect_statements.sol new file mode 100644 index 000000000..ea867c10b --- /dev/null +++ b/test/libsolidity/syntaxTests/nameAndTypeResolution/no_effect_statements.sol @@ -0,0 +1,16 @@ +contract test { + struct s { uint a; uint b;} + function f() pure public returns (byte) { + s; + s(1,2); + s[7]; + uint; + uint[7]; + } +} +// ---- +// Warning: (93-94): Statement has no effect. +// Warning: (98-104): Statement has no effect. +// Warning: (108-112): Statement has no effect. +// Warning: (116-120): Statement has no effect. +// Warning: (124-131): Statement has no effect. diff --git a/test/libsolidity/syntaxTests/nameAndTypeResolution/437_warn_unused_function_parameter.sol b/test/libsolidity/syntaxTests/nameAndTypeResolution/warnUnused/function_parameter.sol similarity index 100% rename from test/libsolidity/syntaxTests/nameAndTypeResolution/437_warn_unused_function_parameter.sol rename to test/libsolidity/syntaxTests/nameAndTypeResolution/warnUnused/function_parameter.sol diff --git a/test/libsolidity/syntaxTests/nameAndTypeResolution/435_warn_unused_local.sol b/test/libsolidity/syntaxTests/nameAndTypeResolution/warnUnused/local.sol similarity index 100% rename from test/libsolidity/syntaxTests/nameAndTypeResolution/435_warn_unused_local.sol rename to test/libsolidity/syntaxTests/nameAndTypeResolution/warnUnused/local.sol diff --git a/test/libsolidity/syntaxTests/nameAndTypeResolution/436_warn_unused_local_assigned.sol b/test/libsolidity/syntaxTests/nameAndTypeResolution/warnUnused/local_assignment.sol similarity index 100% rename from test/libsolidity/syntaxTests/nameAndTypeResolution/436_warn_unused_local_assigned.sol rename to test/libsolidity/syntaxTests/nameAndTypeResolution/warnUnused/local_assignment.sol diff --git a/test/libsolidity/syntaxTests/nameAndTypeResolution/439_warn_unused_return_parameter.sol b/test/libsolidity/syntaxTests/nameAndTypeResolution/warnUnused/return_parameter.sol similarity index 100% rename from test/libsolidity/syntaxTests/nameAndTypeResolution/439_warn_unused_return_parameter.sol rename to test/libsolidity/syntaxTests/nameAndTypeResolution/warnUnused/return_parameter.sol