mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
skip exceptions returned from lambdas ThrowableIsNotThrown (IDEA-140336)
This commit is contained in:
+14
-52
@@ -80,8 +80,7 @@ public class ThrowableInstanceNeverThrownInspection extends BaseInspection {
|
||||
@Override
|
||||
public void visitNewExpression(PsiNewExpression expression) {
|
||||
super.visitNewExpression(expression);
|
||||
if (!TypeUtils.expressionHasTypeOrSubtype(expression,
|
||||
CommonClassNames.JAVA_LANG_THROWABLE)) {
|
||||
if (!TypeUtils.expressionHasTypeOrSubtype(expression, CommonClassNames.JAVA_LANG_THROWABLE)) {
|
||||
return;
|
||||
}
|
||||
final PsiElement parent = getParent(expression.getParent());
|
||||
@@ -89,47 +88,16 @@ public class ThrowableInstanceNeverThrownInspection extends BaseInspection {
|
||||
parent instanceof PsiReturnStatement) {
|
||||
return;
|
||||
}
|
||||
if (PsiTreeUtil.getParentOfType(parent, PsiCallExpression.class) !=
|
||||
null) {
|
||||
if (PsiTreeUtil.getParentOfType(parent, PsiCallExpression.class) != null) {
|
||||
return;
|
||||
}
|
||||
final PsiElement typedParent =
|
||||
PsiTreeUtil.getParentOfType(expression,
|
||||
PsiAssignmentExpression.class,
|
||||
PsiVariable.class);
|
||||
final PsiLocalVariable variable;
|
||||
if (typedParent instanceof PsiAssignmentExpression) {
|
||||
final PsiAssignmentExpression assignmentExpression =
|
||||
(PsiAssignmentExpression)typedParent;
|
||||
final PsiExpression rhs = assignmentExpression.getRExpression();
|
||||
if (!PsiTreeUtil.isAncestor(rhs, expression, false)) {
|
||||
return;
|
||||
}
|
||||
final PsiExpression lhs = assignmentExpression.getLExpression();
|
||||
if (!(lhs instanceof PsiReferenceExpression)) {
|
||||
return;
|
||||
}
|
||||
final PsiReferenceExpression referenceExpression =
|
||||
(PsiReferenceExpression)lhs;
|
||||
final PsiElement target = referenceExpression.resolve();
|
||||
if (!(target instanceof PsiLocalVariable)) {
|
||||
return;
|
||||
}
|
||||
variable = (PsiLocalVariable)target;
|
||||
final PsiElement typedParent = PsiTreeUtil.getParentOfType(expression, PsiAssignmentExpression.class, PsiVariable.class);
|
||||
final PsiElement variable = ThrowableResultOfMethodCallIgnoredInspection.getVariable(typedParent, expression);
|
||||
if (variable == null) {
|
||||
return;
|
||||
}
|
||||
else if (typedParent instanceof PsiVariable) {
|
||||
if (!(typedParent instanceof PsiLocalVariable)) {
|
||||
return;
|
||||
}
|
||||
variable = (PsiLocalVariable)typedParent;
|
||||
}
|
||||
else {
|
||||
variable = null;
|
||||
}
|
||||
if (variable != null) {
|
||||
final Query<PsiReference> query =
|
||||
ReferencesSearch.search(variable,
|
||||
variable.getUseScope());
|
||||
if (variable instanceof PsiLocalVariable) {
|
||||
final Query<PsiReference> query = ReferencesSearch.search(variable, variable.getUseScope());
|
||||
for (PsiReference reference : query) {
|
||||
final PsiElement usage = reference.getElement();
|
||||
PsiElement usageParent = usage.getParent();
|
||||
@@ -140,8 +108,7 @@ public class ThrowableInstanceNeverThrownInspection extends BaseInspection {
|
||||
usageParent instanceof PsiReturnStatement) {
|
||||
return;
|
||||
}
|
||||
if (PsiTreeUtil.getParentOfType(usageParent,
|
||||
PsiCallExpression.class) != null) {
|
||||
if (PsiTreeUtil.getParentOfType(usageParent, PsiCallExpression.class) != null) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -171,12 +138,9 @@ public class ThrowableInstanceNeverThrownInspection extends BaseInspection {
|
||||
if (!(grandParent instanceof PsiMethodCallExpression)) {
|
||||
return null;
|
||||
}
|
||||
final PsiMethodCallExpression methodCallExpression =
|
||||
(PsiMethodCallExpression)grandParent;
|
||||
final PsiReferenceExpression methodExpression =
|
||||
methodCallExpression.getMethodExpression();
|
||||
@NonNls final String methodName =
|
||||
methodExpression.getReferenceName();
|
||||
final PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression)grandParent;
|
||||
final PsiReferenceExpression methodExpression = methodCallExpression.getMethodExpression();
|
||||
@NonNls final String methodName = methodExpression.getReferenceName();
|
||||
if (!"initCause".equals(methodName)) {
|
||||
return null;
|
||||
}
|
||||
@@ -185,13 +149,11 @@ public class ThrowableInstanceNeverThrownInspection extends BaseInspection {
|
||||
if (method == null) {
|
||||
return null;
|
||||
}
|
||||
final PsiParameterList parameterList =
|
||||
method.getParameterList();
|
||||
final PsiParameterList parameterList = method.getParameterList();
|
||||
if (parameterList.getParametersCount() != 1) {
|
||||
return null;
|
||||
}
|
||||
final PsiParameter[] parameters =
|
||||
parameterList.getParameters();
|
||||
final PsiParameter[] parameters = parameterList.getParameters();
|
||||
final PsiType type = parameters[0].getType();
|
||||
if (!type.equalsToText(CommonClassNames.JAVA_LANG_THROWABLE)) {
|
||||
return null;
|
||||
|
||||
+34
-39
@@ -27,8 +27,7 @@ import com.siyeh.ig.BaseInspectionVisitor;
|
||||
import com.siyeh.ig.psiutils.TypeUtils;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class ThrowableResultOfMethodCallIgnoredInspection
|
||||
extends BaseInspection {
|
||||
public class ThrowableResultOfMethodCallIgnoredInspection extends BaseInspection {
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
@@ -66,11 +65,11 @@ public class ThrowableResultOfMethodCallIgnoredInspection
|
||||
}
|
||||
if (parent instanceof PsiReturnStatement ||
|
||||
parent instanceof PsiThrowStatement ||
|
||||
parent instanceof PsiExpressionList) {
|
||||
parent instanceof PsiExpressionList ||
|
||||
parent instanceof PsiLambdaExpression) {
|
||||
return;
|
||||
}
|
||||
if (!TypeUtils.expressionHasTypeOrSubtype(expression,
|
||||
CommonClassNames.JAVA_LANG_THROWABLE)) {
|
||||
if (!TypeUtils.expressionHasTypeOrSubtype(expression, CommonClassNames.JAVA_LANG_THROWABLE)) {
|
||||
return;
|
||||
}
|
||||
final PsiMethod method = expression.resolveMethod();
|
||||
@@ -79,8 +78,7 @@ public class ThrowableResultOfMethodCallIgnoredInspection
|
||||
}
|
||||
if (!method.hasModifierProperty(PsiModifier.STATIC)) {
|
||||
final PsiClass containingClass = method.getContainingClass();
|
||||
if (InheritanceUtil.isInheritor(containingClass,
|
||||
CommonClassNames.JAVA_LANG_THROWABLE)) {
|
||||
if (InheritanceUtil.isInheritor(containingClass, CommonClassNames.JAVA_LANG_THROWABLE)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -91,39 +89,13 @@ public class ThrowableResultOfMethodCallIgnoredInspection
|
||||
return;
|
||||
}
|
||||
|
||||
final PsiLocalVariable variable;
|
||||
if (parent instanceof PsiAssignmentExpression) {
|
||||
final PsiAssignmentExpression assignmentExpression =
|
||||
(PsiAssignmentExpression)parent;
|
||||
final PsiExpression rhs = assignmentExpression.getRExpression();
|
||||
if (!PsiTreeUtil.isAncestor(rhs, expression, false)) {
|
||||
return;
|
||||
}
|
||||
final PsiExpression lhs = assignmentExpression.getLExpression();
|
||||
if (!(lhs instanceof PsiReferenceExpression)) {
|
||||
return;
|
||||
}
|
||||
final PsiReferenceExpression referenceExpression =
|
||||
(PsiReferenceExpression)lhs;
|
||||
final PsiElement target = referenceExpression.resolve();
|
||||
if (!(target instanceof PsiLocalVariable)) {
|
||||
return;
|
||||
}
|
||||
variable = (PsiLocalVariable)target;
|
||||
final PsiElement var = getVariable(parent, expression);
|
||||
if (var == null) {
|
||||
return;
|
||||
}
|
||||
else if (parent instanceof PsiVariable) {
|
||||
if (!(parent instanceof PsiLocalVariable)) {
|
||||
return;
|
||||
}
|
||||
variable = (PsiLocalVariable)parent;
|
||||
}
|
||||
else {
|
||||
variable = null;
|
||||
}
|
||||
if (variable != null) {
|
||||
final Query<PsiReference> query =
|
||||
ReferencesSearch.search(variable,
|
||||
variable.getUseScope());
|
||||
|
||||
if (var instanceof PsiLocalVariable) {
|
||||
final Query<PsiReference> query = ReferencesSearch.search(var, var.getUseScope());
|
||||
for (PsiReference reference : query) {
|
||||
final PsiElement usage = reference.getElement();
|
||||
PsiElement usageParent = usage.getParent();
|
||||
@@ -140,4 +112,27 @@ public class ThrowableResultOfMethodCallIgnoredInspection
|
||||
registerMethodCallError(expression);
|
||||
}
|
||||
}
|
||||
|
||||
protected static PsiElement getVariable(PsiElement parent, PsiElement expression) {
|
||||
if (parent instanceof PsiAssignmentExpression) {
|
||||
final PsiAssignmentExpression assignmentExpression = (PsiAssignmentExpression)parent;
|
||||
final PsiExpression rhs = assignmentExpression.getRExpression();
|
||||
if (!PsiTreeUtil.isAncestor(rhs, expression, false)) {
|
||||
return null;
|
||||
}
|
||||
final PsiExpression lhs = assignmentExpression.getLExpression();
|
||||
if (!(lhs instanceof PsiReferenceExpression)) {
|
||||
return null;
|
||||
}
|
||||
final PsiReferenceExpression referenceExpression = (PsiReferenceExpression)lhs;
|
||||
final PsiElement target = referenceExpression.resolve();
|
||||
if (!(target instanceof PsiLocalVariable)) {
|
||||
return null;
|
||||
}
|
||||
return target;
|
||||
}
|
||||
else {
|
||||
return parent;
|
||||
}
|
||||
}
|
||||
}
|
||||
+14
-1
@@ -1,7 +1,6 @@
|
||||
package com.siyeh.igtest.bugs.throwable_result_of_method_call_ignored;
|
||||
|
||||
|
||||
|
||||
public class ThrowableResultOfMethodCallIgnored {
|
||||
public static void test() {
|
||||
try {
|
||||
@@ -38,4 +37,18 @@ class ResWrap {
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
interface I {
|
||||
Exception get();
|
||||
}
|
||||
|
||||
class LambdaReturn {
|
||||
{
|
||||
I i = () -> createException("foo");
|
||||
}
|
||||
|
||||
private RuntimeException createException(String message) {
|
||||
return new RuntimeException(message);
|
||||
}
|
||||
}
|
||||
-1
@@ -1,7 +1,6 @@
|
||||
package com.siyeh.ig.bugs;
|
||||
|
||||
import com.intellij.codeInspection.InspectionProfileEntry;
|
||||
import com.siyeh.ig.IGInspectionTestCase;
|
||||
import com.siyeh.ig.LightInspectionTestCase;
|
||||
|
||||
public class ThrowableResultOfMethodCallIgnoredInspectionTest extends LightInspectionTestCase {
|
||||
|
||||
Reference in New Issue
Block a user