IG: hide strict throw from finally mode behind option (IDEA-154184) appcode/146.1097 appcode/146.1101 clion/146.1091 clion/146.1102 dbe/146.1092 idea/146.1093 phpstorm/146.1094 pycharm/146.1095 pycharm/146.1099 rubymine/146.1096 webstorm/146.1100
authorBas Leijdekkers <basleijdekkers@gmail.com>
Wed, 6 Apr 2016 20:42:15 +0000 (22:42 +0200)
committerBas Leijdekkers <basleijdekkers@gmail.com>
Wed, 6 Apr 2016 20:53:23 +0000 (22:53 +0200)
plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/InspectionGadgetsBundle.properties
plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/errorhandling/ThrowFromFinallyBlockInspection.java

index 1a7bf3eb15232792fd7ba018f5b595c733eca312..3cb40faa79c72bd2d530a3a306f71005966d9640 100644 (file)
@@ -956,6 +956,7 @@ finally.block.cannot.complete.normally.problem.descriptor=<code>#ref</code> bloc
 empty.try.block.problem.descriptor=Empty <code>#ref</code> block #loc
 throw.from.finally.block.problem.descriptor=<code>#ref</code> inside 'finally' block #loc
 possible.throw.from.finally.block.problem.descriptor=<code>{0}</code> might be thrown inside ''finally'' block #loc
 empty.try.block.problem.descriptor=Empty <code>#ref</code> block #loc
 throw.from.finally.block.problem.descriptor=<code>#ref</code> inside 'finally' block #loc
 possible.throw.from.finally.block.problem.descriptor=<code>{0}</code> might be thrown inside ''finally'' block #loc
+throw,from.finally.block.everywhere.option=Warn everywhere declared exceptions may be thrown
 throw.caught.locally.problem.descriptor=<code>#ref</code> caught by containing 'try' statement #loc
 throw.caught.locally.ignore.option=Ignore rethrown exceptions
 return.from.finally.block.problem.descriptor=<code>#ref</code> inside 'finally' block #loc
 throw.caught.locally.problem.descriptor=<code>#ref</code> caught by containing 'try' statement #loc
 throw.caught.locally.ignore.option=Ignore rethrown exceptions
 return.from.finally.block.problem.descriptor=<code>#ref</code> inside 'finally' block #loc
