redundant cast: support switch labels syntax (IDEA-226175)

GitOrigin-RevId: afdf87f8692f610ee87b9892c61f9ab97986bab8
This commit is contained in:
Anna Kozlova
2019-11-08 09:06:35 +00:00
committed by intellij-monorepo-bot
parent 31b347b263
commit f45758ac79
3 changed files with 21 additions and 0 deletions
@@ -646,6 +646,13 @@ public class RedundantCastUtil {
}
}
if (parent instanceof PsiExpressionStatement &&
operand instanceof PsiFunctionalExpression &&
parent.getParent() instanceof PsiSwitchLabeledRuleStatement &&
!castTo.equals(PsiTypesUtil.getExpectedTypeByParent(parent))) {
return;
}
if (arrayAccessAtTheLeftSideOfAssignment(parent, typeCast)) {
if (TypeConversionUtil.isAssignable(opType, castTo, false) && opType.getArrayDimensions() == castTo.getArrayDimensions()) {
addToResults(typeCast);
@@ -0,0 +1,9 @@
import java.util.function.Predicate;
class RedundantCast {
private static void foo(final int matchType) {
Object o = switch (matchType) {
default -> (Predicate<Object>) target -> target == null;
};
}
}
@@ -2,6 +2,7 @@
package com.intellij.java.codeInsight.daemon
import com.intellij.JavaTestUtil
import com.intellij.codeInspection.redundantCast.RedundantCastInspection
import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase
class JavaSwitchExpressionsHighlightingTest : LightJavaCodeInsightFixtureTestCase() {
@@ -17,6 +18,10 @@ class JavaSwitchExpressionsHighlightingTest : LightJavaCodeInsightFixtureTestCas
fun testEnhancedSwitchUnreachable() = doTest()
fun testSwitchExpressionHasResult() = doTest()
fun testYieldStatements() = doTest()
fun testRedundantCastInSwitchBranch() {
myFixture.enableInspections(RedundantCastInspection())
doTest()
}
private fun doTest() {
myFixture.configureByFile("${getTestName(false)}.java")