mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
[java] IDEA-283208 Add quickfix: Lift 'throw' out of 'switch' expression
Adds a quick-fix: "Lift 'throw' out of 'switch' expression" that that is provided for error "'switch' expression does not have any result expressions". Some rare cases are not handled (fix is not provided for mentioned error). For example when switch is used inside: - initialization/condition/update part of for loop - initializer of class field - condition of do-while loop - lock expression in synchronized statement - any part of assert statement - resource initializer of 'try' statement #IDEA-283208 Fixed Merge-request: IJ-MR-172146 Merged-by: Marcin Mikosik <marcin.mikosik@jetbrains.com> GitOrigin-RevId: a2710c49e16b7f495974fb167bcae01b3d850f7b
This commit is contained in:
committed by
intellij-monorepo-bot
parent
d09c7efa6a
commit
9fd6ada970
@@ -527,4 +527,5 @@ public abstract class QuickFixFactory {
|
||||
|
||||
public abstract @Nullable ModCommandAction createRecordThisDelegateFix(PsiMethod psi);
|
||||
|
||||
public abstract @Nullable CommonIntentionAction createLiftThrowOutOfSwitchExpression(@NotNull PsiSwitchExpression psiSwitchExpression);
|
||||
}
|
||||
@@ -458,4 +458,5 @@ add.missing.nested.patterns.fix.text=Add missing nested pattern{0, choice, 1#|2#
|
||||
|
||||
add.missing.str.processor=Add 'STR.' Processor
|
||||
|
||||
record.delegate.to.canonical.constructor.fix.name=Delegate to canonical constructor
|
||||
record.delegate.to.canonical.constructor.fix.name=Delegate to canonical constructor
|
||||
lift.throw.out.of.switch.expression.fix.name=Lift 'throw' out of 'switch' expression
|
||||
+3
-1
@@ -14,7 +14,6 @@ import com.intellij.codeInspection.dataFlow.fix.RedundantInstanceofFix;
|
||||
import com.intellij.core.JavaPsiBundle;
|
||||
import com.intellij.ide.highlighter.JavaFileType;
|
||||
import com.intellij.java.analysis.JavaAnalysisBundle;
|
||||
import com.intellij.java.codeserver.core.JavaPsiMethodUtil;
|
||||
import com.intellij.java.codeserver.core.JavaPsiModifierUtil;
|
||||
import com.intellij.java.codeserver.core.JavaPsiSwitchUtil;
|
||||
import com.intellij.java.codeserver.core.JpmsModuleAccessInfo;
|
||||
@@ -250,6 +249,9 @@ public final class DefaultJavaErrorFixProvider extends AbstractJavaErrorFixProvi
|
||||
}
|
||||
return null;
|
||||
});
|
||||
fix(SWITCH_EXPRESSION_NO_RESULT, error -> {
|
||||
return myFactory.createLiftThrowOutOfSwitchExpression(error.psi());
|
||||
});
|
||||
fix(SYNTAX_ERROR, error -> error.psi().getParent() instanceof PsiSwitchLabeledRuleStatement rule &&
|
||||
error.psi().getErrorDescription().equals(JavaPsiBundle.message("expected.switch.rule"))
|
||||
? myFactory.createWrapSwitchRuleStatementsIntoBlockFix(rule)
|
||||
|
||||
+257
@@ -0,0 +1,257 @@
|
||||
// Copyright 2000-2025 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.QuickFixBundle;
|
||||
import com.intellij.modcommand.ActionContext;
|
||||
import com.intellij.modcommand.ModPsiUpdater;
|
||||
import com.intellij.modcommand.PsiUpdateModCommandAction;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.util.containers.SmartHashSet;
|
||||
import com.siyeh.ig.psiutils.CommentTracker;
|
||||
import com.siyeh.ig.psiutils.SideEffectChecker;
|
||||
import com.siyeh.ig.psiutils.StatementExtractor;
|
||||
import org.jetbrains.annotations.Nls;
|
||||
import org.jetbrains.annotations.NotNullByDefault;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import static com.intellij.codeInsight.BlockUtils.expandSingleStatementToBlockStatement;
|
||||
|
||||
/**
|
||||
* This class provides a quick fix to lift a throw expression out of a switch expression
|
||||
* when all execution branches of the switch expression end with a throw statement.
|
||||
*/
|
||||
@NotNullByDefault
|
||||
public class LiftThrowOutOfSwitchExpressionFix extends PsiUpdateModCommandAction<PsiSwitchExpression> {
|
||||
public static @Nullable LiftThrowOutOfSwitchExpressionFix create(PsiSwitchExpression switchExpression) {
|
||||
return isAvailable(switchExpression) ? new LiftThrowOutOfSwitchExpressionFix(switchExpression) : null;
|
||||
}
|
||||
|
||||
private static boolean isAvailable(PsiSwitchExpression switchExpression) {
|
||||
PsiStatement enclosingStatement = enclosingStatementOf(switchExpression);
|
||||
if (enclosingStatement == null) return false;
|
||||
// Fix is not available when switch is inside for's initializer OR condition OR update.
|
||||
if (enclosingStatement.getParent() instanceof PsiForStatement) return false;
|
||||
if (enclosingStatement instanceof PsiDeclarationStatement declarationStatement) {
|
||||
PsiElement[] elements = declarationStatement.getDeclaredElements();
|
||||
return 0 < elements.length && elements[0] instanceof PsiLocalVariable;
|
||||
}
|
||||
if (isInsideConditionalExpression(switchExpression)) return false;
|
||||
return (topmostExpressionOf(enclosingStatement) != null);
|
||||
}
|
||||
|
||||
private static boolean isInsideConditionalExpression(PsiElement element) {
|
||||
return PsiTreeUtil.getParentOfType(element, PsiStatement.class, PsiConditionalExpression.class) instanceof PsiConditionalExpression;
|
||||
}
|
||||
|
||||
private LiftThrowOutOfSwitchExpressionFix(PsiSwitchExpression expression) {
|
||||
super(expression);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nls(capitalization = Nls.Capitalization.Sentence) String getFamilyName() {
|
||||
return QuickFixBundle.message("lift.throw.out.of.switch.expression.fix.name");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void invoke(ActionContext context, PsiSwitchExpression switchExpression, ModPsiUpdater updater) {
|
||||
PsiCodeBlock body = switchExpression.getBody();
|
||||
if (body == null) return;
|
||||
PsiElementFactory factory = JavaPsiFacade.getElementFactory(context.project());
|
||||
replaceThrowStatementsInsideSwitchBodyScope(body, factory);
|
||||
|
||||
PsiStatement enclosingStatement = enclosingStatementOf(switchExpression);
|
||||
if (enclosingStatement == null) return;
|
||||
|
||||
if (enclosingStatement instanceof PsiDeclarationStatement declarationStatement) {
|
||||
replaceDeclarationStatementWithThrowStatement(declarationStatement, switchExpression, factory, updater);
|
||||
}
|
||||
else {
|
||||
PsiExpression mainExpression = topmostExpressionOf(enclosingStatement);
|
||||
if (mainExpression != null) {
|
||||
replaceStatementWithThrowStatement(enclosingStatement, mainExpression, switchExpression, factory, updater);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static @Nullable PsiStatement enclosingStatementOf(PsiSwitchExpression switchExpression) {
|
||||
return PsiTreeUtil.getParentOfType(switchExpression, PsiStatement.class);
|
||||
}
|
||||
|
||||
private static void replaceThrowStatementsInsideSwitchBodyScope(PsiCodeBlock body, PsiElementFactory factory) {
|
||||
for (PsiStatement statement : body.getStatements()) {
|
||||
if (statement instanceof PsiSwitchLabeledRuleStatement ruleStatement) {
|
||||
PsiStatement ruleBody = ruleStatement.getBody();
|
||||
if (ruleBody instanceof PsiThrowStatement throwStatement) {
|
||||
replaceThrowStatementWithExceptionExpression(throwStatement, factory);
|
||||
}
|
||||
else if (ruleBody != null) {
|
||||
replaceThrowStatementsWithYieldStatements(ruleBody, factory);
|
||||
}
|
||||
}
|
||||
else {
|
||||
replaceThrowStatementsWithYieldStatements(statement, factory);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void replaceThrowStatementWithExceptionExpression(PsiThrowStatement throwStatement, PsiElementFactory factory) {
|
||||
PsiExpression exception = throwStatement.getException();
|
||||
CommentTracker commentTracker = new CommentTracker();
|
||||
String exceptionText = exception != null ? commentTracker.text(exception) : "";
|
||||
commentTracker.replaceAndRestoreComments(throwStatement, factory.createStatementFromText(exceptionText + ";", throwStatement));
|
||||
}
|
||||
|
||||
private static void replaceThrowStatementsWithYieldStatements(PsiElement element, PsiElementFactory factory) {
|
||||
for (PsiThrowStatement throwStatement : collectThrowStatements(element)) {
|
||||
replaceThrowStatementWithYieldStatement(throwStatement, factory);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all throw statements that are contained in the scope of the psiElement
|
||||
* excluding those from scopes of nested classes, lambda expressions, switch expressions.
|
||||
*/
|
||||
private static Set<PsiThrowStatement> collectThrowStatements(PsiElement psiElement) {
|
||||
SmartHashSet<PsiThrowStatement> throwStatements = new SmartHashSet<>();
|
||||
if (psiElement instanceof PsiThrowStatement throwStatement) {
|
||||
throwStatements.add(throwStatement);
|
||||
return throwStatements;
|
||||
}
|
||||
var visitor = new JavaRecursiveElementWalkingVisitor() {
|
||||
@Override
|
||||
public void visitThrowStatement(PsiThrowStatement throwStatement) {
|
||||
throwStatements.add(throwStatement);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitClass(PsiClass aClass) { }
|
||||
|
||||
@Override
|
||||
public void visitSwitchExpression(PsiSwitchExpression expression) { }
|
||||
|
||||
@Override
|
||||
public void visitLambdaExpression(PsiLambdaExpression expression) { }
|
||||
};
|
||||
visitor.visitElement(psiElement);
|
||||
return throwStatements;
|
||||
}
|
||||
|
||||
private static void replaceThrowStatementWithYieldStatement(PsiThrowStatement throwStatement, PsiElementFactory factory) {
|
||||
PsiExpression exception = throwStatement.getException();
|
||||
var commentTracker = new CommentTracker();
|
||||
var exceptionText = exception != null ? commentTracker.text(exception) : "";
|
||||
var yieldText = "yield " + exceptionText + ";";
|
||||
var yieldStatement = factory.createStatementFromText(yieldText, throwStatement);
|
||||
var newYield = commentTracker.replaceAndRestoreComments(throwStatement, yieldStatement);
|
||||
if (newYield.getParent() instanceof PsiSwitchLabeledRuleStatement) {
|
||||
expandSingleStatementToBlockStatement((PsiStatement)newYield);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces the enclosingStatement with (in order)
|
||||
* 1) all side effects of the enclosingStatement that precede switchExpression within the enclosingStatement
|
||||
* 2) a throw statement with the switchExpression
|
||||
*/
|
||||
private static void replaceDeclarationStatementWithThrowStatement(PsiDeclarationStatement enclosingStatement,
|
||||
PsiSwitchExpression switchExpression,
|
||||
PsiElementFactory factory,
|
||||
ModPsiUpdater updater) {
|
||||
var enclosingVariable = PsiTreeUtil.getParentOfType(switchExpression, PsiLocalVariable.class);
|
||||
for (PsiElement element : enclosingStatement.getDeclaredElements()) {
|
||||
if (element == enclosingVariable) {
|
||||
PsiExpression initializer = enclosingVariable.getInitializer();
|
||||
if (initializer != null) {
|
||||
replaceStatementWithThrowStatement(enclosingStatement, initializer, switchExpression, factory, updater);
|
||||
}
|
||||
break;
|
||||
}
|
||||
else {
|
||||
if (element instanceof PsiLocalVariable variable) {
|
||||
var variableDeclaration =
|
||||
factory.createVariableDeclarationStatement(variable.getName(), variable.getType(), variable.getInitializer());
|
||||
enclosingStatement.getParent().addBefore(variableDeclaration, enclosingStatement);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static @Nullable PsiExpression topmostExpressionOf(PsiStatement statement) {
|
||||
if (statement instanceof PsiReturnStatement returnStatement) {
|
||||
return returnStatement.getReturnValue();
|
||||
}
|
||||
else if (statement instanceof PsiExpressionStatement expressionStatement) {
|
||||
return expressionStatement.getExpression();
|
||||
}
|
||||
else if (statement instanceof PsiThrowStatement throwStatement) {
|
||||
return throwStatement.getException();
|
||||
}
|
||||
else if (statement instanceof PsiIfStatement ifStatement) {
|
||||
return ifStatement.getCondition();
|
||||
}
|
||||
else if (statement instanceof PsiWhileStatement whileStatement) {
|
||||
return whileStatement.getCondition();
|
||||
}
|
||||
else if (statement instanceof PsiForeachStatementBase foreachStatementBase) {
|
||||
return foreachStatementBase.getIteratedValue();
|
||||
}
|
||||
else if (statement instanceof PsiYieldStatement yieldStatement) {
|
||||
return yieldStatement.getExpression();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces the enclosingStatement with (in order):
|
||||
* 1) all side effects of the enclosingStatement that precede switchExpression within the enclosingStatement
|
||||
* 2) a throw statement with the switchExpression
|
||||
*/
|
||||
private static void replaceStatementWithThrowStatement(PsiStatement enclosingStatement,
|
||||
PsiExpression enclosingExpression,
|
||||
PsiSwitchExpression switchExpression,
|
||||
PsiElementFactory factory, ModPsiUpdater updater) {
|
||||
var precedingExpressions = findAllPrecedingExpressionsInsideAncestor(switchExpression, enclosingExpression);
|
||||
var expressions = SideEffectChecker.extractSideEffectExpressions(enclosingExpression, e -> !precedingExpressions.contains(e));
|
||||
PsiStatement[] statements = StatementExtractor.generateStatements(expressions, enclosingExpression);
|
||||
|
||||
var commentTracker = new CommentTracker();
|
||||
var throwText = "throw " + commentTracker.text(switchExpression) + ";";
|
||||
var throwStatement =
|
||||
commentTracker.replaceAndRestoreComments(enclosingStatement, factory.createStatementFromText(throwText, switchExpression));
|
||||
if (!(throwStatement.getParent() instanceof PsiCodeBlock)) {
|
||||
throwStatement = expandSingleStatementToBlockStatement((PsiStatement)throwStatement);
|
||||
}
|
||||
for (PsiStatement statement : statements) {
|
||||
throwStatement.getParent().addBefore(statement, throwStatement);
|
||||
}
|
||||
PsiExpression exception = ((PsiThrowStatement)throwStatement).getException();
|
||||
if (exception != null) {
|
||||
updater.moveCaretTo(exception.getTextOffset());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all expressions of the ancestor that are executed in runtime before the descendant.
|
||||
*/
|
||||
private static Set<PsiExpression> findAllPrecedingExpressionsInsideAncestor(PsiElement descendant,
|
||||
PsiElement ancestor) {
|
||||
Set<PsiExpression> set = new SmartHashSet<>();
|
||||
PsiElement current = descendant;
|
||||
while (true) {
|
||||
var preceding = current.getPrevSibling();
|
||||
while (preceding == null) {
|
||||
var parent = current.getParent();
|
||||
if (parent == null || parent == ancestor) return set;
|
||||
current = parent;
|
||||
preceding = parent.getPrevSibling();
|
||||
}
|
||||
if (preceding instanceof PsiExpression psiExpression) {
|
||||
set.add(psiExpression);
|
||||
}
|
||||
current = preceding;
|
||||
}
|
||||
}
|
||||
}
|
||||
+6
@@ -1158,4 +1158,10 @@ public final class QuickFixFactoryImpl extends QuickFixFactory {
|
||||
ReplaceOnDemandImportIntention.replaceOnDemand(element);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable IntentionAction createLiftThrowOutOfSwitchExpression(@NotNull PsiSwitchExpression psiSwitchExpression) {
|
||||
LiftThrowOutOfSwitchExpressionFix fix = LiftThrowOutOfSwitchExpressionFix.create(psiSwitchExpression);
|
||||
return fix != null ? fix.asIntention() : null;
|
||||
}
|
||||
}
|
||||
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "true-preview"
|
||||
|
||||
class Foo {
|
||||
int bar() {
|
||||
throw <caret>switch (7) {
|
||||
default -> {
|
||||
new Runnable() {
|
||||
public void run() {
|
||||
throw new RuntimeException("runnable");
|
||||
}
|
||||
}.run();
|
||||
yield new RuntimeException("message");
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "true-preview"
|
||||
|
||||
class Foo {
|
||||
int bar() {
|
||||
throw <caret>switch (7) {
|
||||
default -> {
|
||||
int x = switch (5) {
|
||||
case 1:
|
||||
throw new RuntimeException("one");
|
||||
case 2:
|
||||
yield 3;
|
||||
default:
|
||||
throw new RuntimeException("default");
|
||||
};
|
||||
yield new RuntimeException("message");
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "true-preview"
|
||||
|
||||
class Foo {
|
||||
int bar() {
|
||||
throw <caret>switch (7) {
|
||||
default -> {
|
||||
Runnable runnable = () -> {
|
||||
throw new RuntimeException("runnable");
|
||||
};
|
||||
runnable.run();
|
||||
yield new RuntimeException("message");
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "true-preview"
|
||||
|
||||
class Foo {
|
||||
int bar() {
|
||||
throw <caret>switch (7) {
|
||||
default -> {
|
||||
class LocalClass {
|
||||
public void fooBar() {
|
||||
throw new RuntimeException("local classs");
|
||||
}
|
||||
}
|
||||
yield new RuntimeException("message");
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "true-preview"
|
||||
|
||||
class Foo {
|
||||
int bar() {
|
||||
// BEFORE STATEMENT
|
||||
/* INSIDE 0*/
|
||||
throw <caret>switch /*INSIDE 1*/ (7) {
|
||||
// BEFORE FIRST
|
||||
case 1 -> new /*INSIDE 2*/ RuntimeException("one"); // TRAILING
|
||||
// BETWEEN
|
||||
default -> {
|
||||
// BEFORE
|
||||
yield new /*INSIDE 3*/ RuntimeException("default"); // TRAILING
|
||||
// AFTER
|
||||
}
|
||||
// AFTER LAST
|
||||
};
|
||||
// AFTER STATEMENT
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "true-preview"
|
||||
|
||||
class Foo {
|
||||
int bar() {
|
||||
throw <caret>switch (7) {
|
||||
default -> {
|
||||
{
|
||||
yield new RuntimeException("then");
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "true-preview"
|
||||
|
||||
class Foo {
|
||||
int bar() {
|
||||
throw <caret>switch (7) {
|
||||
default -> {
|
||||
for (int i = 0; i < new java.util.Random().nextInt(); i++) {
|
||||
yield new RuntimeException("for");
|
||||
}
|
||||
yield new RuntimeException("default");
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "true-preview"
|
||||
|
||||
class Foo {
|
||||
int bar() {
|
||||
throw <caret>switch (7) {
|
||||
default -> {
|
||||
if (true) {
|
||||
yield new RuntimeException("then");
|
||||
} else {
|
||||
yield new RuntimeException("else");
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "true-preview"
|
||||
|
||||
class Foo {
|
||||
int bar() {
|
||||
throw <caret>switch (7) {
|
||||
default -> {
|
||||
switch (5) {
|
||||
case 1:
|
||||
yield new RuntimeException("one");
|
||||
default:
|
||||
yield new RuntimeException("default");
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "true-preview"
|
||||
|
||||
class Foo {
|
||||
int bar(int param) {
|
||||
throw switch (param) {
|
||||
default:
|
||||
switch (param) {
|
||||
case 1 -> {
|
||||
yield new RuntimeException();
|
||||
}
|
||||
default -> {
|
||||
yield new RuntimeException();
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "true-preview"
|
||||
|
||||
class Foo {
|
||||
enum Two {
|
||||
ONE, TWO;
|
||||
}
|
||||
int bar(Two param) {
|
||||
throw <caret>switch (param) {
|
||||
case ONE -> {
|
||||
yield new RuntimeException("1");
|
||||
}
|
||||
case TWO -> {
|
||||
yield new RuntimeException("2");
|
||||
}
|
||||
default -> {
|
||||
yield new RuntimeException("default");
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "true-preview"
|
||||
|
||||
class Foo {
|
||||
enum Three {
|
||||
ONE, TWO;
|
||||
}
|
||||
int bar(Three param) {
|
||||
throw <caret>switch (param) {
|
||||
case ONE -> new RuntimeException("1");
|
||||
case TWO -> new RuntimeException("2");
|
||||
default -> new RuntimeException("default");
|
||||
};
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "true-preview"
|
||||
|
||||
class Foo {
|
||||
int bar(int param, boolean b) {
|
||||
throw <caret>switch (param) {
|
||||
case 1:
|
||||
if (b) {
|
||||
yield new RuntimeException("one");
|
||||
}
|
||||
default:
|
||||
yield new RuntimeException("default");
|
||||
};
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "true-preview"
|
||||
|
||||
class Foo {
|
||||
int bar() {
|
||||
throw <caret>switch (7) {
|
||||
default -> {
|
||||
while (new java.util.Random().nextBoolean()) {
|
||||
yield new RuntimeException("while");
|
||||
}
|
||||
yield new RuntimeException("default");
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "true-preview"
|
||||
|
||||
class Foo {
|
||||
int bar(int param) {
|
||||
throw <caret>switch (param) {
|
||||
default -> new RuntimeException("default");
|
||||
};
|
||||
}
|
||||
int a() {return 0;}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "true-preview"
|
||||
|
||||
class Foo {
|
||||
int bar(int param) {
|
||||
a();
|
||||
throw <caret>switch (param) {
|
||||
default -> new RuntimeException("default");
|
||||
};
|
||||
}
|
||||
int a() {return 0;}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "true-preview"
|
||||
|
||||
public class Foo {
|
||||
public int bar(int param) {
|
||||
switch (param) {
|
||||
default -> {
|
||||
call();
|
||||
throw <caret>switch (param) {
|
||||
default -> new ArithmeticException();
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
public int call() {
|
||||
return 3;
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "true-preview"
|
||||
|
||||
public class Foo {
|
||||
public int bar(int param) {
|
||||
if (param==2) {
|
||||
call(1);
|
||||
throw <caret>switch (param) {
|
||||
default -> new ArithmeticException();
|
||||
};
|
||||
} else {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
public int call(Object... param) {
|
||||
return 3;
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "true-preview"
|
||||
|
||||
class Foo {
|
||||
void bar(int param) {
|
||||
methodA(1);
|
||||
throw <caret>switch (param) {
|
||||
default -> new RuntimeException("default");
|
||||
};
|
||||
}
|
||||
void parentMethod(int x, int y, int z) {}
|
||||
void methodA(int x) {}
|
||||
void methodB(int x) {}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "true-preview"
|
||||
|
||||
class Foo {
|
||||
void bar(int param) {
|
||||
a(1);
|
||||
b(2);
|
||||
throw <caret>switch (param) {
|
||||
default -> new RuntimeException("default");
|
||||
};
|
||||
}
|
||||
void parentMethod(int x, int y, int z) {}
|
||||
int nestedMethod(int x, int y, int z) {}
|
||||
void a(int x) {}
|
||||
void b(int x) {}
|
||||
void c(int x) {}
|
||||
void d(int x) {}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "true-preview"
|
||||
|
||||
class Foo {
|
||||
void bar(int param) {
|
||||
a(1);
|
||||
throw <caret>switch (param) {
|
||||
default -> new RuntimeException("default");
|
||||
};
|
||||
}
|
||||
Exception newException(int x, int y, int z) {
|
||||
return new RuntimeException();
|
||||
}
|
||||
void a(int x) {}
|
||||
void b(int x) {}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "true-preview"
|
||||
|
||||
class Foo {
|
||||
void bar(int param) {
|
||||
for (int i = 0; i < 10; i++) {
|
||||
throw <caret>switch (param) {
|
||||
default -> new RuntimeException("default");
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "true-preview"
|
||||
|
||||
class Foo {
|
||||
void bar(int param) {
|
||||
throw <caret>switch (param) {
|
||||
default -> new RuntimeException("default");
|
||||
};
|
||||
}
|
||||
void methodA(int x) {}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "true-preview"
|
||||
|
||||
class Foo {
|
||||
void bar(int param) {
|
||||
throw <caret>switch (param) {
|
||||
default -> new RuntimeException("default");
|
||||
};
|
||||
}
|
||||
void methodA(int x) {}
|
||||
void methodB(int x) {}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "true-preview"
|
||||
|
||||
class Foo {
|
||||
int bar(int param) {
|
||||
int a = 3;
|
||||
int b;
|
||||
int c = 4;
|
||||
throw <caret>switch (param) {
|
||||
default -> new RuntimeException("default");
|
||||
};
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "true-preview"
|
||||
|
||||
class Foo {
|
||||
int bar(int param) {
|
||||
a();
|
||||
throw <caret>switch (param) {
|
||||
default -> new RuntimeException("default");
|
||||
};
|
||||
}
|
||||
int a() {return 0;}
|
||||
int b() {return 0;}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "true-preview"
|
||||
|
||||
class Foo {
|
||||
int bar(Three param) {
|
||||
a();
|
||||
throw <caret>switch (param) {
|
||||
default -> new RuntimeException("default");
|
||||
};
|
||||
}
|
||||
int a() {return 0;}
|
||||
int b() {return 0;}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "true-preview"
|
||||
|
||||
class Foo {
|
||||
int bar(int param) {
|
||||
throw <caret>switch (param) {
|
||||
default -> new RuntimeException("default");
|
||||
};
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "true-preview"
|
||||
|
||||
public class Foo {
|
||||
public int bar(int param) {
|
||||
switch (param) {
|
||||
default -> {
|
||||
throw <caret>switch (param) {
|
||||
default -> new ArithmeticException();
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "true-preview"
|
||||
|
||||
class Foo {
|
||||
int bar(Three param) {
|
||||
throw <caret>switch (param) {
|
||||
default -> new RuntimeException("default");
|
||||
};
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "true-preview"
|
||||
|
||||
class Foo {
|
||||
int bar(int param) {
|
||||
int x;
|
||||
throw <caret>switch (param) {
|
||||
default -> new RuntimeException("default");
|
||||
};
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "true-preview"
|
||||
|
||||
class Foo {
|
||||
int bar(Three param) {
|
||||
throw <caret>switch (param) {
|
||||
default -> new RuntimeException("default");
|
||||
};
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "true-preview"
|
||||
|
||||
class Foo {
|
||||
void bar(int param) {
|
||||
throw <caret>switch (param) {
|
||||
default -> new RuntimeException("default");
|
||||
};
|
||||
}
|
||||
void methodA(int x) {}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "true-preview"
|
||||
|
||||
class Foo {
|
||||
void bar(int param) {
|
||||
int x = switch (param) {
|
||||
case 1 -> 7;
|
||||
default -> {
|
||||
throw <caret>switch (param) {
|
||||
default -> new RuntimeException("default");
|
||||
};
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "true-preview"
|
||||
|
||||
class Foo {
|
||||
int bar() {
|
||||
return <caret>switch (7) {
|
||||
default -> {
|
||||
new Runnable() {
|
||||
public void run() {
|
||||
throw new RuntimeException("runnable");
|
||||
}
|
||||
}.run();
|
||||
throw new RuntimeException("message");
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "true-preview"
|
||||
|
||||
class Foo {
|
||||
int bar() {
|
||||
return <caret>switch (7) {
|
||||
default -> {
|
||||
int x = switch (5) {
|
||||
case 1:
|
||||
throw new RuntimeException("one");
|
||||
case 2:
|
||||
yield 3;
|
||||
default:
|
||||
throw new RuntimeException("default");
|
||||
};
|
||||
throw new RuntimeException("message");
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "true-preview"
|
||||
|
||||
class Foo {
|
||||
int bar() {
|
||||
return <caret>switch (7) {
|
||||
default -> {
|
||||
Runnable runnable = () -> {
|
||||
throw new RuntimeException("runnable");
|
||||
};
|
||||
runnable.run();
|
||||
throw new RuntimeException("message");
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "true-preview"
|
||||
|
||||
class Foo {
|
||||
int bar() {
|
||||
return <caret>switch (7) {
|
||||
default -> {
|
||||
class LocalClass {
|
||||
public void fooBar() {
|
||||
throw new RuntimeException("local classs");
|
||||
}
|
||||
}
|
||||
throw new RuntimeException("message");
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "false"
|
||||
|
||||
class Foo {
|
||||
void bar(int param) {
|
||||
assert <caret>switch(param) { default -> throw new RuntimeException(); } : "message";
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "false"
|
||||
|
||||
class Foo {
|
||||
void bar(int param) {
|
||||
assert true : <caret>switch(param) { default -> throw new RuntimeException(); };
|
||||
}
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "false"
|
||||
|
||||
class Foo {
|
||||
int bar = <caret>switch(param) { default -> throw new RuntimeException(); };
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "false"
|
||||
|
||||
class Foo {
|
||||
int bar(int param, boolean b) {
|
||||
return b? <caret>switch (param) { default -> throw new RuntimeException("default"); } : 7;
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "false"
|
||||
|
||||
class Foo {
|
||||
void bar(int param) {
|
||||
do {
|
||||
methodA(1);
|
||||
} while (<caret>switch (param) { default -> throw new RuntimeException("default"); })
|
||||
}
|
||||
void methodA(int x) {}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "false"
|
||||
|
||||
class Foo {
|
||||
void bar(int param) {
|
||||
for (int i = 0; i == <caret>switch(param) { default -> throw new RuntimeException(); }; i++) {
|
||||
}
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "false"
|
||||
|
||||
class Foo {
|
||||
void bar(int param) {
|
||||
for (int i = <caret>switch(param) { default -> throw new RuntimeException(); }; i < 0; i++) {
|
||||
}
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "false"
|
||||
|
||||
class Foo {
|
||||
void bar(int param) {
|
||||
for (int i = 0; i < 0; i = <caret>switch(param) { default -> throw new RuntimeException(); }) {
|
||||
}
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "false"
|
||||
|
||||
|
||||
class Foo {
|
||||
void bar() {
|
||||
class Local {
|
||||
int bar = <caret>switch(3) { default -> throw new RuntimeException(); };
|
||||
}
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "false"
|
||||
|
||||
class Foo {
|
||||
int bar(int param, boolean b) {
|
||||
return m(b? <caret>switch (param) { default -> throw new RuntimeException("default"); } : 7);
|
||||
}
|
||||
int m(int x) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "false"
|
||||
|
||||
class Foo {
|
||||
void bar(int param) {
|
||||
synchronized (<caret>switch (param) { default -> throw new RuntimeException("default"); }) {
|
||||
}
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "false"
|
||||
|
||||
class Foo {
|
||||
void bar(int param) {
|
||||
try (java.io.InputStream resource = <caret>switch(param) { default -> throw new RuntimeException(); }) {
|
||||
}
|
||||
}
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "true-preview"
|
||||
|
||||
class Foo {
|
||||
int bar() {
|
||||
// BEFORE STATEMENT
|
||||
return /* INSIDE 0*/ <caret>switch /*INSIDE 1*/ (7) {
|
||||
// BEFORE FIRST
|
||||
case 1 -> throw new /*INSIDE 2*/ RuntimeException("one"); // TRAILING
|
||||
// BETWEEN
|
||||
default -> {
|
||||
// BEFORE
|
||||
throw new /*INSIDE 3*/ RuntimeException("default"); // TRAILING
|
||||
// AFTER
|
||||
}
|
||||
// AFTER LAST
|
||||
};
|
||||
// AFTER STATEMENT
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "true-preview"
|
||||
|
||||
class Foo {
|
||||
int bar() {
|
||||
return <caret>switch (7) {
|
||||
default -> {
|
||||
{
|
||||
throw new RuntimeException("then");
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "true-preview"
|
||||
|
||||
class Foo {
|
||||
int bar() {
|
||||
return <caret>switch (7) {
|
||||
default -> {
|
||||
for (int i = 0; i < new java.util.Random().nextInt(); i++) {
|
||||
throw new RuntimeException("for");
|
||||
}
|
||||
throw new RuntimeException("default");
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "true-preview"
|
||||
|
||||
class Foo {
|
||||
int bar() {
|
||||
return <caret>switch (7) {
|
||||
default -> {
|
||||
if (true) {
|
||||
throw new RuntimeException("then");
|
||||
} else {
|
||||
throw new RuntimeException("else");
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "true-preview"
|
||||
|
||||
class Foo {
|
||||
int bar() {
|
||||
return <caret>switch (7) {
|
||||
default -> {
|
||||
switch (5) {
|
||||
case 1:
|
||||
throw new RuntimeException("one");
|
||||
default:
|
||||
throw new RuntimeException("default");
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "true-preview"
|
||||
|
||||
class Foo {
|
||||
int bar(int param) {
|
||||
return <caret>switch (param) {
|
||||
default:
|
||||
switch (param) {
|
||||
case 1 -> throw new RuntimeException();
|
||||
default -> throw new RuntimeException();
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "true-preview"
|
||||
|
||||
class Foo {
|
||||
enum Two {
|
||||
ONE, TWO;
|
||||
}
|
||||
int bar(Two param) {
|
||||
return <caret>switch (param) {
|
||||
case ONE -> {
|
||||
throw new RuntimeException("1");
|
||||
}
|
||||
case TWO -> {
|
||||
throw new RuntimeException("2");
|
||||
}
|
||||
default -> {
|
||||
throw new RuntimeException("default");
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "true-preview"
|
||||
|
||||
class Foo {
|
||||
enum Three {
|
||||
ONE, TWO;
|
||||
}
|
||||
int bar(Three param) {
|
||||
return <caret>switch (param) {
|
||||
case ONE -> throw new RuntimeException("1");
|
||||
case TWO -> throw new RuntimeException("2");
|
||||
default -> throw new RuntimeException("default");
|
||||
};
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "true-preview"
|
||||
|
||||
class Foo {
|
||||
int bar(int param, boolean b) {
|
||||
return <caret>switch (param) {
|
||||
case 1:
|
||||
if (b) {
|
||||
throw new RuntimeException("one");
|
||||
}
|
||||
default:
|
||||
throw new RuntimeException("default");
|
||||
};
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "true-preview"
|
||||
|
||||
class Foo {
|
||||
int bar() {
|
||||
return <caret>switch (7) {
|
||||
default -> {
|
||||
while (new java.util.Random().nextBoolean()) {
|
||||
throw new RuntimeException("while");
|
||||
}
|
||||
throw new RuntimeException("default");
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "true-preview"
|
||||
|
||||
class Foo {
|
||||
int bar(int param) {
|
||||
return <caret>switch (param) {
|
||||
default -> throw new RuntimeException("default");
|
||||
} + a();
|
||||
}
|
||||
int a() {return 0;}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "true-preview"
|
||||
|
||||
class Foo {
|
||||
int bar(int param) {
|
||||
return a() + switch<caret> (param) {
|
||||
default -> throw new RuntimeException("default");
|
||||
};
|
||||
}
|
||||
int a() {return 0;}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "true-preview"
|
||||
|
||||
public class Foo {
|
||||
public int bar(int param) {
|
||||
switch (param) {
|
||||
default -> call() + <caret>switch (param) {
|
||||
default -> throw new ArithmeticException();
|
||||
};
|
||||
}
|
||||
}
|
||||
public int call() {
|
||||
return 3;
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "true-preview"
|
||||
|
||||
public class Foo {
|
||||
public int bar(int param) {
|
||||
if (param==2)
|
||||
return call(call(1), <caret>switch (param) {
|
||||
default -> throw new ArithmeticException();
|
||||
});
|
||||
else {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
public int call(Object... param) {
|
||||
return 3;
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "true-preview"
|
||||
|
||||
class Foo {
|
||||
void bar(int param) {
|
||||
parentMethod(
|
||||
methodA(1),
|
||||
<caret>switch (param) { default -> throw new RuntimeException("default"); },
|
||||
methodA(2)
|
||||
);
|
||||
}
|
||||
void parentMethod(int x, int y, int z) {}
|
||||
void methodA(int x) {}
|
||||
void methodB(int x) {}
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "true-preview"
|
||||
|
||||
class Foo {
|
||||
void bar(int param) {
|
||||
parentMethod(
|
||||
a(1),
|
||||
nestedMethod(
|
||||
b(2),
|
||||
<caret>switch (param) { default -> throw new RuntimeException("default"); },
|
||||
c(3)
|
||||
),
|
||||
d(4)
|
||||
);
|
||||
}
|
||||
void parentMethod(int x, int y, int z) {}
|
||||
int nestedMethod(int x, int y, int z) {}
|
||||
void a(int x) {}
|
||||
void b(int x) {}
|
||||
void c(int x) {}
|
||||
void d(int x) {}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "true-preview"
|
||||
|
||||
class Foo {
|
||||
void bar(int param) {
|
||||
throw newException(
|
||||
a(1),
|
||||
<caret>switch (param) { default -> throw new RuntimeException("default"); },
|
||||
b(2)
|
||||
);
|
||||
}
|
||||
Exception newException(int x, int y, int z) {
|
||||
return new RuntimeException();
|
||||
}
|
||||
void a(int x) {}
|
||||
void b(int x) {}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "true-preview"
|
||||
|
||||
class Foo {
|
||||
void bar(int param) {
|
||||
for (int i = 0; i < 10; i++) {
|
||||
int x = <caret>switch (param) { default -> throw new RuntimeException("default"); };
|
||||
}
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "true-preview"
|
||||
|
||||
class Foo {
|
||||
void bar(int param) {
|
||||
for (var item : <caret>switch (param) { default -> throw new RuntimeException("default");}) {
|
||||
methodA(1);
|
||||
}
|
||||
}
|
||||
void methodA(int x) {}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "true-preview"
|
||||
|
||||
class Foo {
|
||||
void bar(int param) {
|
||||
if (<caret>switch (param) { default -> throw new RuntimeException("default"); }) {
|
||||
methodA(1);
|
||||
} else {
|
||||
methodB(2);
|
||||
}
|
||||
}
|
||||
void methodA(int x) {}
|
||||
void methodB(int x) {}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "true-preview"
|
||||
|
||||
class Foo {
|
||||
int bar(int param) {
|
||||
int a = 3, b, c = 4, d = <caret>switch (param) {
|
||||
default -> throw new RuntimeException("default");
|
||||
}, e = 5;
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "true-preview"
|
||||
|
||||
class Foo {
|
||||
int bar(int param) {
|
||||
return a() + <caret>switch (param) {
|
||||
default -> throw new RuntimeException("default");
|
||||
} + b();
|
||||
}
|
||||
int a() {return 0;}
|
||||
int b() {return 0;}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "true-preview"
|
||||
|
||||
class Foo {
|
||||
int bar(Three param) {
|
||||
int x = a() + <caret>switch (param) {
|
||||
default -> throw new RuntimeException("default");
|
||||
} + b();
|
||||
}
|
||||
int a() {return 0;}
|
||||
int b() {return 0;}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "true-preview"
|
||||
|
||||
class Foo {
|
||||
int bar(int param) {
|
||||
return <caret>switch (param) {
|
||||
default -> throw new RuntimeException("default");
|
||||
};
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "true-preview"
|
||||
|
||||
public class Foo {
|
||||
public int bar(int param) {
|
||||
switch (param) {
|
||||
default -> <caret>switch (param) {
|
||||
default -> throw new ArithmeticException();
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "true-preview"
|
||||
|
||||
class Foo {
|
||||
int bar(Three param) {
|
||||
throw <caret>switch (param) {
|
||||
default -> throw new RuntimeException("default");
|
||||
};
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "true-preview"
|
||||
|
||||
class Foo {
|
||||
int bar(int param) {
|
||||
int x;
|
||||
x = <caret>switch (param) {
|
||||
default -> throw new RuntimeException("default");
|
||||
};
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "true-preview"
|
||||
|
||||
class Foo {
|
||||
int bar(Three param) {
|
||||
int x = <caret>switch (param) {
|
||||
default -> throw new RuntimeException("default");
|
||||
};
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "true-preview"
|
||||
|
||||
class Foo {
|
||||
void bar(int param) {
|
||||
while (<caret>switch (param) { default -> throw new RuntimeException("default"); }) {
|
||||
methodA(1);
|
||||
}
|
||||
}
|
||||
void methodA(int x) {}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
// "Lift 'throw' out of 'switch' expression" "true-preview"
|
||||
|
||||
class Foo {
|
||||
void bar(int param) {
|
||||
int x = switch (param) {
|
||||
case 1 -> 7;
|
||||
default -> {
|
||||
yield <caret>switch (param) {
|
||||
default -> throw new RuntimeException("default");
|
||||
};
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
// Copyright 2000-2025 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;
|
||||
|
||||
public class LiftThrowOutOfSwitchExpressionFixTest extends LightQuickFixParameterizedTestCase {
|
||||
@Override
|
||||
protected String getBasePath() {
|
||||
return "/codeInsight/daemonCodeAnalyzer/quickFix/liftThrowOutOfSwitchExpression";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user