Skip to content

Commit

Permalink
Use sets to avoid duplicates
Browse files Browse the repository at this point in the history
  • Loading branch information
gvladika committed Sep 26, 2024
1 parent 4f7d0bc commit b63e62a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
16 changes: 8 additions & 8 deletions slither/detectors/statements/unused_custom_errors.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
Module detecting unused custom errors
"""
from typing import List
from typing import List, Set
from slither.core.declarations.custom_error import CustomError
from slither.core.declarations.custom_error_top_level import CustomErrorTopLevel
from slither.core.declarations.solidity_variables import SolidityCustomRevert
Expand Down Expand Up @@ -48,32 +48,32 @@ class UnusedCustomErrors(AbstractDetector):

def _detect(self) -> List[Output]:
"""Detect unused custom errors"""
declared_custom_errors: List[CustomError] = []
custom_reverts: List[SolidityCustomRevert] = []
unused_custom_errors: List[CustomError] = []
declared_custom_errors: Set[CustomError] = set()
custom_reverts: Set[SolidityCustomRevert] = set()
unused_custom_errors: Set[CustomError] = set()

# Collect all custom errors defined in the contracts
for contract in self.compilation_unit.contracts:
for custom_error in contract.custom_errors:
declared_custom_errors.append(custom_error)
declared_custom_errors.add(custom_error)

# Add custom errors defined outside of contracts
for custom_error in self.compilation_unit.custom_errors:
declared_custom_errors.append(custom_error)
declared_custom_errors.add(custom_error)

# Collect all custom errors invoked in revert statements
for contract in self.compilation_unit.contracts:
for function in contract.functions_and_modifiers:
for internal_call in function.internal_calls:
if isinstance(internal_call, SolidityCustomRevert):
custom_reverts.append(internal_call)
custom_reverts.add(internal_call)

# Find unused custom errors
for declared_error in declared_custom_errors:
if not any(
declared_error.name in custom_revert.name for custom_revert in custom_reverts
):
unused_custom_errors.append(declared_error)
unused_custom_errors.add(declared_error)

# Format results
results = []
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
The following unused error(s) should be removed:
-UnusedParentErr() (tests/e2e/detectors/test_data/unused-custom-errors/0.8.15/ContractToTestForCustomErrors.sol)

-UnusedParentErrWithArg(uint256) (tests/e2e/detectors/test_data/unused-custom-errors/0.8.15/ContractToTestForCustomErrors.sol)
-UnusedLibError() (tests/e2e/detectors/test_data/unused-custom-errors/0.8.15/CustomErrorsLib.sol)

-UnusedParentErr() (tests/e2e/detectors/test_data/unused-custom-errors/0.8.15/ContractToTestForCustomErrors.sol)
-UnusedErrorLibConsumerErr() (tests/e2e/detectors/test_data/unused-custom-errors/0.8.15/ErrorLibConsumer.sol)

-UnusedParentErrWithArg(uint256) (tests/e2e/detectors/test_data/unused-custom-errors/0.8.15/ContractToTestForCustomErrors.sol)

-UnusedLibError() (tests/e2e/detectors/test_data/unused-custom-errors/0.8.15/CustomErrorsLib.sol)

-UnusedErrorLibConsumerErr() (tests/e2e/detectors/test_data/unused-custom-errors/0.8.15/ErrorLibConsumer.sol)
-UnusedParentErr() (tests/e2e/detectors/test_data/unused-custom-errors/0.8.15/ContractToTestForCustomErrors.sol)

-UnusedTopLevelErr() (tests/e2e/detectors/test_data/unused-custom-errors/0.8.15/ContractToTestForCustomErrors.sol)

0 comments on commit b63e62a

Please sign in to comment.