Skip to content

SONARJAVA-6781 Implement new rule S9344: Bitwise AND operations with zero should be corrected - #5952

Merged
romainbrenguier merged 3 commits into
masterfrom
new-rule/SONARJAVA-6781-S9344
Aug 19, 2026
Merged

SONARJAVA-6781 Implement new rule S9344: Bitwise AND operations with zero should be corrected#5952
romainbrenguier merged 3 commits into
masterfrom
new-rule/SONARJAVA-6781-S9344

Conversation

@romainbrenguier

Copy link
Copy Markdown
Contributor

Detect bitwise AND operations with literal zero (& 0 and &= 0), which always produce zero regardless of the other operand, indicating a likely programming error such as a wrong constant or wrong operator.

Part of

Detect bitwise AND operations with literal zero (`& 0` and `&= 0`),
which always produce zero regardless of the other operand, indicating
a likely programming error such as a wrong constant or wrong operator.
@hashicorp-vault-sonar-prod

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

Copy link
Copy Markdown
Contributor

SONARJAVA-6781

S9344 adds one rule to the quality profile, incrementing the total from 465 to 466.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@romainbrenguier
romainbrenguier marked this pull request as ready for review August 19, 2026 08:10

@nathsou nathsou 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.

Automated code review found a few issues worth addressing before merge.

}
}

private static boolean isZero(ExpressionTree expression) {

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.

isZero() fails to detect a parenthesized zero literal, so flags & (0) is not flagged (false negative). LiteralUtils.longLiteralValue() only unwraps UNARY_MINUS/UNARY_PLUS before checking for INT_LITERAL/LONG_LITERAL — it never unwraps PARENTHESIZED_EXPRESSION. The codebase already has ExpressionUtils.skipParentheses used by other checks (e.g. StringConcatToTextBlockCheck, MathOnFloatCheck, UselessMathematicalComparisonCheck) for exactly this purpose. Also not covered by the test sample.

}

@Override
public void visitNode(Tree tree) {

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.

Issue location is inconsistent between the two branches: the & case reports the whole binary expression (reportIssue(tree, MESSAGE)), while the &= case reports only the operator token (reportIssue(assignment.operatorToken(), MESSAGE), line 47). The sibling rule S2437 (UnnecessaryBitOperationCheck) uses operatorToken() uniformly for both binary and assignment forms — worth aligning here too.

assertThat(actualProfile.isDefault()).isFalse();
assertThat(actualProfile.rules())
.hasSize(465)
.hasSize(466)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It's added by the rule-api tool. We will be doing a re-run of rule-api for all rules before the release anyway.

assertThat(actualProfile.isDefault()).isFalse();
assertThat(actualProfile.rules())
.hasSize(465)
.hasSize(466)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It's added by the rule-api tool. We will be doing a re-run of rule-api for all rules before the release anyway.

…tion

- Wrap expression with ExpressionUtils.skipParentheses() in isZero()
  to detect parenthesized zero literals like `flags & (0)`
- Report issue on operator token instead of whole expression for binary
  `&` to be consistent with `&=` case and sibling rule S2437
- Add test cases for parenthesized zero (compliant and noncompliant)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@sonarqube-next

Copy link
Copy Markdown
Contributor

@romainbrenguier
romainbrenguier merged commit 9b33bd7 into master Aug 19, 2026
16 checks passed
@romainbrenguier
romainbrenguier deleted the new-rule/SONARJAVA-6781-S9344 branch August 19, 2026 13:46
@gitar-bot

gitar-bot Bot commented Aug 19, 2026

Copy link
Copy Markdown
Code Review ✅ Approved

Implements rule S9344 to detect bitwise AND operations with literal zero and updates the expected rule count in agentic profile tests. No issues found.

Implementation Status ✅ 1 / 1 issues implemented
SONARJAVA-6781 — 1 / 1 objectives

The PR successfully implements the new rule S9344 to check for bitwise AND operations with zero.

✅ 1 complete
  • ✅ Implement new rule S9344: Bitwise AND operations with zero should be corrected
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

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