Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

fix(health): exempt Java try ... fail() expected-exception idiom from swallowed_catch (#2996)

Rishabh Mishra863a958a43d4
diff --git a/packages/core/src/repowise/core/analysis/health/complexity/error_handling.py b/packages/core/src/repowise/core/analysis/health/complexity/error_handling.py
index c7c2591e9..7fc5d37a3 100644
--- a/packages/core/src/repowise/core/analysis/health/complexity/error_handling.py
+++ b/packages/core/src/repowise/core/analysis/health/complexity/error_handling.py
@@ -231,6 +231,29 @@ def _eh_go_hit(node: Node) -> bool:
return False
+def _eh_java_try_ends_in_fail(catch_clause: Node, language: str) -> bool:
+ """True when the enclosing Java try block's last real statement is a fail(...) call."""
+ parent = catch_clause.parent
+ if parent is None or parent.type not in ("try_statement", "try_with_resources_statement"):
+ return False
+ try_body = parent.child_by_field_name("body")
+ if try_body is None:
+ return False
+ real = _eh_real_stmts(try_body, language)
+ if not real:
+ return False
+ last = real[-1]
+ if last.type != "expression_statement":
+ return False
+ inv = last.child_by_field_name("expression") or (
+ _eh_named(last)[0] if _eh_named(last) else None
+ )
+ if inv is None or inv.type != "method_invocation":
+ return False
+ name_node = inv.child_by_field_name("name")
+ return name_node is not None and _eh_text(name_node) == "fail"
+
+
# The node types ``_eh_rust_hit`` / ``_eh_go_hit`` can fire on.
_EH_RUST_KINDS = frozenset({"call_expression", "macro_invocation"})
_EH_GO_KINDS = frozenset({"if_statement", "short_var_declaration", "assignment_statement"})
@@ -262,7 +285,11 @@ def _eh_visit(
catch_kinds = lmap.catch_kinds
if catch_kinds and node.type in catch_kinds:
block = _eh_find_body_block(node)
- if block is not None and _eh_body_is_swallowed(block, language):
+ if (
+ block is not None
+ and _eh_body_is_swallowed(block, language)
+ and not (language == "java" and _eh_java_try_ends_in_fail(node, language))
+ ):
hits.append(ErrorHandlingHit("swallowed_catch", node.start_point[0] + 1))
if (
language == "python"
diff --git a/tests/unit/health/test_error_handling.py b/tests/unit/health/test_error_handling.py
index bb8f67d8e..8aadac989 100644
--- a/tests/unit/health/test_error_handling.py
+++ b/tests/unit/health/test_error_handling.py
@@ -68,6 +68,30 @@ _FIXTURES = [
{"swallowed_catch": 1},
"empty Java catch",
),
+ (
+ "java",
+ b"class A { void m(){ try { go(); fail(\"x\"); } catch (Exception e) {} } }\n",
+ {},
+ "Java try ending in fail() -> expected exception idiom, not swallowed",
+ ),
+ (
+ "java",
+ b"class A { void m(){ try { go(); Assert.fail(\"x\"); } catch (Exception e) { // expected\n } } }\n",
+ {},
+ "Java try ending in Assert.fail() -> clean",
+ ),
+ (
+ "java",
+ b"class A { void m(){ try (var r = open()) { go(); Assertions.fail(\"x\"); } catch (Exception e) {} } }\n",
+ {},
+ "Java try-with-resources ending in Assertions.fail() -> clean",
+ ),
+ (
+ "java",
+ b"class A { void m(){ try { fail(\"x\"); go(); } catch (Exception e) {} } }\n",
+ {"swallowed_catch": 1},
+ "Java fail() not last in try -> swallowed",
+ ),
(
"java",
b"class A { void m(){ try { go(); } catch (Exception e) { log(e); } } }\n",