index 6c6563388cb19f0f2075bea806d38798ce660d19..6946c620819857f329b5f92b96995a3af5955531 100644 (file)
@@ -1,5 +1,5 @@
 /*
 /*
- * Copyright 2003-2015 Dave Griffith, Bas Leijdekkers
+ * Copyright 2003-2016 Dave Griffith, Bas Leijdekkers
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
 package com.siyeh.ig.errorhandling;
 
 import com.intellij.codeInsight.ExceptionUtil;
 package com.siyeh.ig.errorhandling;
 
 import com.intellij.codeInsight.ExceptionUtil;
+import com.intellij.codeInspection.ui.SingleCheckboxOptionsPanel;
 import com.intellij.psi.*;
 import com.intellij.psi.util.PsiTreeUtil;
 import com.siyeh.InspectionGadgetsBundle;
 import com.intellij.psi.*;
 import com.intellij.psi.util.PsiTreeUtil;
 import com.siyeh.InspectionGadgetsBundle;
@@ -27,13 +28,14 @@ import com.siyeh.ig.psiutils.VariableAccessUtils;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
 
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
 
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.OutputStream;
+import javax.swing.*;
 import java.util.List;
 
 public class ThrowFromFinallyBlockInspection extends BaseInspection {
 
 import java.util.List;
 
 public class ThrowFromFinallyBlockInspection extends BaseInspection {
 
+  @SuppressWarnings("PublicField")
+  public boolean warnOnAllExceptions = false;
+
   @Override
   @NotNull
   public String getDisplayName() {
   @Override
   @NotNull
   public String getDisplayName() {
@@ -46,6 +48,13 @@ public class ThrowFromFinallyBlockInspection extends BaseInspection {
     return true;
   }
 
     return true;
   }
 
+  @Nullable
+  @Override
+  public JComponent createOptionsPanel() {
+    return new SingleCheckboxOptionsPanel(InspectionGadgetsBundle.message("throw,from.finally.block.everywhere.option"),
+                                          this, "warnOnAllExceptions");
+  }
+
   @Override
   @NotNull
   protected String buildErrorString(Object... infos) {
   @Override
   @NotNull
   protected String buildErrorString(Object... infos) {
@@ -63,11 +72,14 @@ public class ThrowFromFinallyBlockInspection extends BaseInspection {
     return new ThrowFromFinallyBlockVisitor();
   }
 
     return new ThrowFromFinallyBlockVisitor();
   }
 
-  private static class ThrowFromFinallyBlockVisitor extends BaseInspectionVisitor {
+  private class ThrowFromFinallyBlockVisitor extends BaseInspectionVisitor {
 
     @Override
 
     @Override
-    public void visitMethodCallExpression(PsiMethodCallExpression expression) {
-      super.visitMethodCallExpression(expression);
+    public void visitCallExpression(PsiCallExpression expression) {
+      super.visitCallExpression(expression);
+      if (!warnOnAllExceptions) {
+        return;
+      }
       final List<PsiClassType> exceptions = ExceptionUtil.getThrownExceptions(expression);
       if (exceptions.isEmpty()) {
         return;
       final List<PsiClassType> exceptions = ExceptionUtil.getThrownExceptions(expression);
       if (exceptions.isEmpty()) {
         return;
@@ -75,7 +87,12 @@ public class ThrowFromFinallyBlockInspection extends BaseInspection {
       for (PsiClassType exception : exceptions) {
         final PsiCodeBlock finallyBlock = getContainingFinallyBlock(expression, exception);
         if (finallyBlock != null && isHidingOfPreviousException(finallyBlock, expression)) {
       for (PsiClassType exception : exceptions) {
         final PsiCodeBlock finallyBlock = getContainingFinallyBlock(expression, exception);
         if (finallyBlock != null && isHidingOfPreviousException(finallyBlock, expression)) {
-          registerMethodCallError(expression, exception);
+          if (expression instanceof PsiMethodCallExpression) {
+            registerMethodCallError((PsiMethodCallExpression)expression, exception);
+          }
+          else if (expression instanceof PsiNewExpression) {
+            registerNewExpressionError((PsiNewExpression)expression, exception);
+          }
           return;
         }
       }
           return;
         }
       }
@@ -109,7 +126,7 @@ public class ThrowFromFinallyBlockInspection extends BaseInspection {
       }
     }
 
       }
     }
 
-    private static boolean isHidingOfPreviousException(PsiCodeBlock finallyBlock, PsiElement throwElement) {
+    private boolean isHidingOfPreviousException(PsiCodeBlock finallyBlock, PsiElement throwElement) {
       final PsiElement parent = finallyBlock.getParent();
       if (!(parent instanceof PsiTryStatement)) {
         // never reached
       final PsiElement parent = finallyBlock.getParent();
       if (!(parent instanceof PsiTryStatement)) {
         // never reached
@@ -142,9 +159,7 @@ public class ThrowFromFinallyBlockInspection extends BaseInspection {
     }
 
     @Nullable
     }
 
     @Nullable
-    public static <T extends PsiElement> T getParentOfType(@Nullable PsiElement element,
-                                                           @NotNull Class<T> aClass,
-                                                           @NotNull PsiElement stopAt) {
+    public <T extends PsiElement> T getParentOfType(@Nullable PsiElement element, @NotNull Class<T> aClass, @NotNull PsiElement stopAt) {
       if (element == null || element instanceof PsiFile) return null;
       element = element.getParent();
 
       if (element == null || element instanceof PsiFile) return null;
       element = element.getParent();