diff --git a/slither/detectors/variables/unused_state_variables.py b/slither/detectors/variables/unused_state_variables.py index e23ca6777..7705b75ed 100644 --- a/slither/detectors/variables/unused_state_variables.py +++ b/slither/detectors/variables/unused_state_variables.py @@ -3,6 +3,9 @@ Module detecting unused state variables """ from slither.detectors.abstract_detector import AbstractDetector, DetectorClassification +from slither.core.solidity_types import ArrayType +from slither.visitors.expression.export_values import ExportValues +from slither.core.variables.state_variable import StateVariable class UnusedStateVars(AbstractDetector): """ @@ -26,10 +29,21 @@ class UnusedStateVars(AbstractDetector): if contract.is_signature_only(): return None # Get all the variables read in all the functions and modifiers + + all_functions = (contract.all_functions_called + contract.modifiers) variables_used = [x.state_variables_read + x.state_variables_written for x in - (contract.all_functions_called + contract.modifiers)] + all_functions] + + array_candidates = [x.variables for x in all_functions] + array_candidates = [i for sl in array_candidates for i in sl] + contract.state_variables + array_candidates = [x.type.length for x in array_candidates if isinstance(x.type, ArrayType) and x.type.length] + array_candidates = [ExportValues(x).result() for x in array_candidates] + array_candidates = [i for sl in array_candidates for i in sl] + array_candidates = [v for v in array_candidates if isinstance(v, StateVariable)] + # Flat list variables_used = [item for sublist in variables_used for item in sublist] + variables_used = list(set(variables_used + array_candidates)) # Return the variables unused that are not public return [x for x in contract.variables if x not in variables_used and x.visibility != 'public']