[java-inspections] IDEA-277236 Java 17 support: Conversion from if to switch - misplaced end of line comments and including unrelated return statement in switch.

GitOrigin-RevId: 3bb220692bc9d731f945be76996c314c9ee353f3
This commit is contained in:
Mikhail Pyltsin
2024-11-26 16:55:45 +00:00
committed by intellij-monorepo-bot
parent f299c8ecd4
commit 74da8644f3
4 changed files with 34 additions and 0 deletions
@@ -1147,6 +1147,14 @@ public final class EnhancedSwitchMigrationInspection extends AbstractBaseJavaLoc
grabCommentsBeforeColon(label, ct, sb);
sb.append("->");
sb.append(myRuleResult.generate(ct, this));
if (!myUsedElements.isEmpty()) {
PsiElement element = PsiTreeUtil.nextCodeLeaf(myUsedElements.get(myUsedElements.size() - 1));
if(element instanceof PsiJavaToken javaToken && javaToken.textMatches("}") &&
element.getParent() instanceof PsiCodeBlock codeBlock &&
codeBlock.getParent() instanceof PsiSwitchBlock) {
sb.append(ct.commentsBefore(element));
}
}
return sb.toString();
}
@@ -0,0 +1,12 @@
class Comments {
String foo(int par) {
<caret>return switch (par) {
case 11 -> "ciao";/* case 1*/
case 14 -> "fourteen";//case 2
case 15 -> "fifteen"; //case 3
default -> "default";
};
}
}
@@ -0,0 +1,13 @@
class Comments {
String foo(int par) {
i<caret>f(par == 11)
return "ciao";/* case 1*/
else if(par == 14)
return "fourteen";//case 2
else if(par == 15)
return "fifteen"; //case 3
return "default";
}
}
@@ -30,4 +30,5 @@ public class IfCanBeSwitchSwitchExpressionFixTest extends IGQuickFixesTestCase {
public void testJava14Expression() { doTest();}
public void testJava14Comment() { doTest();}
public void testJavaCommentsOutsideLastStatement() { doTest();}
}