Repository navigation
SONARJAVA-7156: Implemented rule S9431 Custom exceptions should extend "Exception" or "RuntimeException" rather than "Throwable" - #6331
Conversation
…d "Exception" or "RuntimeException" rather than "Throwable" Detects classes that directly extend java.lang.Throwable and directs developers to extend Exception or RuntimeException instead.
This comment has been minimized.
This comment has been minimized.
| void does_not_report_without_semantic() { | ||
| CheckVerifier.newVerifier() | ||
| .onFile(mainCodeSourcesPath("checks/CustomExceptionExtendsThrowableCheckSample.java")) | ||
| .withCheck(new CustomExceptionExtendsThrowableCheck()) | ||
| .withoutSemantic() | ||
| .verifyIssues(); | ||
| } |
There was a problem hiding this comment.
💡 Quality: Test name says 'does_not_report' but it asserts the issues are reported
does_not_report_without_semantic calls .withoutSemantic().verifyIssues() on the same sample file, which has // Noncompliant markers. verifyIssues() fails unless every marker gets an issue, so this test actually checks that the rule still reports without semantic. I checked why that works: JavaCheckVerifier.withoutSemantic() only nulls the Sema in the scanner context (VisitorsBridgeForTests.createScannerContext), while JDT still resolves java.lang.Throwable in the tree. So the assertion passes, but the name says the opposite of what the test checks, and nothing tests the no-semantic case where the rule should stay silent. Fix: rename the test to match what it checks, like reports_without_semantic. If the goal really was 'no issues without semantic', use verifyNoIssues() instead, as JpaEagerFetchTypeCheckTest does.
Rename the test so the name matches the assertion:
@Test
void reports_without_semantic() {
CheckVerifier.newVerifier()
.onFile(mainCodeSourcesPath("checks/CustomExceptionExtendsThrowableCheckSample.java"))
.withCheck(new CustomExceptionExtendsThrowableCheck())
.withoutSemantic()
.verifyIssues();
}
- Apply fix
Check the box to apply the fix or reply for a change | Was this helpful? React with 👍 / 👎
|
⚖️ Ruling update ready for review: #6335 |
|
/pvf |
|
@romainbrenguier ❌ Your
|
|
⚖️ Ruling update ready for review: #6341 |
Ruling Diff SummaryDetected changes in 2 rule files: 0 issues removed, 2 issues added. S9431 (
|
|
/pvf S9431 |
|
📊 Performance Validation complete |
rombirli
left a comment
There was a problem hiding this comment.
Please fix ruling and +29k LOC added before review
|
Code Review 👍 Approved with suggestions 1 closed / 2 findings🟡 Medium risk · Adding S9431 reports classes directly extending Throwable, which could introduce new findings and affect project quality-gate results. Adds rule S9431 to flag custom exceptions that directly extend 💡 Quality: Test name says 'does_not_report' but it asserts the issues are reported📄 java-checks/src/test/java/org/sonar/java/checks/CustomExceptionExtendsThrowableCheckTest.java:35-41
Rename the test so the name matches the assertion✅ 1 closed✅ Quality: Ruling bot committed ~494 duplicate baselines outside expected/
🤖 Prompt for agentsVerification Rules 1/1 met PRs 1 inspectedFunctional validation No results Rules 1 passed 2 not applicable
Broader impact PRs 1 inspectedCross-PR 1 PR inspected 0 open impactsAuto-approval Not enabledFor more verification details, comment OptionsAuto-apply is off → Gitar will not commit updates to this branch. Comment with these commands to change the behavior for this request:
Was this helpful? React with 👍 / 👎 | Gitar |




Detects classes that directly extend java.lang.Throwable and directs developers to extend Exception or RuntimeException instead.
Agent workflow
PR created using
uv run new_rule_implementation.py S9431 -j SONARJAVA-7156 -a codex -g AGENTS.md .claude/skills/new-rule/SKILL.mdTool link: https://github.com/SonarSource/languages-experimental-tooling/tree/romain/my-tickets/personal/romain-brenguier