|
22 | 22 | import org.sonar.java.model.SyntacticEquivalence; |
23 | 23 | import org.sonar.plugins.java.api.IssuableSubscriptionVisitor; |
24 | 24 | import org.sonar.plugins.java.api.semantic.Symbol; |
| 25 | +import org.sonar.plugins.java.api.semantic.Type; |
25 | 26 | import org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree; |
26 | 27 | import org.sonar.plugins.java.api.tree.ConditionalExpressionTree; |
27 | 28 | import org.sonar.plugins.java.api.tree.ExpressionTree; |
28 | 29 | import org.sonar.plugins.java.api.tree.IdentifierTree; |
| 30 | +import org.sonar.plugins.java.api.tree.LiteralTree; |
29 | 31 | import org.sonar.plugins.java.api.tree.MemberSelectExpressionTree; |
30 | 32 | import org.sonar.plugins.java.api.tree.MethodInvocationTree; |
31 | 33 | import org.sonar.plugins.java.api.tree.NewClassTree; |
32 | 34 | import org.sonar.plugins.java.api.tree.Tree; |
| 35 | +import org.sonar.plugins.java.api.tree.UnaryExpressionTree; |
33 | 36 |
|
34 | 37 | @Rule(key = "S9358") |
35 | 38 | public class TernaryOperatorSameOperationCheck extends IssuableSubscriptionVisitor { |
@@ -130,16 +133,55 @@ private static boolean hasExactlyOneArgumentDifference(List<? extends Expression |
130 | 133 | } |
131 | 134 | int differences = 0; |
132 | 135 | for (int i = 0; i < leftArgs.size(); i++) { |
133 | | - if (!sameTree(leftArgs.get(i), rightArgs.get(i))) { |
| 136 | + ExpressionTree leftArg = leftArgs.get(i); |
| 137 | + ExpressionTree rightArg = rightArgs.get(i); |
| 138 | + if (!sameTree(leftArg, rightArg)) { |
| 139 | + if (!areInterchangeableOperands(leftArg, rightArg)) { |
| 140 | + return false; |
| 141 | + } |
134 | 142 | differences++; |
135 | 143 | } |
136 | 144 | } |
137 | 145 | return differences == 1; |
138 | 146 | } |
139 | 147 |
|
140 | 148 | private static boolean sameArrayAccess(ArrayAccessExpressionTree left, ArrayAccessExpressionTree right) { |
| 149 | + ExpressionTree leftIndex = left.dimension().expression(); |
| 150 | + ExpressionTree rightIndex = right.dimension().expression(); |
141 | 151 | return sameTree(left.expression(), right.expression()) |
142 | | - && !sameTree(left.dimension().expression(), right.dimension().expression()); |
| 152 | + && !sameTree(leftIndex, rightIndex) |
| 153 | + && areInterchangeableOperands(leftIndex, rightIndex); |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * The differing operands can be moved into a conditional expression only when doing so keeps the code readable |
| 158 | + * and does not alter semantics: both operands must be simple (identifiers, literals or field accesses) |
| 159 | + * and have the same type, so that the resulting conditional expression does not involve boxing, unboxing or |
| 160 | + * a different overload resolution. |
| 161 | + */ |
| 162 | + private static boolean areInterchangeableOperands(ExpressionTree left, ExpressionTree right) { |
| 163 | + return isSimpleOperand(left) && isSimpleOperand(right) && sameTypeOrUnknown(left.symbolType(), right.symbolType()); |
| 164 | + } |
| 165 | + |
| 166 | + private static boolean isSimpleOperand(ExpressionTree tree) { |
| 167 | + ExpressionTree expression = ExpressionUtils.skipParentheses(tree); |
| 168 | + if (expression instanceof LiteralTree || expression.is(Tree.Kind.IDENTIFIER)) { |
| 169 | + return true; |
| 170 | + } |
| 171 | + if (expression.is(Tree.Kind.UNARY_MINUS)) { |
| 172 | + return ((UnaryExpressionTree) expression).expression() instanceof LiteralTree; |
| 173 | + } |
| 174 | + if (expression.is(Tree.Kind.MEMBER_SELECT)) { |
| 175 | + return isSimpleOperand(((MemberSelectExpressionTree) expression).expression()); |
| 176 | + } |
| 177 | + return false; |
| 178 | + } |
| 179 | + |
| 180 | + private static boolean sameTypeOrUnknown(Type left, Type right) { |
| 181 | + if (left.isUnknown() || right.isUnknown()) { |
| 182 | + return true; |
| 183 | + } |
| 184 | + return left.equals(right); |
143 | 185 | } |
144 | 186 |
|
145 | 187 | private static boolean sameTree(Tree left, Tree right) { |
|
0 commit comments