Minor improvements

pull/1660/head
Feist Josselin 2 years ago
parent 24df353955
commit d0c0a17823
  1. 26
      slither/detectors/functions/cyclomatic_complexity.py

@ -1,6 +1,15 @@
from typing import List, Tuple
from slither.core.declarations import Function
from slither.detectors.abstract_detector import AbstractDetector, DetectorClassification
from slither.utils.code_complexity import compute_cyclomatic_complexity
from slither.utils.output import Output
def _check_for_high_cc(high_cc_functions: List[Tuple[Function, int]], f: Function) -> None:
cc = compute_cyclomatic_complexity(f)
if cc > 11:
high_cc_functions.append((f, cc))
class CyclomaticComplexity(AbstractDetector):
@ -15,29 +24,24 @@ class CyclomaticComplexity(AbstractDetector):
WIKI = 'https://github.com/crytic/slither/wiki/Detector-Documentation#cyclomatic-complexity"'
WIKI_TITLE = "Cyclomatic complexity detector"
WIKI_TITLE = "Cyclomatic complexity"
WIKI_DESCRIPTION = "Detects functions with high (> 11) cyclomatic complexity."
WIKI_EXPLOIT_SCENARIO = ""
WIKI_RECOMMENDATION = (
"Reduce cyclomatic complexity by splitting the function into several smaller subroutines."
)
@staticmethod
def _check_for_high_cc(high_cc_functions: list, f: Function):
cc = compute_cyclomatic_complexity(f)
if cc > 11:
high_cc_functions.append((f, cc))
def _detect(self):
def _detect(self) -> List[Output]:
results = []
high_cc_functions = []
high_cc_functions: List[Tuple[Function, int]] = []
f: Function
for c in self.compilation_unit.contracts:
for f in c.functions_declared:
CyclomaticComplexity._check_for_high_cc(high_cc_functions, f)
_check_for_high_cc(high_cc_functions, f)
for f in self.compilation_unit.functions_top_level:
CyclomaticComplexity._check_for_high_cc(high_cc_functions, f)
_check_for_high_cc(high_cc_functions, f)
for f, cc in high_cc_functions:
info = [f, f" has a high cyclomatic complexity ({cc}).\n"]

Loading…
Cancel
Save