Repository navigation
SONARJAVA-7241: Improve S9358 to only raise when differing operands are simple and have the same type - #6402
Conversation
…simple and have the same type Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Ruling Diff SummaryDetected changes in 4 rule files: 14 issues removed, 0 issues added. S9358 (
|
|
|
||
| private static boolean sameType(Type left, Type right) { | ||
| if (left.isUnknown() || right.isUnknown()) { | ||
| return true; |
There was a problem hiding this comment.
Returning true could be challenged here.
If we have no information about the types, it would be less risky to return false.
I'm not sure how often type information is missing though so this is your call.
There was a problem hiding this comment.
Renamed to sameTypeOrUnknown to be clearer.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
|
Code Review ✅ Approved🟡 Medium risk · Restricting S9358 to simple, same-typed differing operands could suppress findings and alter reported issue counts in analyzed projects. Narrows S9358 to raise only when the single differing operand in both branches is a simple identifier, literal, negative literal, or field access chain with the same type, avoiding readability and semantic regressions from complex arguments. No issues found. Verification Rules 2/2 met Repos 4 inspectedFunctional validation No results Rules 2 passed 1 not applicable
Broader impact Repos 4 inspectedCross-repo 4 repositories inspected 0 open impacts
Auto-approval Not enabledFor more verification details, comment OptionsAuto-apply is off → Gitar will not commit updates to this branch. Comment with these commands to change the behavior for this request:
Was this helpful? React with 👍 / 👎 | Gitar |




Summary
S9358 (Conditional expressions should not duplicate operations in both branches) raised many issues where moving the condition inside the operation would hurt readability or change semantics.
The rule now only raises when the single differing argument (or array index) in both branches:
this.x,a.b). Method invocations, lambdas, method references, casts, binary expressions, object creations... are no longer considered.Ruling
Removed issues (all involving complex arguments: nested calls, lambdas, casts):
RegularImmutableTablex2,TreeMultiset,MessageDigestHashFunctionQuotedQualityCSV,MBeanContainerx2,CounterStatisticTestConditionEvaluator,RuleActivator,ValuesActionKept:
JavaVersion(version[0]vsversion[1]) andQualityGateConditionsUpdater(differing string literals).🤖 Generated with Claude Code
Agent workflow
Tool link: https://github.com/SonarSource/languages-experimental-tooling/tree/romain/my-tickets/personal/romain-brenguier
PR created using
uv run create_with_claude.py SONARJAVA-7241.txtPR updated using
uv run update_with_claude.py --prompt "Implement the action plan described in the document Can we either make sameType return a ternary value or rename it sameTypeOrUnknown?." -a "Can we either make sameType return a ternary value or rename it sameTypeOrUnknown?" -g "claude"