Java: Unwrap break-with-value when the value is a switch expression (IDEA-204393)

This commit is contained in:
Pavel Dolgov
2018-12-18 16:57:35 +03:00
parent 6a5f2be2a2
commit 06b41bbc74
3 changed files with 34 additions and 1 deletions
@@ -93,8 +93,14 @@ public class RedundantLabeledSwitchRuleCodeBlockInspection extends LocalInspecti
private static void unwrapBreakValue(PsiStatement body, PsiBreakStatement breakStatement) {
PsiExpression valueExpression = breakStatement.getValueExpression();
if (valueExpression != null) {
PsiElementFactory factory = JavaPsiFacade.getElementFactory(body.getProject());
PsiExpressionStatement statement = (PsiExpressionStatement)factory.createStatementFromText("x=1;", body);
statement.getExpression().replace(valueExpression);
CommentTracker tracker = new CommentTracker();
tracker.replaceAndRestoreComments(body, tracker.text(valueExpression) + ';');
// replaceAndRestoreComments() will work with a copy of the expression so it won't see the original comments
tracker.markUnchanged(valueExpression);
tracker.replaceAndRestoreComments(body, statement);
}
}
@@ -0,0 +1,13 @@
// "Unwrap code block of labeled rule" "LIKE_UNUSED_SYMBOL"
class C {
int foo(final int x, int n) {
return switch (x) {
case 0 -> /*1*/ /*3*/ //4
switch (n) { // 2
case 0 -> 0;
default -> 1;
};
default -> 2;
};
}
}
@@ -0,0 +1,14 @@
// "Unwrap code block of labeled rule" "LIKE_UNUSED_SYMBOL"
class C {
int foo(final int x, int n) {
return switch (x) {
case 0 -> {
<caret>break /*1*/ switch (n) { // 2
case 0 -> 0;
default -> 1;
}/*3*/; //4
}
default -> 2;
};
}
}