From efe103c79c100a7a0cd100681b58b477240bed84 Mon Sep 17 00:00:00 2001 From: JorgeAtPaladin Date: Fri, 16 Sep 2022 20:48:44 +0000 Subject: [PATCH] Constants detector: Also detect Contract types --- .../variables/possible_const_state_variables.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/slither/detectors/variables/possible_const_state_variables.py b/slither/detectors/variables/possible_const_state_variables.py index 47af9741b..245f81a91 100644 --- a/slither/detectors/variables/possible_const_state_variables.py +++ b/slither/detectors/variables/possible_const_state_variables.py @@ -3,8 +3,10 @@ Module detecting state variables that could be declared as constant """ from slither.core.compilation_unit import SlitherCompilationUnit from slither.core.solidity_types.elementary_type import ElementaryType +from slither.core.solidity_types.user_defined_type import UserDefinedType from slither.detectors.abstract_detector import AbstractDetector, DetectorClassification from slither.visitors.expression.export_values import ExportValues +from slither.core.declarations import Contract from slither.core.declarations.solidity_variables import SolidityFunction from slither.core.variables.state_variable import StateVariable from slither.formatters.variables.possible_const_state_variables import custom_format @@ -29,9 +31,22 @@ class ConstCandidateStateVars(AbstractDetector): WIKI_DESCRIPTION = "Constant state variables should be declared constant to save gas." WIKI_RECOMMENDATION = "Add the `constant` attributes to state variables that never change." + + @staticmethod + def _is_valid_type(v): + t = v.type + if isinstance(t, ElementaryType): + return True + if isinstance(t, UserDefinedType): + type_t = t.type + if isinstance(type_t, Contract): + return True + return False + + @staticmethod def _valid_candidate(v): - return isinstance(v.type, ElementaryType) and not (v.is_constant or v.is_immutable) + return _is_valid_type(v) and not (v.is_constant or v.is_immutable) # https://solidity.readthedocs.io/en/v0.5.2/contracts.html#constant-state-variables valid_solidity_function = [