Skip to content

SONARJAVA-6345 Create rule S8695: Redundant time instantiation patterns should be simplified#5635

Merged
alban-auzeill merged 6 commits into
masterfrom
alban/SONARJAVA-6345
May 27, 2026
Merged

SONARJAVA-6345 Create rule S8695: Redundant time instantiation patterns should be simplified#5635
alban-auzeill merged 6 commits into
masterfrom
alban/SONARJAVA-6345

Conversation

@alban-auzeill
Copy link
Copy Markdown
Member

@alban-auzeill alban-auzeill commented May 27, 2026


Summary by Gitar

  • New rule:
    • Added SimpleTemporalInstantiationCheck (S8695) to simplify verbose java.time object instantiations.
    • Detects patterns like LocalDate.from(Instant.now()) that should be replaced with LocalDate.now().
  • Rule updates:
    • Changed rule title to "Redundant time instantiation patterns should be simplified".
    • Reduced default severity from "Major" to "Minor" and maintainability impact from "MEDIUM" to "LOW".
  • Quick fix improvements:
    • Added and refined quick-fix definitions in test samples for now() and now(clock) patterns.
    • Fixed quick-fix mapping index in test cases for ZonedDateTime and OffsetDateTime expressions.
  • Documentation:
    • Updated HTML rule description and examples to better clarify supported patterns and usage.

This will update automatically on new commits.

@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud Bot commented May 27, 2026

Agentic Analysis: Early Results

Agentic Analysis and Context Augmentation are available on your project. Here are some issues that could have been prevented. Follow the links to learn how to put them into action.

12 issue(s) found across 1 file(s):

Rule File Line Message
java:S1118 java-checks-test-sources/default/src/main/java/checks/SimpleTemporalInstantiationCheckSample.java 14 Add a private constructor to hide the implicit public one.
java:S139 java-checks-test-sources/default/src/main/java/checks/SimpleTemporalInstantiationCheckSample.java 21 Move this trailing comment on the previous empty line.
java:S125 java-checks-test-sources/default/src/main/java/checks/SimpleTemporalInstantiationCheckSample.java 23 This block of commented-out lines of code should be removed.
java:S139 java-checks-test-sources/default/src/main/java/checks/SimpleTemporalInstantiationCheckSample.java 33 Move this trailing comment on the previous empty line.
java:S139 java-checks-test-sources/default/src/main/java/checks/SimpleTemporalInstantiationCheckSample.java 39 Move this trailing comment on the previous empty line.
java:S125 java-checks-test-sources/default/src/main/java/checks/SimpleTemporalInstantiationCheckSample.java 41 This block of commented-out lines of code should be removed.
java:S125 java-checks-test-sources/default/src/main/java/checks/SimpleTemporalInstantiationCheckSample.java 52 This block of commented-out lines of code should be removed.
java:S139 java-checks-test-sources/default/src/main/java/checks/SimpleTemporalInstantiationCheckSample.java 52 Move this trailing comment on the previous empty line.
java:S139 java-checks-test-sources/default/src/main/java/checks/SimpleTemporalInstantiationCheckSample.java 55 Move this trailing comment on the previous empty line.
java:S125 java-checks-test-sources/default/src/main/java/checks/SimpleTemporalInstantiationCheckSample.java 57 This block of commented-out lines of code should be removed.
java:S139 java-checks-test-sources/default/src/main/java/checks/SimpleTemporalInstantiationCheckSample.java 61 Move this trailing comment on the previous empty line.
java:S125 java-checks-test-sources/default/src/main/java/checks/SimpleTemporalInstantiationCheckSample.java 63 This block of commented-out lines of code should be removed.

Analyzed by SonarQube Agentic Analysis in 5.4 s

@hashicorp-vault-sonar-prod
Copy link
Copy Markdown
Contributor

hashicorp-vault-sonar-prod Bot commented May 27, 2026

SONARJAVA-6345

