[java] redundant cast: don't suggest to remove cast in switch branches without expected type (IDEA-296622)

GitOrigin-RevId: 24bebef33ad6d01861a68df1470f2ed2f454c4c8
This commit is contained in:
Anna Kozlova
2022-06-24 20:48:12 +00:00
committed by intellij-monorepo-bot
parent bf5d8ffdc8
commit 2fd039b5eb
2 changed files with 13 additions and 3 deletions
@@ -1,4 +1,4 @@
// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.psi.util;
import com.intellij.codeInsight.AnnotationUtil;
@@ -736,8 +736,10 @@ public final class RedundantCastUtil {
visitSwitchBlockSelector(expression);
PsiType expectedTypeByParent = PsiTypesUtil.getExpectedTypeByParent(expression);
for (PsiExpression resultExpression : PsiUtil.getSwitchResultExpressions(expression)) {
addIfNarrowing(resultExpression, expectedTypeByParent);
if (expectedTypeByParent != null) {
for (PsiExpression resultExpression : PsiUtil.getSwitchResultExpressions(expression)) {
addIfNarrowing(resultExpression, expectedTypeByParent);
}
}
super.visitSwitchExpression(expression);
@@ -31,4 +31,12 @@ class RedundantCast {
default -> new ArrayList<>();
};
}
void castForFunctionalExpression(String s) {
(switch (s) {
case "a" -> (Runnable)() -> System.out.println("a");
case "b" -> (Runnable)() -> System.out.println("b");
default -> throw new IllegalArgumentException();
}).run();
}
}