[java-inspection] EA-974827 incorrect behavior for EnhancedSwitchMigration when a variable is outside switch scope

GitOrigin-RevId: 5c88546b27597aba2b75141eb49439eb1a779dc8
This commit is contained in:
Mikhail Pyltsin
2023-12-18 20:57:19 +00:00
committed by intellij-monorepo-bot
parent 4d18cc897e
commit c402552b77
4 changed files with 34 additions and 1 deletions
@@ -812,6 +812,9 @@ public final class EnhancedSwitchMigrationInspection extends AbstractBaseJavaLoc
final LocalsOrMyInstanceFieldsControlFlowPolicy policy = LocalsOrMyInstanceFieldsControlFlowPolicy.getInstance();
final ControlFlow controlFlow = ControlFlowFactory.getInstance(declaration.getProject()).getControlFlow(declaration.getParent(), policy);
final int switchStart = controlFlow.getStartOffset(statement);
if (switchStart <= 0) {
return null;
}
final ControlFlow beforeFlow = new ControlFlowSubRange(controlFlow, 0, switchStart);
if (!ControlFlowUtil.isVariableDefinitelyAssigned(assignedVariable, beforeFlow)) {
return null;
@@ -0,0 +1,13 @@
// "Replace with enhanced 'switch' statement" "true-preview"
class NotDefenitessignment1 {
void test(int x) {
String s = "1";
Runnable runnable = () -> {
switch (x) {
case 1 -> s = "2";
case 2 -> s = "3";
}
};
}
}
@@ -0,0 +1,17 @@
// "Replace with enhanced 'switch' statement" "true-preview"
class NotDefenitessignment1 {
void test(int x) {
String s = "1";
Runnable runnable = () -> {
switc<caret>h (x) {
case 1:
s = "2";
break;
case 2:
s = "3";
break;
}
};
}
}
@@ -1,4 +1,4 @@
// Copyright 2000-2018 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-2023 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.
package com.intellij.java.codeInspection;
import com.intellij.codeInsight.daemon.quickFix.LightQuickFixParameterizedTestCase;