Skip to content

SONARJAVA-6778: Implement S9393: "Duration.get()" should only be called with SECONDS or NANOS - #6138

Merged
nathsou merged 3 commits into
masterfrom
new-rule/S9393
Sep 15, 2026
Merged

nathsou merged 3 commits into
masterfrom
new-rule/S9393

Conversation

@nathsou

@nathsou nathsou commented Sep 14, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Implement rule S9393: "Duration.get()" should only be called with SECONDS or NANOS.
  • Add DurationGetTemporalUnitCheck, accompanying tests, and test samples.
  • Add generated rule metadata and activate in Sonar_way quality profile.

Links

AI disclosure

  • LLM model used for implementation: gemini-3.8-flash-high

@nathsou nathsou self-assigned this Sep 14, 2026
@hashicorp-vault-sonar-prod

hashicorp-vault-sonar-prod Bot commented Sep 14, 2026

Copy link
Copy Markdown
Contributor

SONARJAVA-6778

@gitar-bot

gitar-bot Bot commented Sep 15, 2026

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

Implements rule S9393 to detect invalid Duration.get() calls with temporal units other than SECONDS or NANOS. The custom TemporalUnit exemption was addressed, and test coverage extended to catch violations on custom enum implementations. No open issues remain.

✅ 2 resolved
Bug: Custom TemporalUnit exemption rests on a false JDK claim

📄 sonar-java-plugin/src/main/resources/org/sonar/l10n/java/rules/java/S9393.html:1-2 📄 sonar-java-plugin/src/main/resources/org/sonar/l10n/java/rules/java/S9393.html:12 📄 java-checks/src/main/java/org/sonar/java/checks/DurationGetTemporalUnitCheck.java:68 📄 java-checks/src/main/java/org/sonar/java/checks/DurationGetTemporalUnitCheck.java:83-89 📄 java-checks-test-sources/default/src/main/java/checks/DurationGetTemporalUnitCheckSample.java:75-80 🔗 Duration.get throws UnsupportedTemporalTypeException for any unit other than SECONDS/NANOS
The rule description states "Custom TemporalUnit implementations are not reported because Duration delegates the operation to them" — a sentence carried over from S8218, where it is true for Instant.plus/minus/until (those methods do unit.addTo(this, amount) for non-ChronoUnit units). Duration.get(TemporalUnit) never delegates: it compares unit == SECONDS / unit == NANOS and otherwise throws UnsupportedTemporalTypeException, so a custom unit always throws. This makes S9393.html:12 contradict its own opening paragraph ("supports only ChronoUnit.SECONDS and ChronoUnit.NANOS. Passing any other TemporalUnit value causes an UnsupportedTemporalTypeException"), and it makes the sample assert guaranteed-throwing calls as compliant (DurationGetTemporalUnitCheckSample.java:77-78 duration.get(CustomUnit.CUSTOM_MINUTES)), because isChronoUnitConstant() only accepts constants owned by ChronoUnit. Fix the description rationale (RSPEC side), and ideally extend detection to any enum constant of a TemporalUnit subtype other than ChronoUnit.SECONDS/NANOS so these certain runtime failures are reported.

Quality: Message/fix guidance not extended to custom TemporalUnit enums

📄 java-checks/src/main/java/org/sonar/java/checks/DurationGetTemporalUnitCheck.java:68 📄 java-checks/src/main/java/org/sonar/java/checks/DurationGetTemporalUnitCheck.java:95 📄 sonar-java-plugin/src/main/resources/org/sonar/l10n/java/rules/java/S9393.html:11-18
The delta extends detection to any enum implementing TemporalUnit (line 95), so duration.get(CustomUnit.CUSTOM_MINUTES) is now reported, yet the issue message still says "use dedicated conversion methods instead" and the "How to fix it" section only offers toMinutes()/toHours()/ChronoUnit.SECONDS remedies — none of which apply to a custom unit (there is no conversion method for it, and the correct fix is to stop calling Duration.get with that unit or to use unit.between(...)/getDuration()). A developer hitting the issue on a custom unit gets remediation advice that cannot be followed; extend the RSPEC "How to fix it" text (and regenerate S9393.html) to cover custom TemporalUnit implementations.

Review coverage

Functional validation 1 of 1 objectives covered

Rules No rules evaluated

Auto-approval Not enabled · Set up

Implementation Status ✅ 1 of 1 objectives covered
SONARJAVA-6778 - 1 of 1 objectives covered

This PR implements rule S9393 to detect when Duration.get() is called with unsupported temporal units.

✅ 1 covered here
  • ✅ Implement rule S9393 to detect when "Duration.get()" is called with units other than SECONDS or NANOS
Options

Auto-apply is off → Gitar will not commit updates to this branch.
Display: compact → Counting what did not apply, without listing it.

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 ac4b509 into master Sep 15, 2026
16 checks passed
@nathsou
nathsou deleted the new-rule/S9393 branch September 15, 2026 09:15
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