diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/migration/TryFinallyCanBeTryWithResourcesInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/migration/TryFinallyCanBeTryWithResourcesInspection.java index 0e91b06c3b6f..58266fae1716 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/migration/TryFinallyCanBeTryWithResourcesInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/migration/TryFinallyCanBeTryWithResourcesInspection.java @@ -143,10 +143,12 @@ public class TryFinallyCanBeTryWithResourcesInspection extends BaseInspection { separator = true; } int j = 1; - while (!unwantedChildren.contains(Integer.valueOf(j)) && j < tryBlockChildren.length - 1) { - tryStatement.getParent().addBefore(tryBlockChildren[j], tryStatement); - unwantedChildren.add(j); - j++; + if (!unwantedChildren.isEmpty()) { + while (!unwantedChildren.contains(Integer.valueOf(j)) && j < tryBlockChildren.length - 1) { + tryStatement.getParent().addBefore(tryBlockChildren[j], tryStatement); + unwantedChildren.add(j); + j++; + } } newTryStatementText.append(") {"); final int tryBlockStatementsLength = tryBlockChildren.length - 1; diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/migration/try_finally_can_be_try_with_resources/KeepResourceCallInBlock.after.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/migration/try_finally_can_be_try_with_resources/KeepResourceCallInBlock.after.java new file mode 100644 index 000000000000..c834d6358166 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/migration/try_finally_can_be_try_with_resources/KeepResourceCallInBlock.after.java @@ -0,0 +1,10 @@ +import java.io.Reader; + +class X { + public void test() throws Exception { + + try (Reader r = new FileReader("")) { + r.read(); + } + } +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/migration/try_finally_can_be_try_with_resources/KeepResourceCallInBlock.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/migration/try_finally_can_be_try_with_resources/KeepResourceCallInBlock.java new file mode 100644 index 000000000000..440351deeddb --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/migration/try_finally_can_be_try_with_resources/KeepResourceCallInBlock.java @@ -0,0 +1,14 @@ +import java.io.Reader; + +class X { + public void test() throws Exception { + Reader r = new FileReader(""); + + try { + r.read(); + } + finally { + r.close(); + } + } +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/migration/TryFinallyCanBeTryWithResourcesFixTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/migration/TryFinallyCanBeTryWithResourcesFixTest.java index 766077afdebd..bf4852456402 100644 --- a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/migration/TryFinallyCanBeTryWithResourcesFixTest.java +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/migration/TryFinallyCanBeTryWithResourcesFixTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2014 JetBrains s.r.o. + * Copyright 2000-2015 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. @@ -24,6 +24,7 @@ public class TryFinallyCanBeTryWithResourcesFixTest extends IGQuickFixesTestCase public void testComments() { doTest(); } public void testMultiple() { doTest(); } public void testDependents() { doTest(); } + public void testKeepResourceCallInBlock() { doTest(); } @Override public void setUp() throws Exception {