Repository navigation
SONARJAVA-7152 S1695: Update issue message to suggest Objects.requireNonNull() - #6324
romainbrenguier wants to merge 4 commits into
Conversation
…NonNull() Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
…ign rule description Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
…EC PR #8333) Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
rombirli
left a comment
There was a problem hiding this comment.
the ticket is about adding this rule to sonar-way, which i wasn't able to see in this PR
…as a message argument Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
CI failed: The Ruling Update job failed while building an automated fix PR: applying the generated S1695 ruling patch hit a merge conflict in commons-beanutils expected results, and a second ruling QA job shows no clear failure in its excerpt. Neither failure is shown to be caused by this PR's S1695 message change.OverviewTwo unique error templates were analyzed from 3 logs. One is a concrete tooling failure in the Ruling Update and Notify job, where FailuresRuling fix-PR patch conflict on S1695 expected results (confidence: high)
Ruling QA job with no visible failing step (confidence: low)
Summary
Code Review ✅ Approved 2 closed / 2 findings🟡 Medium risk · Changing S1695 to skip message-bearing NullPointerException throws and revise diagnostics could suppress findings or alter guidance for analyzed code. Updates the S1695 issue message to suggest ✅ 2 closed✅ Quality: New message doesn't fit issues raised on
|
| Auto-apply | Compact |
|
|
Was this helpful? React with 👍 / 👎 | Gitar
|
❌ ErrorsYour PR has failed checks. Please review the issues below and take necessary action before merging. 🚦 3 Pipeline jobs failed
Useful? React with 👍 / 👎 This comment will be updated automatically if new data arrives.🔗 Commit SHA: 7eda71f | Docs | View more details | Give us feedback! |




