diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/migration/TryWithIdenticalCatchesInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/migration/TryWithIdenticalCatchesInspection.java index bbe0ea3aa2df..a723ab5dad6a 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/migration/TryWithIdenticalCatchesInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/migration/TryWithIdenticalCatchesInspection.java @@ -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 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; } } diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/errorhandling/try_identical_catches/NonDisjunctTypes.after.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/errorhandling/try_identical_catches/NonDisjunctTypes.after.java new file mode 100644 index 000000000000..496224ac20ee --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/errorhandling/try_identical_catches/NonDisjunctTypes.after.java @@ -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(); + } + } +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/errorhandling/try_identical_catches/NonDisjunctTypes.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/errorhandling/try_identical_catches/NonDisjunctTypes.java new file mode 100644 index 000000000000..b80d01159f0c --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/errorhandling/try_identical_catches/NonDisjunctTypes.java @@ -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(); + } + catch (Ex1 ex) { + ex.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/migration/TryWithIdenticalCatchesTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/migration/TryWithIdenticalCatchesTest.java index 55682f6bc880..7ad5270607ba 100644 --- a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/migration/TryWithIdenticalCatchesTest.java +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/migration/TryWithIdenticalCatchesTest.java @@ -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