IDEA-204717 Can't convert enhanced 'switch' to 'if' if a branch's body contains an end-of-line comment

This commit is contained in:
Tagir Valeev
2018-12-26 17:56:52 +07:00
parent 5457c671f8
commit 7addf9e702
3 changed files with 29 additions and 2 deletions

View File

@@ -338,8 +338,13 @@ public class ConvertSwitchToIfIntention implements IntentionAction {
if (bodyStatement instanceof PsiBlockStatement) {
final PsiBlockStatement blockStatement = (PsiBlockStatement)bodyStatement;
final PsiCodeBlock codeBlock = blockStatement.getCodeBlock();
for (PsiStatement statement : codeBlock.getStatements()) {
out.append(commentTracker.text(statement));
PsiElement start = PsiTreeUtil.skipWhitespacesForward(codeBlock.getFirstBodyElement());
PsiElement end = PsiTreeUtil.skipWhitespacesBackward(codeBlock.getLastBodyElement());
if (start != null && end != null) {
for (PsiElement child = start; child != null; child = child.getNextSibling()) {
out.append(commentTracker.text(child));
if (child == end) break;
}
}
}
else {

View File

@@ -0,0 +1,10 @@
// "Replace 'switch' with 'if'" "true"
class Test {
void f(int n, int k) {
if (n == 0) {
if (k == 0) return; //this comment causes the exception IDEA-204717
if (k == 1) {
}
}
}
}

View File

@@ -0,0 +1,12 @@
// "Replace 'switch' with 'if'" "true"
class Test {
void f(int n, int k) {
sw<caret>itch (n) {
case 0 -> {
if (k == 0) return; //this comment causes the exception IDEA-204717
if (k == 1) {
}
}
}
}
}