Skip to content

Commit

Permalink
Unused variable: Check for array length expressions (fix #167)
Browse files Browse the repository at this point in the history
  • Loading branch information
montyly committed Feb 8, 2019
1 parent 3dbe807 commit 0ad0777
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion slither/detectors/variables/unused_state_variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"""

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):
"""
Expand All @@ -26,10 +29,21 @@ def detect_unused(self, contract):
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']
Expand Down

0 comments on commit 0ad0777

Please sign in to comment.