From 53537864a2b7322c58248ea8c9937d13aca7ca61 Mon Sep 17 00:00:00 2001 From: Leonardo Alt Date: Wed, 12 Dec 2018 12:02:47 +0100 Subject: [PATCH] Disable unused var warning for functions with empty body --- libsolidity/analysis/StaticAnalyzer.cpp | 28 +++++++++---------- ...ction_external_call_allowed_conversion.sol | 1 - ...1_function_internal_allowed_conversion.sol | 1 - .../057_legal_override_direct.sol | 1 - .../058_legal_override_indirect.sol | 1 - ...061_missing_base_constructor_arguments.sol | 1 - ...62_base_constructor_arguments_override.sol | 1 - .../216_function_argument_storage_to_mem.sol | 1 - .../437_warn_unused_function_parameter.sol | 1 - .../439_warn_unused_return_parameter.sol | 1 - .../syntaxTests/parsing/empty_function.sol | 3 -- .../parsing/function_normal_comments.sol | 2 -- ...pe_as_storage_variable_with_assignment.sol | 3 -- .../parsing/function_type_in_expression.sol | 3 -- ...ltiple_functions_natspec_documentation.sol | 8 ------ .../parsing/single_function_param.sol | 2 -- 16 files changed, 14 insertions(+), 44 deletions(-) diff --git a/libsolidity/analysis/StaticAnalyzer.cpp b/libsolidity/analysis/StaticAnalyzer.cpp index 383918412..4d0f34611 100644 --- a/libsolidity/analysis/StaticAnalyzer.cpp +++ b/libsolidity/analysis/StaticAnalyzer.cpp @@ -63,21 +63,21 @@ bool StaticAnalyzer::visit(FunctionDefinition const& _function) void StaticAnalyzer::endVisit(FunctionDefinition const&) { - m_currentFunction = nullptr; - m_constructor = false; - for (auto const& var: m_localVarUseCount) - if (var.second == 0) - { - if (var.first.second->isCallableParameter()) - m_errorReporter.warning( - var.first.second->location(), - "Unused function parameter. Remove or comment out the variable name to silence this warning." - ); - else - m_errorReporter.warning(var.first.second->location(), "Unused local variable."); - } - + if (m_currentFunction && !m_currentFunction->body().statements().empty()) + for (auto const& var: m_localVarUseCount) + if (var.second == 0) + { + if (var.first.second->isCallableParameter()) + m_errorReporter.warning( + var.first.second->location(), + "Unused function parameter. Remove or comment out the variable name to silence this warning." + ); + else + m_errorReporter.warning(var.first.second->location(), "Unused local variable."); + } m_localVarUseCount.clear(); + m_constructor = false; + m_currentFunction = nullptr; } bool StaticAnalyzer::visit(Identifier const& _identifier) diff --git a/test/libsolidity/syntaxTests/nameAndTypeResolution/049_function_external_call_allowed_conversion.sol b/test/libsolidity/syntaxTests/nameAndTypeResolution/049_function_external_call_allowed_conversion.sol index ec72adeb9..2ca092db1 100644 --- a/test/libsolidity/syntaxTests/nameAndTypeResolution/049_function_external_call_allowed_conversion.sol +++ b/test/libsolidity/syntaxTests/nameAndTypeResolution/049_function_external_call_allowed_conversion.sol @@ -7,5 +7,4 @@ contract Test { function g (C c) external {} } // ---- -// Warning: (125-128): Unused function parameter. Remove or comment out the variable name to silence this warning. // Warning: (113-141): Function state mutability can be restricted to pure diff --git a/test/libsolidity/syntaxTests/nameAndTypeResolution/051_function_internal_allowed_conversion.sol b/test/libsolidity/syntaxTests/nameAndTypeResolution/051_function_internal_allowed_conversion.sol index aedc7b0b3..b5c5bf5a6 100644 --- a/test/libsolidity/syntaxTests/nameAndTypeResolution/051_function_internal_allowed_conversion.sol +++ b/test/libsolidity/syntaxTests/nameAndTypeResolution/051_function_internal_allowed_conversion.sol @@ -9,5 +9,4 @@ contract Test { } } // ---- -// Warning: (68-71): Unused function parameter. Remove or comment out the variable name to silence this warning. // Warning: (56-82): Function state mutability can be restricted to pure diff --git a/test/libsolidity/syntaxTests/nameAndTypeResolution/057_legal_override_direct.sol b/test/libsolidity/syntaxTests/nameAndTypeResolution/057_legal_override_direct.sol index 062507ee0..4f836baf9 100644 --- a/test/libsolidity/syntaxTests/nameAndTypeResolution/057_legal_override_direct.sol +++ b/test/libsolidity/syntaxTests/nameAndTypeResolution/057_legal_override_direct.sol @@ -1,6 +1,5 @@ contract B { function f() public {} } contract C is B { function f(uint i) public {} } // ---- -// Warning: (67-73): Unused function parameter. Remove or comment out the variable name to silence this warning. // Warning: (13-35): Function state mutability can be restricted to pure // Warning: (56-84): Function state mutability can be restricted to pure diff --git a/test/libsolidity/syntaxTests/nameAndTypeResolution/058_legal_override_indirect.sol b/test/libsolidity/syntaxTests/nameAndTypeResolution/058_legal_override_indirect.sol index f59da4726..8f3d268bb 100644 --- a/test/libsolidity/syntaxTests/nameAndTypeResolution/058_legal_override_indirect.sol +++ b/test/libsolidity/syntaxTests/nameAndTypeResolution/058_legal_override_indirect.sol @@ -2,6 +2,5 @@ contract A { function f(uint a) public {} } contract B { function f() public {} } contract C is A, B { } // ---- -// Warning: (24-30): Unused function parameter. Remove or comment out the variable name to silence this warning. // Warning: (13-41): Function state mutability can be restricted to pure // Warning: (57-79): Function state mutability can be restricted to pure diff --git a/test/libsolidity/syntaxTests/nameAndTypeResolution/061_missing_base_constructor_arguments.sol b/test/libsolidity/syntaxTests/nameAndTypeResolution/061_missing_base_constructor_arguments.sol index 8ebb46aa2..31be70cad 100644 --- a/test/libsolidity/syntaxTests/nameAndTypeResolution/061_missing_base_constructor_arguments.sol +++ b/test/libsolidity/syntaxTests/nameAndTypeResolution/061_missing_base_constructor_arguments.sol @@ -1,4 +1,3 @@ contract A { constructor(uint a) public { } } contract B is A { } // ---- -// Warning: (25-31): Unused function parameter. Remove or comment out the variable name to silence this warning. diff --git a/test/libsolidity/syntaxTests/nameAndTypeResolution/062_base_constructor_arguments_override.sol b/test/libsolidity/syntaxTests/nameAndTypeResolution/062_base_constructor_arguments_override.sol index 8ebb46aa2..31be70cad 100644 --- a/test/libsolidity/syntaxTests/nameAndTypeResolution/062_base_constructor_arguments_override.sol +++ b/test/libsolidity/syntaxTests/nameAndTypeResolution/062_base_constructor_arguments_override.sol @@ -1,4 +1,3 @@ contract A { constructor(uint a) public { } } contract B is A { } // ---- -// Warning: (25-31): Unused function parameter. Remove or comment out the variable name to silence this warning. diff --git a/test/libsolidity/syntaxTests/nameAndTypeResolution/216_function_argument_storage_to_mem.sol b/test/libsolidity/syntaxTests/nameAndTypeResolution/216_function_argument_storage_to_mem.sol index c5175a410..9785c25bf 100644 --- a/test/libsolidity/syntaxTests/nameAndTypeResolution/216_function_argument_storage_to_mem.sol +++ b/test/libsolidity/syntaxTests/nameAndTypeResolution/216_function_argument_storage_to_mem.sol @@ -6,5 +6,4 @@ contract C { } } // ---- -// Warning: (91-106): Unused function parameter. Remove or comment out the variable name to silence this warning. // Warning: (80-122): Function state mutability can be restricted to pure diff --git a/test/libsolidity/syntaxTests/nameAndTypeResolution/437_warn_unused_function_parameter.sol b/test/libsolidity/syntaxTests/nameAndTypeResolution/437_warn_unused_function_parameter.sol index 8a36eaad0..4227cbb90 100644 --- a/test/libsolidity/syntaxTests/nameAndTypeResolution/437_warn_unused_function_parameter.sol +++ b/test/libsolidity/syntaxTests/nameAndTypeResolution/437_warn_unused_function_parameter.sol @@ -3,4 +3,3 @@ contract C { } } // ---- -// Warning: (28-34): Unused function parameter. Remove or comment out the variable name to silence this warning. diff --git a/test/libsolidity/syntaxTests/nameAndTypeResolution/439_warn_unused_return_parameter.sol b/test/libsolidity/syntaxTests/nameAndTypeResolution/439_warn_unused_return_parameter.sol index b1422c4f3..b4c9be35c 100644 --- a/test/libsolidity/syntaxTests/nameAndTypeResolution/439_warn_unused_return_parameter.sol +++ b/test/libsolidity/syntaxTests/nameAndTypeResolution/439_warn_unused_return_parameter.sol @@ -3,4 +3,3 @@ contract C { } } // ---- -// Warning: (51-57): Unused function parameter. Remove or comment out the variable name to silence this warning. diff --git a/test/libsolidity/syntaxTests/parsing/empty_function.sol b/test/libsolidity/syntaxTests/parsing/empty_function.sol index 320a0bccd..d10273075 100644 --- a/test/libsolidity/syntaxTests/parsing/empty_function.sol +++ b/test/libsolidity/syntaxTests/parsing/empty_function.sol @@ -3,7 +3,4 @@ contract test { function functionName(bytes20 arg1, address addr) public view returns (int id) { } } // ---- -// Warning: (58-70): Unused function parameter. Remove or comment out the variable name to silence this warning. -// Warning: (72-84): Unused function parameter. Remove or comment out the variable name to silence this warning. -// Warning: (107-113): Unused function parameter. Remove or comment out the variable name to silence this warning. // Warning: (36-118): Function state mutability can be restricted to pure diff --git a/test/libsolidity/syntaxTests/parsing/function_normal_comments.sol b/test/libsolidity/syntaxTests/parsing/function_normal_comments.sol index 94e1e60a4..b61d104f9 100644 --- a/test/libsolidity/syntaxTests/parsing/function_normal_comments.sol +++ b/test/libsolidity/syntaxTests/parsing/function_normal_comments.sol @@ -4,6 +4,4 @@ contract test { function functionName(bytes32 input) public returns (bytes32 out) {} } // ---- -// Warning: (97-110): Unused function parameter. Remove or comment out the variable name to silence this warning. -// Warning: (128-139): Unused function parameter. Remove or comment out the variable name to silence this warning. // Warning: (75-143): Function state mutability can be restricted to pure diff --git a/test/libsolidity/syntaxTests/parsing/function_type_as_storage_variable_with_assignment.sol b/test/libsolidity/syntaxTests/parsing/function_type_as_storage_variable_with_assignment.sol index 11e77f250..c97bf4f99 100644 --- a/test/libsolidity/syntaxTests/parsing/function_type_as_storage_variable_with_assignment.sol +++ b/test/libsolidity/syntaxTests/parsing/function_type_as_storage_variable_with_assignment.sol @@ -3,7 +3,4 @@ contract test { function (uint, uint) internal returns (uint) f1 = f; } // ---- -// Warning: (31-37): Unused function parameter. Remove or comment out the variable name to silence this warning. -// Warning: (39-45): Unused function parameter. Remove or comment out the variable name to silence this warning. -// Warning: (63-69): Unused function parameter. Remove or comment out the variable name to silence this warning. // Warning: (20-73): Function state mutability can be restricted to pure diff --git a/test/libsolidity/syntaxTests/parsing/function_type_in_expression.sol b/test/libsolidity/syntaxTests/parsing/function_type_in_expression.sol index 3defb5ea9..b9ba65fb1 100644 --- a/test/libsolidity/syntaxTests/parsing/function_type_in_expression.sol +++ b/test/libsolidity/syntaxTests/parsing/function_type_in_expression.sol @@ -5,9 +5,6 @@ contract test { } } // ---- -// Warning: (31-37): Unused function parameter. Remove or comment out the variable name to silence this warning. -// Warning: (39-45): Unused function parameter. Remove or comment out the variable name to silence this warning. -// Warning: (63-69): Unused function parameter. Remove or comment out the variable name to silence this warning. // Warning: (108-156): Unused local variable. // Warning: (20-73): Function state mutability can be restricted to pure // Warning: (78-167): Function state mutability can be restricted to pure diff --git a/test/libsolidity/syntaxTests/parsing/multiple_functions_natspec_documentation.sol b/test/libsolidity/syntaxTests/parsing/multiple_functions_natspec_documentation.sol index 85d9e6a8c..c236846cd 100644 --- a/test/libsolidity/syntaxTests/parsing/multiple_functions_natspec_documentation.sol +++ b/test/libsolidity/syntaxTests/parsing/multiple_functions_natspec_documentation.sol @@ -10,14 +10,6 @@ contract test { function functionName4(bytes32 input) public returns (bytes32 out) {} } // ---- -// Warning: (97-110): Unused function parameter. Remove or comment out the variable name to silence this warning. -// Warning: (128-139): Unused function parameter. Remove or comment out the variable name to silence this warning. -// Warning: (203-216): Unused function parameter. Remove or comment out the variable name to silence this warning. -// Warning: (234-245): Unused function parameter. Remove or comment out the variable name to silence this warning. -// Warning: (304-317): Unused function parameter. Remove or comment out the variable name to silence this warning. -// Warning: (335-346): Unused function parameter. Remove or comment out the variable name to silence this warning. -// Warning: (410-423): Unused function parameter. Remove or comment out the variable name to silence this warning. -// Warning: (441-452): Unused function parameter. Remove or comment out the variable name to silence this warning. // Warning: (74-143): Function state mutability can be restricted to pure // Warning: (180-249): Function state mutability can be restricted to pure // Warning: (281-350): Function state mutability can be restricted to pure diff --git a/test/libsolidity/syntaxTests/parsing/single_function_param.sol b/test/libsolidity/syntaxTests/parsing/single_function_param.sol index 955f20f0e..b64fffcec 100644 --- a/test/libsolidity/syntaxTests/parsing/single_function_param.sol +++ b/test/libsolidity/syntaxTests/parsing/single_function_param.sol @@ -3,6 +3,4 @@ contract test { function functionName(bytes32 input) public returns (bytes32 out) {} } // ---- -// Warning: (58-71): Unused function parameter. Remove or comment out the variable name to silence this warning. -// Warning: (89-100): Unused function parameter. Remove or comment out the variable name to silence this warning. // Warning: (36-104): Function state mutability can be restricted to pure