mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
[java-inspections] Do not report redundant cast if any of labels is incompatible with the selector expression
Fixes IDEA-352510 False positive 'redundant cast' warning in switch GitOrigin-RevId: b31e6f906bebb9de15c82439006502cc8e01f76f
This commit is contained in:
committed by
intellij-monorepo-bot
parent
43cc7451b2
commit
ec42e3be57
@@ -22,6 +22,7 @@ import com.intellij.util.containers.ContainerUtil;
|
||||
import com.siyeh.ig.bugs.NullArgumentToVariableArgMethodInspection;
|
||||
import com.siyeh.ig.psiutils.ExpectedTypeUtils;
|
||||
import com.siyeh.ig.psiutils.SwitchUtils;
|
||||
import one.util.streamex.StreamEx;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@@ -837,7 +838,20 @@ public final class RedundantCastUtil {
|
||||
}
|
||||
}
|
||||
if (opType != null) {
|
||||
addIfNarrowing(cast, opType, null);
|
||||
PsiCodeBlock body = switchBlock.getBody();
|
||||
boolean hasInconvertibleLabel = false;
|
||||
if (body != null) {
|
||||
hasInconvertibleLabel = StreamEx.of(body.getStatements()).select(PsiSwitchLabelStatementBase.class)
|
||||
.map(PsiSwitchLabelStatementBase::getCaseLabelElementList)
|
||||
.nonNull().flatArray(PsiCaseLabelElementList::getElements)
|
||||
.select(PsiExpression.class)
|
||||
.map(PsiExpression::getType)
|
||||
.nonNull()
|
||||
.anyMatch(t -> !t.isConvertibleFrom(opType));
|
||||
}
|
||||
if (!hasInconvertibleLabel) {
|
||||
addIfNarrowing(cast, opType, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
class X {
|
||||
void test() {
|
||||
Integer integer = (int) 'a';
|
||||
|
||||
switch ((int) integer) {
|
||||
case 'a':
|
||||
default:
|
||||
}
|
||||
}
|
||||
}
|
||||
+2
@@ -110,6 +110,8 @@ public class RedundantCastInspectionTest extends LightJavaCodeInsightFixtureTest
|
||||
public void testInConditionalPreserveResolve() { doTest(); }
|
||||
|
||||
public void testArrayAccess() { doTest(); }
|
||||
|
||||
public void testSwitchUnboxing() { doTest(); }
|
||||
|
||||
public void testPackagePrivate() {
|
||||
myFixture.addClass("package a; public class A {void foo() {}}");
|
||||
|
||||
Reference in New Issue
Block a user