From 8a7e1551915bee52feb7a7ee79109fadca5f56da Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Mon, 14 Oct 2019 13:58:35 +0200 Subject: [PATCH] IG: don't on the order of results of ReferencesSearch.search() in "Type may be weakened" inspection GitOrigin-RevId: ef0776fdba0befd950342a8b63816a5537af8966 --- .../siyeh/ig/psiutils/WeakestTypeFinder.java | 38 +++++-------------- 1 file changed, 9 insertions(+), 29 deletions(-) diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/WeakestTypeFinder.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/WeakestTypeFinder.java index 88124142a9b6..151be7828311 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/WeakestTypeFinder.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/WeakestTypeFinder.java @@ -1,18 +1,4 @@ -/* - * Copyright 2008-2017 Bas Leijdekkers - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.siyeh.ig.psiutils; import com.intellij.openapi.progress.ProgressManager; @@ -67,7 +53,7 @@ public class WeakestTypeFinder { if (variableOrMethodClass == null || variableOrMethodClass instanceof PsiTypeParameter) { return Collections.emptyList(); } - Set weakestTypeClasses = new HashSet<>(); + final Set weakestTypeClasses = new HashSet<>(); final GlobalSearchScope scope = variableOrMethod.getResolveScope(); final JavaPsiFacade facade = JavaPsiFacade.getInstance(variableOrMethod.getProject()); final PsiClass lowerBoundClass; @@ -108,8 +94,8 @@ public class WeakestTypeFinder { referenceParent = PsiUtil.skipParenthesizedExprUp(referenceElement.getParent()); } final PsiElement referenceGrandParent = referenceParent.getParent(); - if (reference instanceof PsiMethodReferenceExpression) { - final PsiMethodReferenceExpression methodReferenceExpression = (PsiMethodReferenceExpression)reference; + if (referenceElement instanceof PsiMethodReferenceExpression) { + final PsiMethodReferenceExpression methodReferenceExpression = (PsiMethodReferenceExpression)referenceElement; final PsiType type = methodReferenceExpression.getFunctionalInterfaceType(); final PsiType returnType = LambdaUtil.getFunctionalInterfaceReturnType(type); if (!PsiType.VOID.equals(returnType) && !checkType(returnType, weakestTypeClasses)) { @@ -125,12 +111,9 @@ public class WeakestTypeFinder { } } else if (referenceParent instanceof PsiLambdaExpression) { - PsiLambdaExpression lambda = (PsiLambdaExpression)referenceParent; - PsiClassType returnType = ObjectUtils.tryCast(LambdaUtil.getFunctionalInterfaceReturnType(lambda), PsiClassType.class); - if (returnType == null) return Collections.emptyList(); - PsiClass resolvedClass = returnType.resolve(); - if (resolvedClass == null) return Collections.emptyList(); - weakestTypeClasses.add(resolvedClass); + final PsiLambdaExpression lambda = (PsiLambdaExpression)referenceParent; + final PsiClassType returnType = ObjectUtils.tryCast(LambdaUtil.getFunctionalInterfaceReturnType(lambda), PsiClassType.class); + checkType(returnType, weakestTypeClasses); } else if (referenceGrandParent instanceof PsiMethodCallExpression) { final PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression)referenceGrandParent; @@ -140,11 +123,8 @@ public class WeakestTypeFinder { } } else if (referenceParent instanceof PsiResourceExpression) { - PsiClass closeable = facade.findClass(CommonClassNames.JAVA_LANG_AUTO_CLOSEABLE, scope); - if (closeable == null || variableOrMethodClass.equals(closeable)) { - return Collections.emptyList(); - } - weakestTypeClasses.add(closeable); + final PsiClass closeable = facade.findClass(CommonClassNames.JAVA_LANG_AUTO_CLOSEABLE, scope); + checkClass(closeable, weakestTypeClasses); } else if (referenceParent instanceof PsiAssignmentExpression) { final PsiAssignmentExpression assignmentExpression = (PsiAssignmentExpression)referenceParent;