From ee9394b4aa0dc1370e9c767ee117a4972c67c817 Mon Sep 17 00:00:00 2001 From: Mathias Baumann Date: Mon, 18 Feb 2019 16:42:58 +0100 Subject: [PATCH 1/2] Move 'warnUnused' tests to subfolder --- .../function_parameter.sol} | 0 .../{435_warn_unused_local.sol => warnUnused/local.sol} | 0 .../local_assignment.sol} | 0 .../return_parameter.sol} | 0 4 files changed, 0 insertions(+), 0 deletions(-) rename test/libsolidity/syntaxTests/nameAndTypeResolution/{437_warn_unused_function_parameter.sol => warnUnused/function_parameter.sol} (100%) rename test/libsolidity/syntaxTests/nameAndTypeResolution/{435_warn_unused_local.sol => warnUnused/local.sol} (100%) rename test/libsolidity/syntaxTests/nameAndTypeResolution/{436_warn_unused_local_assigned.sol => warnUnused/local_assignment.sol} (100%) rename test/libsolidity/syntaxTests/nameAndTypeResolution/{439_warn_unused_return_parameter.sol => warnUnused/return_parameter.sol} (100%) 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 From e3d4155fa88f843db9a5411e463ba6a1c3f92018 Mon Sep 17 00:00:00 2001 From: Mathias Baumann Date: Mon, 18 Feb 2019 18:19:55 +0100 Subject: [PATCH 2/2] Detect custom type expressions that have no effect --- Changelog.md | 1 + libsolidity/analysis/TypeChecker.cpp | 5 +++++ .../syntaxTests/metaTypes/typeOfContract.sol | 1 + .../001_name_references.sol | 2 ++ .../no_effect_statements.sol | 16 ++++++++++++++++ 5 files changed, 25 insertions(+) create mode 100644 test/libsolidity/syntaxTests/nameAndTypeResolution/no_effect_statements.sol diff --git a/Changelog.md b/Changelog.md index 62536e8bf..e2042e3fc 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 ce1910129..2f754c432 100644 --- a/libsolidity/analysis/TypeChecker.cpp +++ b/libsolidity/analysis/TypeChecker.cpp @@ -2316,8 +2316,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.