remove redundant else: check all then clauses (IDEA-58136)

This commit is contained in:
anna
2010-08-30 20:52:53 +04:00
parent ad9c77ecca
commit 0bb2f844f2
5 changed files with 102 additions and 8 deletions
@@ -26,6 +26,7 @@ import com.intellij.psi.controlFlow.*;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* @author ven
@@ -53,20 +54,38 @@ public class RemoveRedundantElseAction extends PsiElementBaseIntentionAction {
if (thenBranch == null) return false;
PsiElement block = PsiTreeUtil.getParentOfType(ifStatement, PsiCodeBlock.class);
if (block != null) {
try {
ControlFlow controlFlow = ControlFlowFactory.getInstance(project).getControlFlow(block, LocalsOrMyInstanceFieldsControlFlowPolicy.getInstance());
int startOffset = controlFlow.getStartOffset(thenBranch);
int endOffset = controlFlow.getEndOffset(thenBranch);
return startOffset != -1 && endOffset != -1 && !ControlFlowUtil.canCompleteNormally(controlFlow, startOffset,endOffset);
}
catch (AnalysisCanceledException e) {
return false;
while (cantCompleteNormally(thenBranch, block)) {
thenBranch = getPrevThenBranch(thenBranch);
if (thenBranch == null) return true;
}
return false;
}
}
return false;
}
@Nullable
private static PsiStatement getPrevThenBranch(@NotNull PsiElement thenBranch) {
final PsiElement ifStatement = thenBranch.getParent();
final PsiElement parent = ifStatement.getParent();
if (parent instanceof PsiIfStatement && ((PsiIfStatement)parent).getElseBranch() == ifStatement) {
return ((PsiIfStatement)parent).getThenBranch();
}
return null;
}
private static boolean cantCompleteNormally(@NotNull PsiStatement thenBranch, PsiElement block) {
try {
ControlFlow controlFlow = ControlFlowFactory.getInstance(thenBranch.getProject()).getControlFlow(block, LocalsOrMyInstanceFieldsControlFlowPolicy.getInstance());
int startOffset = controlFlow.getStartOffset(thenBranch);
int endOffset = controlFlow.getEndOffset(thenBranch);
return startOffset != -1 && endOffset != -1 && !ControlFlowUtil.canCompleteNormally(controlFlow, startOffset, endOffset);
}
catch (AnalysisCanceledException e) {
return false;
}
}
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
if (!CodeInsightUtilBase.prepareFileForWrite(file)) return;
PsiElement elementAt = file.findElementAt(editor.getCaretModel().getOffset());
@@ -0,0 +1,13 @@
// "Remove Redundant 'else'" "true"
class a {
void foo() {
int a = 0;
int b = 0;
if (a != b) {
return;
}
a = b;
a++;
}
}
@@ -0,0 +1,14 @@
// "Remove Redundant 'else'" "true"
class a {
void foo() {
int a = 0;
int b = 0;
if (a != b) {
return;
} e<caret>lse {
a = b;
}
a++;
}
}
@@ -0,0 +1,17 @@
// "Remove Redundant 'else'" "false"
class a {
void foo() {
int a = 0;
int b = 0;
if (a != b) {
a = 10;
} else if (a + 1 == b) {
return;
}
e<caret>lse {
a = b;
}
a++;
}
}
@@ -0,0 +1,31 @@
/*
* Copyright 2000-2010 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.codeInsight.daemon.quickFix;
/**
* User: anna
* Date: Aug 30, 2010
*/
public class RemoveRedundantElseActionTest extends LightQuickFixTestCase {
public void test() throws Exception { doAllTests(); }
protected String getBasePath() {
return "/codeInsight/daemonCodeAnalyzer/quickFix/removeRedundantElse";
}
}