EnhancedSwitchMigrationInspection: do not create block when single statement in branch: IDEA-CR-40190

This commit is contained in:
Roman.Ivanov
2018-12-06 17:41:52 +07:00
parent 2314a25b04
commit 7b3014b9c4
6 changed files with 50 additions and 21 deletions
@@ -503,6 +503,9 @@ public class EnhancedSwitchMigrationInspection extends AbstractBaseJavaLocalInsp
@Override
public String generate(CommentTracker ct) {
if (myResultStatements.length == 1) {
return ct.text(myResultStatements[0]);
}
return StreamEx.of(myResultStatements).map(stmt -> ct.text(stmt)).joining("\n", "{\n", "}");
}
}
@@ -5,12 +5,8 @@ class SwitchExpressionMigration {
private static String m(int n) {
switch (n) {
/*1*/
case 1 -> {
System.out.println("a"/*2*/); /*3*/
}
case 2 -> {
System.out.println("b");
}
case 1 -> System.out.println("a"/*2*/); /*3*/
case 2 -> System.out.println("b");
}
}
}
@@ -4,12 +4,8 @@ import java.util.*;
class SwitchExpressionMigration {
private static String m(int n) {
switch (n) {
case 1 -> {
return "a";
}
case 2 -> {
return "b";
}
case 1 -> return "a";
case 2 -> return "b";
}
}
}
@@ -0,0 +1,19 @@
// "Replace with enhanced 'switch' statement" "true"
import java.util.*;
class SwitchExpressionMigration {
private static int m(int x) {
switch (x) {
case 1 -> {
if (true) return 0;
else return 1;
}
case 2 -> return 2;
case 3, 4 -> {
System.out.println("asda");
return 3;
}
default -> return 12;
}
}
}
@@ -4,15 +4,9 @@ import java.util.*;
class SwitchExpressionMigration {
private static String m(int n) {
switch (n) {
case 3, 1 -> {
System.out.println("a");
}
case 5, 6, 7 -> {
System.out.println("a");
}
case 2, 4 -> {
System.out.println("b");
}
case 3, 1 -> System.out.println("a");
case 5, 6, 7 -> System.out.println("a");
case 2, 4 -> System.out.println("b");
}
}
}
@@ -0,0 +1,21 @@
// "Replace with enhanced 'switch' statement" "true"
import java.util.*;
class SwitchExpressionMigration {
private static int m(int x) {
switch<caret> (x) {
case 1: {
if (true) return 0;
else return 1;
}
case 2:
return 2;
case 3, 4: {
System.out.println("asda");
return 3;
}
default:
return 12;
}
}
}