From c402552b77a9690db59e63201c6fb96f33f5d384 Mon Sep 17 00:00:00 2001 From: Mikhail Pyltsin Date: Mon, 18 Dec 2023 17:23:26 +0100 Subject: [PATCH] [java-inspection] EA-974827 incorrect behavior for EnhancedSwitchMigration when a variable is outside switch scope GitOrigin-RevId: 5c88546b27597aba2b75141eb49439eb1a779dc8 --- .../EnhancedSwitchMigrationInspection.java | 3 +++ .../afterSwitchOutsideScope.java | 13 +++++++++++++ .../beforeSwitchOutsideScope.java | 17 +++++++++++++++++ .../EnhancedSwitchMigrationInspectionTest.java | 2 +- 4 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 java/java-tests/testData/inspection/switchExpressionMigration/afterSwitchOutsideScope.java create mode 100644 java/java-tests/testData/inspection/switchExpressionMigration/beforeSwitchOutsideScope.java diff --git a/java/java-impl/src/com/intellij/codeInspection/EnhancedSwitchMigrationInspection.java b/java/java-impl/src/com/intellij/codeInspection/EnhancedSwitchMigrationInspection.java index 6f1f92c74483..2ab526056762 100644 --- a/java/java-impl/src/com/intellij/codeInspection/EnhancedSwitchMigrationInspection.java +++ b/java/java-impl/src/com/intellij/codeInspection/EnhancedSwitchMigrationInspection.java @@ -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; diff --git a/java/java-tests/testData/inspection/switchExpressionMigration/afterSwitchOutsideScope.java b/java/java-tests/testData/inspection/switchExpressionMigration/afterSwitchOutsideScope.java new file mode 100644 index 000000000000..4bf6d895d7e8 --- /dev/null +++ b/java/java-tests/testData/inspection/switchExpressionMigration/afterSwitchOutsideScope.java @@ -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"; + } + }; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/inspection/switchExpressionMigration/beforeSwitchOutsideScope.java b/java/java-tests/testData/inspection/switchExpressionMigration/beforeSwitchOutsideScope.java new file mode 100644 index 000000000000..907fb1f40087 --- /dev/null +++ b/java/java-tests/testData/inspection/switchExpressionMigration/beforeSwitchOutsideScope.java @@ -0,0 +1,17 @@ +// "Replace with enhanced 'switch' statement" "true-preview" + +class NotDefenitessignment1 { + void test(int x) { + String s = "1"; + Runnable runnable = () -> { + switch (x) { + case 1: + s = "2"; + break; + case 2: + s = "3"; + break; + } + }; + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/codeInspection/EnhancedSwitchMigrationInspectionTest.java b/java/java-tests/testSrc/com/intellij/java/codeInspection/EnhancedSwitchMigrationInspectionTest.java index 8bfa58dba9e1..8b3997199565 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInspection/EnhancedSwitchMigrationInspectionTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInspection/EnhancedSwitchMigrationInspectionTest.java @@ -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;