[java-highlighting] IDEA-324705 Non-final variable in guard: provide a fix to rewrite 'if' statement

GitOrigin-RevId: 99770663b569ed2bd6a8d6452e5b0bbe923f9d15
This commit is contained in:
Mikhail Pyltsin
2023-07-11 13:36:46 +02:00
committed by intellij-monorepo-bot
parent aeb1714a62
commit 133cf1e8d2
10 changed files with 49 additions and 0 deletions

View File

@@ -775,6 +775,10 @@ public final class HighlightControlFlowUtil {
HighlightInfo.Builder builder = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(context).descriptionAndTooltip(message);
IntentionAction action = QUICK_FIX_FACTORY.createVariableAccessFromInnerClassFix(variable, refGuardedPattern);
builder.registerFix(action, null, null, null, null);
IntentionAction action2 = QUICK_FIX_FACTORY.createMakeVariableEffectivelyFinalFix(variable);
if (action2 != null) {
builder.registerFix(action2, null, null, null, null);
}
ErrorFixExtensionPoint.registerFixes(builder, context, "guarded.pattern.variable.must.be.final");
return builder;
}

View File

@@ -0,0 +1,15 @@
// "Make 'mode' effectively final by moving initializer to the 'if' statement" "true-preview"
class Test {
void test(Object o) {
int mode;
if (Math.random() > 0.5) {
mode = 3;
} else {
mode = 2;
}
switch (o) {
case Integer i when i == mode -> {}
default -> {}
}
}
}

View File

@@ -0,0 +1,13 @@
// "Make 'mode' effectively final by moving initializer to the 'if' statement" "true-preview"
class Test {
void test(Object o) {
int mode = 2;
if (Math.random() > 0.5) {
mode = 3;
}
switch (o) {
case Integer i when i == mode<caret> -> {}
default -> {}
}
}
}

View File

@@ -0,0 +1,17 @@
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.codeInsight.daemon.impl.quickfix;
import com.intellij.codeInsight.daemon.quickFix.LightQuickFixParameterizedTestCase;
import com.intellij.pom.java.LanguageLevel;
public class VariableAccessFromSwitch21Test extends LightQuickFixParameterizedTestCase {
@Override
protected String getBasePath() {
return "/codeInsight/daemonCodeAnalyzer/quickFix/mustBeFinal21";
}
@Override
protected LanguageLevel getLanguageLevel() {
return LanguageLevel.JDK_21;
}
}