IDEA-74322 (Bogus trigger of inspection 'Result of X not thrown')

This commit is contained in:
Bas Leijdekkers
2011-09-15 16:37:21 +02:00
parent 9132ad3c4d
commit c16b41bb48
5 changed files with 45 additions and 3 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2007-2010 Bas Leijdekkers
* Copyright 2007-2011 Bas Leijdekkers
*
* 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 @@ import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.psiutils.TypeUtils;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
public class ThrowableInstanceNeverThrownInspection extends BaseInspection {
@@ -167,7 +168,8 @@ public class ThrowableInstanceNeverThrownInspection extends BaseInspection {
(PsiMethodCallExpression) grandParent;
final PsiReferenceExpression methodExpression =
methodCallExpression.getMethodExpression();
final String methodName = methodExpression.getReferenceName();
@NonNls final String methodName =
methodExpression.getReferenceName();
if (!"initCause".equals(methodName)) {
return null;
}
@@ -65,7 +65,8 @@ public class ThrowableResultOfMethodCallIgnoredInspection
parent = parent.getParent();
}
if (parent instanceof PsiReturnStatement ||
parent instanceof PsiThrowStatement) {
parent instanceof PsiThrowStatement ||
parent instanceof PsiExpressionList) {
return;
}
if (!TypeUtils.expressionHasTypeOrSubtype(expression,
@@ -0,0 +1,18 @@
package com.siyeh.igtest.bugs.throwable_result_of_method_call_ignored;
public class A {
public static void test() {
try {
firstNonNull(new Throwable(), null);
}
catch (Exception e) {
throw new RuntimeException(firstNonNull(e.getCause(), e));
}
}
public static <T> T firstNonNull(T first, T second) {
return first != null ? first : second;
}
}
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>A.java</file>
<line>8</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Throwable result of method call ignored</problem_class>
<description>Result of &lt;code&gt;firstNonNull()&lt;/code&gt; not thrown #loc</description>
</problem>
</problems>
@@ -0,0 +1,12 @@
package com.siyeh.ig.bugs;
import com.siyeh.ig.IGInspectionTestCase;
public class ThrowableResultOfMethodCallIgnoredInspectionTest
extends IGInspectionTestCase {
public void test() throws Exception {
doTest("com/siyeh/igtest/bugs/throwable_result_of_method_call_ignored",
new ThrowableResultOfMethodCallIgnoredInspection());
}
}