Skip to content

Commit

Permalink
feat(build): #16 enhance try cognitive complexity
Browse files Browse the repository at this point in the history
- Consider the case when multiple exceptions are
caught in a single try block.
- Update the test case to cover the new feature.
  • Loading branch information
rohaquinlop committed Mar 4, 2024
1 parent b8bba38 commit a1d30a2
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 10 deletions.
31 changes: 28 additions & 3 deletions src/cognitive_complexity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,29 @@ fn statement_cognitive_complexity(statement: Stmt, nesting_level: u64) -> PyResu
match statement {
Stmt::FunctionDef(f) => {
for node in f.body.iter() {
complexity += statement_cognitive_complexity(node.clone(), nesting_level)?;
match node {
Stmt::FunctionDef(..) | Stmt::AsyncFunctionDef(..) => {
complexity +=
statement_cognitive_complexity(node.clone(), nesting_level + 1)?;
}
_ => {
complexity += statement_cognitive_complexity(node.clone(), nesting_level)?;
}
}
// complexity += statement_cognitive_complexity(node.clone(), nesting_level)?;
}
}
Stmt::AsyncFunctionDef(f) => {
for node in f.body.iter() {
complexity += statement_cognitive_complexity(node.clone(), nesting_level)?;
match node {
Stmt::FunctionDef(..) | Stmt::AsyncFunctionDef(..) => {
complexity +=
statement_cognitive_complexity(node.clone(), nesting_level + 1)?;
}
_ => {
complexity += statement_cognitive_complexity(node.clone(), nesting_level)?;
}
}
}
}
Stmt::ClassDef(c) => {
Expand Down Expand Up @@ -203,12 +220,12 @@ fn statement_cognitive_complexity(statement: Stmt, nesting_level: u64) -> PyResu
}
}
Stmt::Try(t) => {
complexity += 1;
for node in t.body.iter() {
complexity += statement_cognitive_complexity(node.clone(), nesting_level + 1)?;
}

for handler in t.handlers.iter() {
complexity += 1;
match handler {
ast::ExceptHandler::ExceptHandler(e) => {
for node in e.body.iter() {
Expand All @@ -218,6 +235,14 @@ fn statement_cognitive_complexity(statement: Stmt, nesting_level: u64) -> PyResu
}
}
}

for node in t.orelse.iter() {
complexity += statement_cognitive_complexity(node.clone(), nesting_level + 1)?;
}

for node in t.finalbody.iter() {
complexity += statement_cognitive_complexity(node.clone(), nesting_level + 1)?;
}
}
Stmt::Match(m) => {
for case in m.cases.iter() {
Expand Down
26 changes: 26 additions & 0 deletions tests/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
@atatus.capture_span()
def k8s_check_host_pid(
shard: GraphShard, method_supplies: MethodSupplies
) -> MethodExecutionResult:
method = MethodsEnum.K8S_CHECK_HOST_PID

def n_ids() -> Iterator[NId]:
graph = shard.syntax_graph
integrity = check_template_integrity_for_all_nodes(
graph, method_supplies.selected_nodes
)
if integrity: # 2 (nested = 1)
for nid in method_supplies.selected_nodes: # 3 (nested = 2)
for c_id in get_host_pid(graph, nid): # 4 (nested = 3)
yield c_id

return get_vulnerabilities_from_n_ids(
n_ids=n_ids(),
query_supplies=QuerySupplies(
desc_key="f267.k8s_check_host_pid",
desc_params={},
method=method,
method_calls=len(method_supplies.selected_nodes),
graph_shard=shard,
),
)
14 changes: 7 additions & 7 deletions tests/test_try_nested.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ def test_try():
try:
a += 1
b += 1
except TypeError:
if a is None:
except TypeError: # 1 (nested = 0)
if a is None: # 2 (nested = 1)
print("Caught a TypeError")
except ValueError:
if b is None:
except ValueError: # 1 (nested = 0)
if b is None: # 2 (nested = 1)
print("Caught a ValueError")
except Exception as e:
except Exception as e: # 1 (nested = 0)
print(f"Caught an exception: {str(e)}")
else:
if a is not None and b is not None:
if a is not None and b is not None: # 2 + 1 (nested = 1)
print("No exception occurred")
finally:
if a:
if a: # 2 (nested = 1)
print("Always executed")

0 comments on commit a1d30a2

Please sign in to comment.