ConvertSwitchToIfIntention: preserve comments from expression when only default branch is present

This commit is contained in:
Tagir Valeev
2019-01-10 17:24:20 +07:00
parent 0cf276793e
commit c68df6edc2
3 changed files with 7 additions and 4 deletions
@@ -91,7 +91,6 @@ public class ConvertSwitchToIfIntention implements IntentionAction {
return;
}
CommentTracker commentTracker = new CommentTracker();
commentTracker.markUnchanged(switchExpression);
final boolean isSwitchOnString = switchExpressionType.equalsToText(CommonClassNames.JAVA_LANG_STRING);
boolean useEquals = isSwitchOnString;
if (!useEquals) {
@@ -114,8 +113,11 @@ public class ConvertSwitchToIfIntention implements IntentionAction {
final boolean hadSideEffects;
final String expressionText;
final Project project = switchStatement.getProject();
if (allBranches.stream().mapToInt(br -> br.getCaseValues().size()).sum() > 1 &&
RemoveUnusedVariableUtil.checkSideEffects(switchExpression, null, new ArrayList<>())) {
int totalCases = allBranches.stream().mapToInt(br -> br.getCaseValues().size()).sum();
if (totalCases > 0) {
commentTracker.markUnchanged(switchExpression);
}
if (totalCases > 1 && RemoveUnusedVariableUtil.checkSideEffects(switchExpression, null, new ArrayList<>())) {
hadSideEffects = true;
final String variableName = new VariableNameGenerator(switchExpression, VariableKind.LOCAL_VARIABLE)
@@ -1,6 +1,7 @@
// "Unwrap 'switch'" "true"
class X {
String test(char c) {
/*comment*/
if (c == 'a') {
System.out.println("foo");
}
@@ -1,7 +1,7 @@
// "Unwrap 'switch'" "true"
class X {
String test(char c) {
s<caret>witch (c) {
s<caret>witch (c/*comment*/) {
default:
if(c == 'a') {
System.out.println("foo");