Skip to content

SONARJAVA-6304: Implement S8218: Instant APIs should only use supported temporal units - #6039

Merged
nathsou merged 4 commits into
masterfrom
new-rule/S9366
Aug 26, 2026
Merged

SONARJAVA-6304: Implement S8218: Instant APIs should only use supported temporal units#6039
nathsou merged 4 commits into
masterfrom
new-rule/S9366

Conversation

@nathsou

@nathsou nathsou commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Implement S8218 as a semantic SonarJava check for unsupported ChronoUnit constants used with Instant.
  • Cover all supported and unsupported units, static imports, custom-unit boundaries, and missing semantics.
  • Generate rule metadata and add S8218 to Sonar way.

Links

AI disclosure

  • LLM model used for implementation: gpt-5.6-sol

@nathsou nathsou self-assigned this Aug 25, 2026
@hashicorp-vault-sonar-prod

hashicorp-vault-sonar-prod Bot commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

SONARJAVA-6304

@nathsou nathsou changed the title SONARJAVA-6304: Implement S9366: avoid unsupported ChronoUnit values with Instant SONARJAVA-6304: Implement S8218: Instant APIs should only use supported temporal units Aug 26, 2026
@gitar-bot

gitar-bot Bot commented Aug 26, 2026

Copy link
Copy Markdown
Code Review ✅ Approved 1 resolved / 1 findings

Implements new rule S8218 to check for unsupported ChronoUnit constants used with Instant APIs, addressing the unreachable guard finding in onMethodInvocationFound. No issues found.

✅ 1 resolved
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
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.

Implementation Status ✅ 3 of 3 objectives covered
SONARJAVA-6304 - 3 of 3 objectives covered

This PR covers the implementation of rule S8218 by excluding custom TemporalUnit implementations and variable value propagation, while reporting unsupported ChronoUnit direct constants and static imports for Instant APIs.

✅ 3 covered here
  • ✅ Exclude custom TemporalUnit implementations from reporting
  • ✅ Exclude value propagation through variables in the initial implementation
  • ✅ Report direct enum constants, including statically imported constants, only when semantic resolution confirms the symbol belongs to java.time.temporal.ChronoUnit
Options

Auto-apply is off → Gitar will not commit updates to this branch.
Display: compact → Showing less information.

Comment with these commands to change the behavior for this request:

Auto-apply Compact
gitar auto-apply:on         
gitar display:verbose         

Was this helpful? React with 👍 / 👎 | Gitar

@sonarqube-next

Copy link
Copy Markdown
Contributor

@nathsou
nathsou merged commit 29da966 into master Aug 26, 2026
19 checks passed
@nathsou
nathsou deleted the new-rule/S9366 branch August 26, 2026 13:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants