From a5ff9dbb3f11d8bd0db9d5ee8f7d39ce81903c1b Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Sat, 30 Jul 2016 00:33:55 +0200 Subject: [PATCH] IG: recognize when catch blocks containing a return statement are identical (IDEA-159182) --- .../TryWithIdenticalCatchesInspection.java | 8 +++++++- .../TryIdenticalCatches.after.java | 13 +++++++++++++ .../try_identical_catches/TryIdenticalCatches.java | 13 +++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) 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 7d7a58124e43..5ef56eeb9d25 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/migration/TryWithIdenticalCatchesInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/migration/TryWithIdenticalCatchesInspection.java @@ -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 parameterValues = match.getParameterValues(parameter); diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/errorhandling/try_identical_catches/TryIdenticalCatches.after.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/errorhandling/try_identical_catches/TryIdenticalCatches.after.java index 8f8699f0fe52..dc837921b878 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/errorhandling/try_identical_catches/TryIdenticalCatches.after.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/errorhandling/try_identical_catches/TryIdenticalCatches.after.java @@ -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; + } } \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/errorhandling/try_identical_catches/TryIdenticalCatches.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/errorhandling/try_identical_catches/TryIdenticalCatches.java index aded25be4d6d..3025c200b734 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/errorhandling/try_identical_catches/TryIdenticalCatches.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/errorhandling/try_identical_catches/TryIdenticalCatches.java @@ -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; + } + catch(RuntimeException e) { + return true; + } + return false; + } } \ No newline at end of file