Comment thread sonar-java-plugin/src/main/resources/org/sonar/l10n/java/rules/java/S8695.json Outdated
@alban-auzeill alban-auzeill changed the title SONARJAVA-6345 Create rule S8695: Simplify calls to .from(Instant.now()) with .now() SONARJAVA-6345 Create rule S8695: Redundant time instantiation patterns should be simplified May 27, 2026
@sonarqube-next
Copy link
Copy Markdown

@alban-auzeill alban-auzeill merged commit d0b1347 into master May 27, 2026
15 checks passed
@alban-auzeill alban-auzeill deleted the alban/SONARJAVA-6345 branch May 27, 2026 17:06
@gitar-bot
Copy link
Copy Markdown

gitar-bot Bot commented May 27, 2026

Code Review ✅ Approved 4 resolved / 4 findings

Implements rule S8695 to simplify redundant java.time instantiations, with refined quick-fixes and corrected test assertions for ZonedDateTime and OffsetDateTime patterns. No outstanding issues remain.

✅ 4 resolved
Bug: quickfix field should be "targeted" not "unknown"

📄 sonar-java-plugin/src/main/resources/org/sonar/l10n/java/rules/java/S8695.json:17
The rule provides quickfixes (evidenced by [[quickfixes=...]] annotations in the test sample and JavaQuickFix usage in the check), but S8695.json declares "quickfix": "unknown". Other rules with quickfixes use "quickfix": "targeted" (e.g., S7481, S6397, S8696). This may affect how the IDE/SonarQube reports the availability of quickfixes to users.

Bug: Issue message is inaccurate for Instant.now() case (no ZoneId)

📄 java-checks/src/main/java/org/sonar/java/checks/SimpleTemporalInstantiationCheck.java:72
The message at line 72 is hardcoded as "Replace "from(TemporalAccessor)" with "now(ZoneId)"." but when the detected pattern is Type.from(Instant.now()), the replacement is now() (without a ZoneId parameter). The message misleadingly suggests a ZoneId is needed. Consider making the message dynamic based on whether a zone argument is present, e.g., using the already-computed replacement variable.

Bug: Incorrect quickfix for Instant.now(clock).atZone(zoneId) pattern

📄 java-checks/src/main/java/org/sonar/java/checks/SimpleTemporalInstantiationCheck.java:54 📄 java-checks/src/main/java/org/sonar/java/checks/SimpleTemporalInstantiationCheck.java:83-91 📄 java-checks/src/main/java/org/sonar/java/checks/SimpleTemporalInstantiationCheck.java:70
Adding addParametersMatcher("java.time.Clock") to INSTANT_NOW (line 54) means the atZone branch in isNonCompliantMethod (lines 87-90) now also matches Instant.now(clock).atZone(zoneId). The replacement logic at line 70 only extracts the atZone argument (zoneId), producing now(zoneId). But LocalDate.from(Instant.now(clock).atZone(zoneId)) is NOT semantically equivalent to LocalDate.now(zoneId) — the former uses a specific clock while the latter uses the system clock.

This means the rule would suggest a semantically incorrect transformation. Either:

  1. The atZone branch should only match when the inner Instant.now() has no arguments (no clock), or
  2. The replacement logic should detect the clock and produce a different suggestion (though there's no single equivalent call).
Bug: Stray @ in // @fix@qf2 silently skips quickfix test

📄 java-checks-test-sources/default/src/main/java/checks/SimpleTemporalInstantiationCheckSample.java:57
Line 57 uses // @fix@qf2 {{…}} but the test framework's regex expects // fix@<id> {{…}} (pattern: //\s*fix@(?<id>\S+)\s+\{\{(?<message>.*)\}\}). The leading @ prevents the annotation from being parsed, so the quickfix message for qf2 is never verified. This was a pre-existing typo, but since the line was modified in this commit (renumbered qf1→qf2), it should be fixed now.

Options

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

Comment with these commands to change:

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

Was this helpful? React with 👍 / 👎 | Gitar

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