IG: recognize when catch blocks containing a return statement are identical (IDEA-159182)

This commit is contained in:
Bas Leijdekkers
2016-07-30 00:34:38 +02:00
parent 732f6943ed
commit a5ff9dbb3f
3 changed files with 33 additions and 1 deletions
@@ -24,6 +24,8 @@ import com.intellij.psi.util.TypeConversionUtil;
import com.intellij.refactoring.extractMethod.InputVariables;
import com.intellij.refactoring.util.duplicates.DuplicatesFinder;
import com.intellij.refactoring.util.duplicates.Match;
import com.intellij.refactoring.util.duplicates.ReturnStatementReturnValue;
import com.intellij.refactoring.util.duplicates.ReturnValue;
import com.intellij.util.IncorrectOperationException;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
@@ -110,7 +112,11 @@ public class TryWithIdenticalCatchesInspection extends BaseInspection {
continue;
}
final Match match = finder.isDuplicate(otherCatchBlock, true);
if (match == null || match.getReturnValue() != null) {
if (match == null) {
continue;
}
final ReturnValue returnValue = match.getReturnValue();
if (returnValue != null && !(returnValue instanceof ReturnStatementReturnValue)) {
continue;
}
final List<PsiElement> parameterValues = match.getParameterValues(parameter);
@@ -160,4 +160,17 @@ class TryIdenticalCatches {
void foo() throws IOException {}
private static final IOException INSTANCE = new IOException();
public boolean returning() {
try {
// work
}
catch(NumberFormatException e) {
return true;
}
catch(RuntimeException e) {
return true;
}
return false;
}
}
@@ -163,4 +163,17 @@ class TryIdenticalCatches {
void foo() throws IOException {}
private static final IOException INSTANCE = new IOException();
public boolean returning() {
try {
// work
}
catch(NumberFormatException e) {
return true;
}
<warning descr="'catch' branch identical to 'NumberFormatException' branch">catch(RuntimeException e)</warning> {
return true;
}
return false;
}
}