diff --git a/java/java-impl/src/com/intellij/codeInspection/enhancedSwitch/SwitchLabeledRuleCanBeCodeBlockInspection.java b/java/java-impl/src/com/intellij/codeInspection/enhancedSwitch/SwitchLabeledRuleCanBeCodeBlockInspection.java index 398f07f418e5..ed3d0142be9c 100644 --- a/java/java-impl/src/com/intellij/codeInspection/enhancedSwitch/SwitchLabeledRuleCanBeCodeBlockInspection.java +++ b/java/java-impl/src/com/intellij/codeInspection/enhancedSwitch/SwitchLabeledRuleCanBeCodeBlockInspection.java @@ -5,8 +5,10 @@ import com.intellij.codeInspection.LocalInspectionTool; import com.intellij.codeInspection.LocalQuickFix; import com.intellij.codeInspection.ProblemDescriptor; import com.intellij.codeInspection.ProblemsHolder; +import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.project.Project; import com.intellij.pom.java.LanguageLevel; +import com.intellij.profile.codeInspection.InspectionProjectProfileManager; import com.intellij.psi.*; import com.intellij.psi.util.PsiUtil; import com.intellij.util.ObjectUtils; @@ -44,11 +46,22 @@ public class SwitchLabeledRuleCanBeCodeBlockInspection extends LocalInspectionTo } } - private void registerProblem(PsiSwitchLabeledRuleStatement statement, boolean isExpressionResult) { - holder.registerProblem(ObjectUtils.notNull(ObjectUtils.tryCast(statement.getFirstChild(), PsiKeyword.class), statement), - message(isExpressionResult ? "inspection.switch.labeled.rule.can.be.code.block.expression.message" + private void registerProblem(@NotNull PsiSwitchLabeledRuleStatement statement, boolean isResultExpression) { + holder.registerProblem(getProblemElement(statement), + message(isResultExpression ? "inspection.switch.labeled.rule.can.be.code.block.expression.message" : "inspection.switch.labeled.rule.can.be.code.block.statement.message"), - new WrapWithCodeBlockFix(isExpressionResult)); + new WrapWithCodeBlockFix(isResultExpression)); + } + + @NotNull + private PsiElement getProblemElement(@NotNull PsiSwitchLabeledRuleStatement statement) { + if (isOnTheFly) { + if (InspectionProjectProfileManager.isInformationLevel(getShortName(), statement) || + ApplicationManager.getApplication().isUnitTestMode()) { + return statement; + } + } + return ObjectUtils.notNull(ObjectUtils.tryCast(statement.getFirstChild(), PsiKeyword.class), statement); } }; } @@ -56,8 +69,8 @@ public class SwitchLabeledRuleCanBeCodeBlockInspection extends LocalInspectionTo private static class WrapWithCodeBlockFix implements LocalQuickFix { private final String myMessage; - WrapWithCodeBlockFix(boolean isExpressionResult) { - myMessage = message(isExpressionResult ? "inspection.switch.labeled.rule.can.be.code.block.fix.expression.name" + WrapWithCodeBlockFix(boolean isResultExpression) { + myMessage = message(isResultExpression ? "inspection.switch.labeled.rule.can.be.code.block.fix.expression.name" : "inspection.switch.labeled.rule.can.be.code.block.fix.statement.name"); } @@ -89,16 +102,13 @@ public class SwitchLabeledRuleCanBeCodeBlockInspection extends LocalInspectionTo } private static void wrapExpression(PsiExpressionStatement expressionStatement) { - PsiExpression expression = expressionStatement.getExpression(); CommentTracker tracker = new CommentTracker(); - tracker.markUnchanged(expression); - tracker.replaceAndRestoreComments(expressionStatement, "{ break " + expression.getText() + "; }"); + tracker.replaceAndRestoreComments(expressionStatement, "{ break " + tracker.text(expressionStatement) + " }"); } private static void wrapStatement(@NotNull PsiStatement statement) { CommentTracker tracker = new CommentTracker(); - tracker.markUnchanged(statement); - tracker.replaceAndRestoreComments(statement, "{ " + statement.getText() + " }"); + tracker.replaceAndRestoreComments(statement, "{ " + tracker.text(statement) + " }"); } } } diff --git a/java/java-tests/testData/inspection/switchLabeledRuleCanBeCodeBlock/InExpression.java b/java/java-tests/testData/inspection/switchLabeledRuleCanBeCodeBlock/InExpression.java index fd94fde4a5ae..6e2d13f1933f 100644 --- a/java/java-tests/testData/inspection/switchLabeledRuleCanBeCodeBlock/InExpression.java +++ b/java/java-tests/testData/inspection/switchLabeledRuleCanBeCodeBlock/InExpression.java @@ -1,12 +1,12 @@ class C { String foo(int n) { return switch (n) { - case 1 -> Integer.toString(n); + case 1 -> Integer.toString(n); case 2 -> { break Integer.toString(n); } - case 3 -> throw new RuntimeException(); + case 3 -> throw new RuntimeException(); case 4 -> { throw new RuntimeException(); } case 5 -> { break "a";} - default -> "b"; + default -> "b"; }; } } \ No newline at end of file diff --git a/java/java-tests/testData/inspection/switchLabeledRuleCanBeCodeBlock/InStatement.java b/java/java-tests/testData/inspection/switchLabeledRuleCanBeCodeBlock/InStatement.java index 2d34b1ee6144..16346003ec20 100644 --- a/java/java-tests/testData/inspection/switchLabeledRuleCanBeCodeBlock/InStatement.java +++ b/java/java-tests/testData/inspection/switchLabeledRuleCanBeCodeBlock/InStatement.java @@ -2,12 +2,12 @@ class C { String foo(int n) { String s; switch (n) { - case 1 -> s = Integer.toString(n); + case 1 -> s = Integer.toString(n); case 2 -> { s = Integer.toString(n); } - case 3 -> throw new RuntimeException(); + case 3 -> throw new RuntimeException(); case 4 -> { throw new RuntimeException(); } case 5 -> { s = "a"; } - default -> s = "b"; + default -> s = "b"; }; return s; }