Skip to content

Commit ab58698

Browse files
SONARJAVA-6824: Inline sameNullableTree to fix SonarQube always-false condition findings
Inline the null checks for enclosingExpression() directly in sameNewClass so SonarQube can properly track nullability from the @nullable return type. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 74f544a commit ab58698

1 file changed

Lines changed: 5 additions & 7 deletions

File tree

java-checks/src/main/java/org/sonar/java/checks/TernaryOperatorSameOperationCheck.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,17 +113,15 @@ private static boolean sameNewClass(NewClassTree left, NewClassTree right) {
113113
if (left.classBody() != null || right.classBody() != null) {
114114
return false;
115115
}
116-
return sameNullableTree(left.enclosingExpression(), right.enclosingExpression());
117-
}
118-
119-
private static boolean sameNullableTree(Tree left, Tree right) {
120-
if (left == null && right == null) {
116+
var leftEnclosing = left.enclosingExpression();
117+
var rightEnclosing = right.enclosingExpression();
118+
if (leftEnclosing == null && rightEnclosing == null) {
121119
return true;
122120
}
123-
if (left == null || right == null) {
121+
if (leftEnclosing == null || rightEnclosing == null) {
124122
return false;
125123
}
126-
return sameTree(left, right);
124+
return sameTree(leftEnclosing, rightEnclosing);
127125
}
128126

129127
private static boolean hasExactlyOneArgumentDifference(List<? extends ExpressionTree> leftArgs, List<? extends ExpressionTree> rightArgs) {

0 commit comments

Comments
 (0)