mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-04-19 04:51:24 +07:00
[java-highlighting] IDEA-325375 Exhaustive switch expressions can produce definitely assigned variables
GitOrigin-RevId: 013d2367e1fa08189272343af83c5af9c2ad0582
This commit is contained in:
committed by
intellij-monorepo-bot
parent
04b6c1513b
commit
82b6cf4e7d
@@ -409,7 +409,7 @@ public abstract class DataFlowInspectionBase extends AbstractBaseJavaLocalInspec
|
||||
if (unreachableElements.isEmpty() || hasDefault) {
|
||||
return unreachableElements;
|
||||
}
|
||||
boolean isEnhancedSwitch = JavaPsiSwitchUtil.isEnhancedSwitchStatement(statement) || statement instanceof PsiSwitchExpression;
|
||||
boolean isEnhancedSwitch = JavaPsiSwitchUtil.isEnhancedSwitch(statement);
|
||||
if (isEnhancedSwitch) {
|
||||
PsiExpression expression = statement.getExpression();
|
||||
if (expression == null) {
|
||||
|
||||
@@ -960,7 +960,8 @@ final class ControlFlowAnalyzer extends JavaElementVisitor {
|
||||
PsiCodeBlock body = statement.getBody();
|
||||
if (body != null) {
|
||||
PsiStatement[] statements = body.getStatements();
|
||||
boolean needToCreateDefault = JavaPsiSwitchUtil.isEnhancedSwitchStatement(statement);
|
||||
//16.2.9 (for statements) and 16.1.6 (for expressions)
|
||||
boolean needToCreateDefault = JavaPsiSwitchUtil.isEnhancedSwitch(statement);
|
||||
PsiType exprType = expr == null ? null : expr.getType();
|
||||
for (PsiStatement aStatement : statements) {
|
||||
ProgressManager.checkCanceled();
|
||||
|
||||
@@ -15,13 +15,13 @@ import java.util.List;
|
||||
public final class JavaPsiSwitchUtil {
|
||||
|
||||
/**
|
||||
* Checks if the given switch statement is enhanced.
|
||||
* Checks if the given switch is enhanced.
|
||||
*
|
||||
* @param statement the switch statement to check
|
||||
* @return true if the switch statement is an enhanced switch statement, false otherwise
|
||||
* @param statement the switch to check
|
||||
* @return true if the switch is an enhanced switch, false otherwise
|
||||
*/
|
||||
public static boolean isEnhancedSwitchStatement(@NotNull PsiSwitchBlock statement) {
|
||||
if(!(statement instanceof PsiSwitchStatement)) return false;
|
||||
public static boolean isEnhancedSwitch(@NotNull PsiSwitchBlock statement) {
|
||||
if(statement instanceof PsiSwitchExpression) return true;
|
||||
|
||||
PsiExpression selector = statement.getExpression();
|
||||
if (selector == null) {
|
||||
|
||||
@@ -238,4 +238,13 @@ class C {
|
||||
};
|
||||
System.out.println(<error descr="Variable 'i' might not have been initialized">i</error>);
|
||||
}
|
||||
|
||||
private void testExpressions1(T obj) {
|
||||
int i;
|
||||
int y = switch (obj) {
|
||||
case T1 t1 -> {i = 1; yield 1;}
|
||||
case T2 t2 -> {i = 2; yield 2;}
|
||||
};
|
||||
System.out.println(i);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user