mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IG: fix disjunct exception type check (IDEA-165847)
This commit is contained in:
+6
-7
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
+22
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
+25
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
+13
-4
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user