SONARJAVA-6304: Implement S9366: avoid unsupported ChronoUnit values with Instant - #6039
SONARJAVA-6304: Implement S9366: avoid unsupported ChronoUnit values with Instant#6039nathsou wants to merge 2 commits into
Conversation
| protected void onMethodInvocationFound(MethodInvocationTree mit) { | ||
| if (context.getSemanticModel() == null || mit.arguments().size() < 2) { | ||
| return; | ||
| } | ||
| ExpressionTree argument = mit.arguments().get(1); |
There was a problem hiding this comment.
💡 Quality: Unreachable guard in onMethodInvocationFound
onMethodInvocationFound is only invoked after MethodMatchers resolved the invocation against java.time.Instant, which requires semantics; with withoutSemantic() the method symbol is unknown and the callback never fires, so context.getSemanticModel() == null can never be true here. Likewise both matchers declare exactly two parameters, so mit.arguments().size() < 2 is never true. The two conditions are unreachable branches (and test_without_semantic does not actually exercise them); dropping them simplifies the check and avoids uncovered branches.
Remove the unreachable semantic/arity guard (the matchers already guarantee both).:
@Override
protected void onMethodInvocationFound(MethodInvocationTree mit) {
ExpressionTree argument = mit.arguments().get(1);
Symbol symbol = referencedSymbol(ExpressionUtils.skipParentheses(argument));
if (isChronoUnitConstant(symbol) && UNSUPPORTED_UNITS.contains(symbol.name())) {
reportIssue(argument, String.format(""%s" is unsupported by Instant and causes an UnsupportedTemporalTypeException.", symbol.name()));
}
}
- Apply fix
Check the box to apply the fix or reply for a change | Was this helpful? React with 👍 / 👎
Code Review 👍 Approved with suggestions 0 resolved / 1 findingsImplements rule S9366 to detect unsupported ChronoUnit values with Instant, complete with thorough test coverage and metadata. Consider removing the unreachable guard in onMethodInvocationFound. 💡 Quality: Unreachable guard in onMethodInvocationFound📄 java-checks/src/main/java/org/sonar/java/checks/UnsupportedChronoUnitWithInstantCheck.java:74-78 📄 java-checks/src/test/java/org/sonar/java/checks/UnsupportedChronoUnitWithInstantCheckTest.java:36-43
Remove the unreachable semantic/arity guard (the matchers already guarantee both).🤖 Prompt for agentsImplementation Status ✅ 4 of 4 objectives covered✅ SONARJAVA-6304 - 4 of 4 objectives coveredThis PR implements the S9366 rule for unsupported ChronoUnit values with Instant, covering all specified objectives including custom unit exclusion, variable propagation restriction, rule creation, and semantic ChronoUnit verification. ✅ 4 covered here
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 |
|




Summary
ChronoUnitconstants used withInstant.Links
AI disclosure