IG: fix disjunct exception type check (IDEA-165847)

This commit is contained in:
Bas Leijdekkers
2016-12-23 22:05:00 +01:00
parent a1ff14e61c
commit 8b5b56366c
4 changed files with 66 additions and 11 deletions
@@ -36,6 +36,7 @@ import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
/**
@@ -252,14 +253,12 @@ public class TryWithIdenticalCatchesInspection extends BaseInspection {
}
return;
}
final int size = out.size();
for (int i = 0; i < size; i++) {
final PsiType collectedType = out.get(i);
for (Iterator<PsiType> iterator = out.iterator(); iterator.hasNext(); ) {
final PsiType collectedType = iterator.next();
if (TypeConversionUtil.isAssignable(type, collectedType)) {
out.remove(i);
out.add(type);
return;
} else if (TypeConversionUtil.isAssignable(collectedType, type)) {
iterator.remove();
}
else if (TypeConversionUtil.isAssignable(collectedType, type)) {
return;
}
}
@@ -0,0 +1,22 @@
public class NonDisjunctTypes {
static class Ex1 extends Exception {}
static class Ex2 extends Ex1 {}
static class Ex3 extends Ex1 {}
public void test() {
try {
if(Math.random() > 0.5) {
throw new Ex1();
}
if(Math.random() > 0.5) {
throw new Ex2();
}
if(Math.random() > 0.5) {
throw new Ex3();
}
}
catch(RuntimeException | Ex1 ex) {
ex.printStackTrace();
}
}
}
@@ -0,0 +1,25 @@
public class NonDisjunctTypes {
static class Ex1 extends Exception {}
static class Ex2 extends Ex1 {}
static class Ex3 extends Ex1 {}
public void test() {
try {
if(Math.random() > 0.5) {
throw new Ex1();
}
if(Math.random() > 0.5) {
throw new Ex2();
}
if(Math.random() > 0.5) {
throw new Ex3();
}
}
catch(RuntimeException | Ex2 | Ex3 ex) {
ex.printStackTrace();
}
<warning descr="'catch' branch identical to 'RuntimeException | Ex2 | Ex3' branch">catch <caret>(Ex1 ex)</warning> {
ex.printStackTrace();
}
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2011 JetBrains s.r.o.
* Copyright 2000-2016 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.
@@ -23,14 +23,23 @@ import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase;
* @author yole
*/
public class TryWithIdenticalCatchesTest extends LightCodeInsightFixtureTestCase {
public void test() {
public void testTryIdenticalCatches() {
doTest();
}
public void testNonDisjunctTypes() {
doTest();
}
public void doTest() {
String name = getTestName(false);
myFixture.enableInspections(TryWithIdenticalCatchesInspection.class);
myFixture.configureByFile("com/siyeh/igtest/errorhandling/try_identical_catches/TryIdenticalCatches.java");
myFixture.configureByFile("com/siyeh/igtest/errorhandling/try_identical_catches/" + name + ".java");
myFixture.checkHighlighting(true, false, false);
IntentionAction intention = myFixture.findSingleIntention("Collapse 'catch' blocks");
assertNotNull(intention);
myFixture.launchAction(intention);
myFixture.checkResultByFile("com/siyeh/igtest/errorhandling/try_identical_catches/TryIdenticalCatches.after.java");
myFixture.checkResultByFile("com/siyeh/igtest/errorhandling/try_identical_catches/" + name + ".after.java");
}
@Override