Skip to content

SONARJAVA-7223 Invert negated ternary conditions in OctalValuesCheck permission bits - #6387

Merged
mary-georgiou merged 1 commit into
masterfrom
remediation/5cc36622-e6ce-4e67-80f4-f135846becdd
Oct 22, 2026
Merged

mary-georgiou merged 1 commit into
masterfrom
remediation/5cc36622-e6ce-4e67-80f4-f135846becdd

Conversation

@sonarqube-next

Copy link
Copy Markdown
Contributor

This PR was created because a team member assigned issues to the Remediation Agent.

The read, write and execute permission-bit ternaries in OctalValuesCheck used negated != conditions, forcing readers to mentally invert each test. They now use positive == comparisons with the branches swapped, producing identical output while improving readability and clearing three SonarQube S9435 warnings.

View Project in SonarQube


Fixed Issues

java:S9435 - Invert this negated condition and swap the two branches of the ternary. • MINOR • View issue 1
java:S9435 - Invert this negated condition and swap the two branches of the ternary. • MINOR • View issue 2
java:S9435 - Invert this negated condition and swap the two branches of the ternary. • MINOR • View issue 3

Location: java:java-checks/src/main/java/org/sonar/java/checks/OctalValuesCheck.java:72

Why is this an issue?

A ternary expression packs both outcomes of a choice into a single line, so the reader takes in the condition and the two results at once. When the condition is negated, that reading no longer works in one pass: the reader has to invert the condition mentally before pairing each result with the case it belongs to. In order.status() != Status.SHIPPED ? "Pending" : "Shipped", the value that goes with "shipped" is the second one, even though shipping is what the condition talks about.

What changed

All three permission-bit ternaries used a negated != comparison, which forces the reader to mentally invert the condition before matching each branch to its case. The hunk rewrites each one with the equivalent positive == comparison and swaps the two result expressions ((bits & 4) != 0 ? 'r' : '-' becomes (bits & 4) == 0 ? '-' : 'r', and likewise for the write and execute bits). The produced string is identical because both the condition and the branches were flipped together, and the operands are side-effect-free, so only readability changes and the negated-condition warnings on the read, write and execute ternaries are resolved.

--- a/java-checks/src/main/java/org/sonar/java/checks/OctalValuesCheck.java
+++ b/java-checks/src/main/java/org/sonar/java/checks/OctalValuesCheck.java
@@ -72,3 +72,3 @@ public class OctalValuesCheck extends BaseTreeVisitor implements JavaFileScanner
-      sb.append((bits & 4) != 0 ? 'r' : '-')
-        .append((bits & 2) != 0 ? 'w' : '-')
-        .append((bits & 1) != 0 ? 'x' : '-');
+      sb.append((bits & 4) == 0 ? '-' : 'r')
+        .append((bits & 2) == 0 ? '-' : 'w')
+        .append((bits & 1) == 0 ? '-' : 'x');

SonarQube Remediation Agent uses AI. Check for mistakes.

@sonarqube-next
sonarqube-next Bot requested a review from rombirli October 22, 2026 07:23
@hashicorp-vault-sonar-prod hashicorp-vault-sonar-prod Bot changed the title Invert negated ternary conditions in OctalValuesCheck permission bits SONARJAVA-7223 Invert negated ternary conditions in OctalValuesCheck permission bits Oct 22, 2026
@hashicorp-vault-sonar-prod

hashicorp-vault-sonar-prod Bot commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

SONARJAVA-7223

@gitar-bot

gitar-bot Bot commented Oct 22, 2026 •

Copy link
Copy Markdown
Code Review ✅ Approved

🟢 Low risk · The three permission-bit ternaries only invert equivalent comparisons and swap branches, leaving generated permission strings unchanged.

Inverts the negated != ternaries in OctalValuesCheck permission-bit formatting to positive == comparisons with swapped branches, producing identical output. No issues found.

Verification Rules 2/2 met PRs 1 inspected

Functional validation No results

Rules 2 passed 1 not applicable

✅ Internal artifact registries
✅ Repository license compliance

Broader impact PRs 1 inspected
Cross-PR 1 PR inspected 0 open impacts

PR 6386: SONARJAVA-7222 Fix S9434 and S9435 issues Inspected

Auto-approval Not enabled

Set up

For more verification details, comment gitar display:verbose.

Options

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

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

@rombirli rombirli left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This rule already excludes ... != null conditions, it might make sense to also exclude ... != 0 conditions as well, as fixing it here doesn't improve much the code WDYT?

@mary-georgiou
mary-georgiou requested review from mary-georgiou and removed request for aurelien-coet-sonarsource October 22, 2026 07:27
@mary-georgiou
mary-georgiou enabled auto-merge (squash) October 22, 2026 07:27
@sonarqube-next

Copy link
Copy Markdown
Contributor Author

@mary-georgiou
mary-georgiou merged commit 23e36d3 into master Oct 22, 2026
24 checks passed
@mary-georgiou
mary-georgiou deleted the remediation/5cc36622-e6ce-4e67-80f4-f135846becdd branch October 22, 2026 07:44
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