Repository navigation
SONARJAVA-7152 S1695: Update issue message to suggest Objects.requireNonNull() - #6324
romainbrenguier wants to merge 6 commits into
Conversation
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
❌ ErrorsYour PR has failed checks. Please review the issues below and take necessary action before merging. 🚦 2 Pipeline jobs failed
Useful? React with 👍 / 👎 This comment will be updated automatically if new data arrives.🔗 Commit SHA: 05e1012 | Docs | View more details | Give us feedback! |
|
We should merge the rspec change first: |
…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>
…as a message argument Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
08d73b8 to
5da5fd6
Compare
|
⚖️ Ruling update ready for review: #6368 |
Ruling Diff SummaryDetected changes in 3 rule files: 16 issues removed, 0 issues added. S1695 (
|
CI failed: One ruling integration test (JavaRulingTest.eclipse_jetty_incremental) failed with a single unexpected issue difference tied to the S1695 change; the other Ruling QA job shows no visible error in its truncated log.OverviewTwo unique error templates were found across 10 analyzed logs. One is a related ruling-test failure; the other job's log is truncated with no confirmed failing step, so its cause is unknown. FailuresJavaRulingTest.eclipse_jetty_incremental expects no differences but finds 1 (confidence: medium)
Ruling QA job log truncated with no visible failing step (confidence: low)
Summary
Code Review ✅ Approved 2 closed / 2 findings🟡 Medium risk · Adding S1695 to Sonar way and suppressing reports for argument-bearing NullPointerException constructors could alter findings and miss manual throws. 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
|




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"PR updated using
uv run update_with_claude.py --prompt "Implement the action plan described in the document Use the rule-api to update the description and metadata for S1695 to the latest version of master on RSPEC.." -a "Use the rule-api to update the description and metadata for S1695 to the latest version of master on RSPEC." -g "claude"PR updated using
uv run update_with_claude.py --prompt "Implement the action plan described in the document Use the rule-api to update the description and metadata for S1695 to the latest version of master on RSPEC.." -a "Use the rule-api to update the description and metadata for S1695 to the latest version of master on RSPEC." -g "claude"PR updated using
uv run update_with_claude.py --prompt "Implement the action plan described in the document Fetch and rebase on origin/master." -a "Fetch and rebase on origin/master" -g "claude"Summary by Gitar
NullPointerExceptionis created with a message argumentS1695to theSonar_wayprofile and changed the remediation cost to5mincertandconventiontags and aCERTERR08-Jsecurity standard mappingS1695.htmlwith new guidance onObjects.requireNonNull(), despite the description saying it is unchangedcommons-beanutils,eclipse-jetty, andguavaThis will update automatically on new commits.