Skip to content

SONARJAVA-7162 Implement rule S9435 - Ternary operator conditions should not be negated - #6353

Open
rombirli wants to merge 4 commits into
masterfrom
rombirli/implement-ternary-negated-condition
Open

rombirli wants to merge 4 commits into
masterfrom
rombirli/implement-ternary-negated-condition

Conversation

@rombirli

Copy link
Copy Markdown
Contributor

Implements rule S9435, which raises on a ternary expression whose condition is negated (!x or x != y), since the reader has to invert the condition mentally before pairing each result with the case it belongs to.

Null checks are exempt: value != null ? value.name() : "unknown" reads naturally as a positive guard, and so does its long form !(value == null). Only the top-level condition is considered, so value != null && count != 0 ? ... : ... does not raise. if statements are out of scope by design, as decided on the RSPEC.

RSPEC: https://github.com/SonarSource/rspec/pull/8337 (branch rule/add-RSPEC-S9435, not merged yet)
Jira: https://sonarsource.atlassian.net/browse/SONARJAVA-7162

@hashicorp-vault-sonar-prod

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

Copy link
Copy Markdown
Contributor

SONARJAVA-7162

@datadog-sonarsource

This comment has been minimized.

@github-actions

Copy link
Copy Markdown
Contributor

⚖️ Ruling update ready for review: #6355

@github-actions

Copy link
Copy Markdown
Contributor

Ruling Diff Summary

Detected changes in 1 rule files: 0 issues removed, 3 issues added.

S9435 (java) on sonarqube-6.5 - 0 issues removed, 3 issues added - new ruling file

Added src/main/java/org/sonar/server/debt/DebtRulesXMLImporter.java (line 254)

(source file not found at this revision: src/main/java/org/sonar/server/debt/DebtRulesXMLImporter.java)

Added src/test/java/org/sonar/server/issue/IssueDocTesting.java (line 69)

(source file not found at this revision: src/test/java/org/sonar/server/issue/IssueDocTesting.java)

Added src/test/java/org/sonar/server/issue/IssueDocTesting.java (line 74)

(source file not found at this revision: src/test/java/org/sonar/server/issue/IssueDocTesting.java)

@gitar-bot

gitar-bot Bot commented Oct 14, 2026

Copy link
Copy Markdown
Code Review ✅ Approved

🟡 Medium risk · Adding S9435 flags negated ternary conditions in analyzed projects, which could introduce new issues and affect quality-gate results.

Adds rule S9435 to flag ternary expressions whose top-level condition is negated (!x or x != y), excluding null checks such as value != null ? ... : ... and their !(value == null) form. No issues found.

Verification Repos 3 inspected

Functional validation No results

Rules No rules evaluated

Cross-repo 3 repositories inspected 0 open impacts

SonarSource/sonarlint-language-server Inspected
SonarSource/sonarlint-vscode Inspected
SonarSource/sonar-java-symbolic-execution 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

@sonarqube-next

Copy link
Copy Markdown
Contributor

@rombirli

Copy link
Copy Markdown
Contributor Author

/pvf

@hashicorp-vault-sonar-prod

Copy link
Copy Markdown
Contributor

📊 Performance Validation complete
🆕 557 new · 🗑️ 0 lost issues
View dashboard

@hashicorp-vault-sonar-prod

Copy link
Copy Markdown
Contributor

📊 Performance Validation complete
🆕 8 new · 🗑️ 0 lost issues
View dashboard

@hashicorp-vault-sonar-prod

Copy link
Copy Markdown
Contributor

📊 Performance Validation complete
🆕 557 new · 🗑️ 0 lost issues
View dashboard

This branch has not been deployed

No deployments
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.

1 participant