[java-inspection] IDEA-310567 'Constant values' false positive for switch expression

- skip 'constant values' for switch expressions if they contain always failed methods

GitOrigin-RevId: 1207911bd11f94f37293254fa609e47b42aa96e1
This commit is contained in:
Mikhail Pyltsin
2024-11-21 14:58:08 +00:00
committed by intellij-monorepo-bot
parent 708b5271f6
commit 5b7611c45e
2 changed files with 26 additions and 1 deletions
@@ -1,4 +1,4 @@
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.codeInspection.dataFlow;
import com.intellij.codeInsight.daemon.impl.quickfix.SimplifyBooleanExpressionFix;
@@ -400,6 +400,13 @@ public final class ConstantValueInspection extends AbstractBaseJavaLocalInspecti
ref.set(true);
return false;
}
if (element instanceof PsiMethodCallExpression methodCallExpression) {
List<? extends MethodContract> contracts = JavaMethodContractUtil.getMethodCallContracts(methodCallExpression);
if (ContainerUtil.exists(contracts, contract -> contract.isTrivial() && contract.getReturnValue().isFail())) {
ref.set(true);
return false;
}
}
return true;
});
return ref.get();
@@ -1,3 +1,5 @@
import org.jetbrains.annotations.Nullable;
class SkipSwitchExpressionWithThrow {
static boolean test(int x) {
return switch (x) {
@@ -7,4 +9,20 @@ class SkipSwitchExpressionWithThrow {
default -> throw new IllegalStateException();
};
}
public enum Foo {
A, B
}
public static <R> R exception() {
throw new RuntimeException();
}
@Nullable
public static Object test2(Foo foo) {
return switch (foo) {
case A -> null;
case B -> exception();
};
}
}