Updates the S1695 issue message to:
Use Objects.requireNonNull() instead of explicitly throwing NullPointerException.The test sample expectations are updated accordingly.
Issues raised on
throws NullPointerExceptionclauses use a dedicated message:Remove NullPointerException from the throws clause.The rule description (S1695.html) is not changed here. It is being updated on RSPEC in https://github.com/SonarSource/rspec/pull/8333 so that it matches the new message; the metadata will be synced from RSPEC once that PR is merged.
🤖 Generated with Claude Code
Agent workflow
Tool link: https://github.com/SonarSource/languages-experimental-tooling/tree/romain/my-tickets/personal/romain-brenguier
PR created using
uv run create_with_claude.py update the issue message in S1695 to be: Use Objects.requireNonNull() instead of explicitly throwing NullPointerException.PR updated using
uv run update_with_claude.py --prompt "Implement the action plan described in the document Code Review: Updates the S1695 issue message to suggestObjects.requireNonNull()instead of explicitly throwingNullPointerException`, with test sample expectations adjusted to match.throws NullPointerExceptionclauses inNPEThrowCheck.java, which still raise the same issue; a separate message for that case is suggested.@NotNulland never mentionsObjects.requireNonNull(); update the RSPEC metadata in the same change or link an RSPEC update.💡 Quality: New message doesn't fit issues raised on
throws NullPointerExceptionclausesFiles: java-checks/src/main/java/org/sonar/java/checks/NPEThrowCheck.java:45-47, java-checks/src/main/java/org/sonar/java/checks/NPEThrowCheck.java:53, java-checks-test-sources/default/src/main/java/checks/NPEThrowCheckSample.java:4
raiseIssueOnNpeis also called for everythrowsclause entry (NPEThrowCheck.java:45-47), and the sample still checks that case (void foo() throws NullPointerException, line 4). There, the new message "Use Objects.requireNonNull() instead of explicitly throwing NullPointerException." doesn't apply: the method throws nothing explicitly, and callingObjects.requireNonNull()wouldn't clear the issue. The fix is to remove the unchecked exception from thethrowsclause. Consider a separate message for the throws-clause case, e.g. "Remove NullPointerException from the throws clause."Fix (Pass a context-specific message for the throws clause):
@OverRide
public void visitNode(Tree tree) {
if (tree.is(Tree.Kind.THROW_STATEMENT)) {
ExpressionTree expressionTree = ((ThrowStatementTree) tree).expression();
raiseIssueOnNpe(expressionTree, expressionTree.symbolType(), "Use Objects.requireNonNull() instead of explicitly throwing NullPointerException.");
} else {
for (TypeTree throwClause : ((MethodTree) tree).throwsClauses()) {
raiseIssueOnNpe(throwClause, throwClause.symbolType(), "Remove NullPointerException from the throws clause.");
}
}
}
💡 Quality: Issue message contradicts the shipped S1695 rule description
Files: java-checks/src/main/java/org/sonar/java/checks/NPEThrowCheck.java:53
The bundled description (S1695.html) says purposely throwing an NPE is bad practice and that callers should "use the
@NotNullannotation instead". Its compliant example only shows@NotNull. The new message instead tells users to callObjects.requireNonNull(), which purposely throws an NPE. So a user who opens the rule description from the issue gets the opposite advice. Update the RSPEC metadata (S1695.html/json) in the same change, or link the RSPEC update, so the message and the description agree.." -a "Code Review: Updates the S1695 issue message to suggestObjects.requireNonNull()instead of explicitly throwingNullPointerException, with test sample expectations adjusted to match.throws NullPointerExceptionclauses inNPEThrowCheck.java, which still raise the same issue; a separate message for that case is suggested.@NotNulland never mentionsObjects.requireNonNull(); update the RSPEC metadata in the same change or link an RSPEC update.💡 Quality: New message doesn't fit issues raised on
throws NullPointerExceptionclausesFiles: java-checks/src/main/java/org/sonar/java/checks/NPEThrowCheck.java:45-47, java-checks/src/main/java/org/sonar/java/checks/NPEThrowCheck.java:53, java-checks-test-sources/default/src/main/java/checks/NPEThrowCheckSample.java:4
raiseIssueOnNpeis also called for everythrowsclause entry (NPEThrowCheck.java:45-47), and the sample still checks that case (void foo() throws NullPointerException, line 4). There, the new message "Use Objects.requireNonNull() instead of explicitly throwing NullPointerException." doesn't apply: the method throws nothing explicitly, and callingObjects.requireNonNull()wouldn't clear the issue. The fix is to remove the unchecked exception from thethrowsclause. Consider a separate message for the throws-clause case, e.g. "Remove NullPointerException from the throws clause."Fix (Pass a context-specific message for the throws clause):
@OverRide
public void visitNode(Tree tree) {
if (tree.is(Tree.Kind.THROW_STATEMENT)) {
ExpressionTree expressionTree = ((ThrowStatementTree) tree).expression();
raiseIssueOnNpe(expressionTree, expressionTree.symbolType(), "Use Objects.requireNonNull() instead of explicitly throwing NullPointerException.");
} else {
for (TypeTree throwClause : ((MethodTree) tree).throwsClauses()) {
raiseIssueOnNpe(throwClause, throwClause.symbolType(), "Remove NullPointerException from the throws clause.");
}
}
}
💡 Quality: Issue message contradicts the shipped S1695 rule description
Files: java-checks/src/main/java/org/sonar/java/checks/NPEThrowCheck.java:53
The bundled description (S1695.html) says purposely throwing an NPE is bad practice and that callers should "use the
@NotNullannotation instead". Its compliant example only shows@NotNull. The new message instead tells users to callObjects.requireNonNull(), which purposely throws an NPE. So a user who opens the rule description from the issue gets the opposite advice. Update the RSPEC metadata (S1695.html/json) in the same change, or link the RSPEC update, so the message and the description agree." -g "claude"`PR updated using
uv run update_with_claude.py --prompt "Implement the action plan described in the document Revert the changes to the html and instead point the reviewer to PR https://github.com/SonarSource/rspec/pull/8333 on RSPEC.." -a "Revert the changes to the html and instead point the reviewer to PR https://github.com/SonarSource/rspec/pull/8333 on RSPEC." -g "claude"PR updated using
uv run update_with_claude.py --prompt "Implement the action plan described in the document We should not raise when the thrown NullPointerException as a message argument. Add a test case for that case.." -a "We should not raise when the thrown NullPointerException as a message argument. Add a test case for that case." -g "claude"