mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
java redundant cast: check conditional poly expression after cast is removed (IDEA-251055)
GitOrigin-RevId: 32c2f18de0725a517f1ceb746edc8fa468b6fafa
This commit is contained in:
committed by
intellij-monorepo-bot
parent
08000a2a06
commit
cdbae12245
@@ -874,7 +874,8 @@ public final class RedundantCastUtil {
|
||||
if (!PsiUtil.isLanguageLevel5OrHigher(typeCast)) {
|
||||
return;
|
||||
}
|
||||
if (!PsiPolyExpressionUtil.isPolyExpression(parent)) { //branches need to be of the same type
|
||||
if (!PsiPolyExpressionUtil.isInAssignmentOrInvocationContext(parent) ||
|
||||
PsiPolyExpressionUtil.sameBooleanOrNumeric(operand, oppositeOperand)) { //branches need to be of the same type
|
||||
if (oppositeOperand == null || !Comparing.equal(conditionalType, oppositeOperand.getType())) return;
|
||||
}
|
||||
}
|
||||
|
||||
+9
-1
@@ -73,7 +73,7 @@ public final class PsiPolyExpressionUtil {
|
||||
return false;
|
||||
}
|
||||
|
||||
private static boolean isInAssignmentOrInvocationContext(PsiExpression expr) {
|
||||
public static boolean isInAssignmentOrInvocationContext(PsiExpression expr) {
|
||||
final PsiElement context = PsiUtil.skipParenthesizedExprUp(expr.getParent());
|
||||
return context instanceof PsiExpressionList ||
|
||||
context instanceof PsiArrayInitializerExpression ||
|
||||
@@ -144,6 +144,14 @@ public final class PsiPolyExpressionUtil {
|
||||
private enum ConditionalKind {
|
||||
BOOLEAN, NUMERIC, NULL
|
||||
}
|
||||
|
||||
public static boolean sameBooleanOrNumeric(PsiExpression thenExpression, PsiExpression elseExpression) {
|
||||
final ConditionalKind thenKind = isBooleanOrNumeric(thenExpression);
|
||||
final ConditionalKind elseKind = isBooleanOrNumeric(elseExpression);
|
||||
if (thenKind == elseKind || elseKind == ConditionalKind.NULL) return thenKind != null;
|
||||
if (thenKind == ConditionalKind.NULL) return elseKind != null;
|
||||
return false;
|
||||
}
|
||||
|
||||
private static ConditionalKind isBooleanOrNumeric(PsiExpression expr) {
|
||||
if (expr instanceof PsiParenthesizedExpression) {
|
||||
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
class MyTest {
|
||||
private static Object foo(String s, boolean b) {
|
||||
return b ? (Object) Double.parseDouble(s) : (Object) Long.parseLong(s);
|
||||
}
|
||||
}
|
||||
+1
@@ -100,6 +100,7 @@ public class LambdaRedundantCastTest extends LightDaemonAnalyzerTestCase {
|
||||
public void testLambdaReturnChain() { doTest(); }
|
||||
public void testExpectedTypeProducesBadReturnType() { doTest(); }
|
||||
public void testNonDenotableParent() { doTest(); }
|
||||
public void testToBeNonPolyConditional() { doTest(); }
|
||||
|
||||
private void doTest() {
|
||||
doTest(BASE_PATH + "/" + getTestName(false) + ".java", true, false);
|
||||
|
||||
Reference in New Issue
Block a user