diff --git a/java/java-impl-inspections/src/com/intellij/codeInspection/SlowListContainsAllInspection.java b/java/java-impl-inspections/src/com/intellij/codeInspection/SlowListContainsAllInspection.java index 6c9d1ef332cc..1372000b138e 100644 --- a/java/java-impl-inspections/src/com/intellij/codeInspection/SlowListContainsAllInspection.java +++ b/java/java-impl-inspections/src/com/intellij/codeInspection/SlowListContainsAllInspection.java @@ -1,4 +1,4 @@ -// Copyright 2000-2020 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. +// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package com.intellij.codeInspection; import com.intellij.codeInspection.dataFlow.rangeSet.LongRangeSet; @@ -6,10 +6,12 @@ import com.intellij.java.JavaBundle; import com.intellij.openapi.project.Project; import com.intellij.psi.*; import com.intellij.psi.codeStyle.JavaCodeStyleManager; +import com.intellij.psi.util.PsiUtil; import com.intellij.util.ObjectUtils; import com.siyeh.ig.callMatcher.CallMatcher; import com.siyeh.ig.psiutils.CommentTracker; import com.siyeh.ig.psiutils.ExpressionUtils; +import com.siyeh.ig.psiutils.TestUtils; import org.jetbrains.annotations.Nls; import org.jetbrains.annotations.NotNull; @@ -23,16 +25,16 @@ public class SlowListContainsAllInspection extends AbstractBaseJavaLocalInspecti @Override public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) { return new JavaElementVisitor() { - @Override public void visitMethodCallExpression(PsiMethodCallExpression call) { + if (TestUtils.isInTestCode(call)) return; super.visitMethodCallExpression(call); if (!LIST_CONTAINS_ALL.test(call)) return; PsiReferenceExpression expression = call.getMethodExpression(); final PsiExpression qualifier = ExpressionUtils.getEffectiveQualifier(expression); if (qualifier == null) return; final LongRangeSet listSizeRange = SlowAbstractSetRemoveAllInspection.getSizeRangeOfCollection(qualifier); - if (listSizeRange.isEmpty() || listSizeRange.max() <= 1) return; + if (listSizeRange.isEmpty() || listSizeRange.max() <= 5) return; holder.registerProblem(call, JavaBundle.message("inspection.slow.list.contains.all.description"), ProblemHighlightType.WARNING, @@ -69,8 +71,10 @@ public class SlowListContainsAllInspection extends AbstractBaseJavaLocalInspecti if (call == null) return; final PsiExpression qualifier = call.getMethodExpression().getQualifierExpression(); if (qualifier == null) return; + PsiExpression strippedQualifier = PsiUtil.deparenthesizeExpression(qualifier); + if (strippedQualifier == null) return; final CommentTracker ct = new CommentTracker(); - PsiElement result = ct.replace(qualifier, "new java.util.HashSet<>(" + ct.text(qualifier) + ")"); + PsiElement result = ct.replace(qualifier, "new java.util.HashSet<>(" + ct.text(strippedQualifier) + ")"); JavaCodeStyleManager.getInstance(project).shortenClassReferences(result); } } diff --git a/java/java-impl/src/inspectionDescriptions/SlowListContainsAll.html b/java/java-impl/src/inspectionDescriptions/SlowListContainsAll.html index 5820e8368377..3f89b698e4e4 100644 --- a/java/java-impl/src/inspectionDescriptions/SlowListContainsAll.html +++ b/java/java-impl/src/inspectionDescriptions/SlowListContainsAll.html @@ -2,7 +2,9 @@
Reports calls tocontainsAll() on java.util.List.
- The time complexity of this method call is O(n·m). When the list is large, this can be an expensive operation. + The time complexity of this method call is O(n·m), where n is the number of elements in the list on which + the method is called, and m is the number of elements in the collection passed to the method as a parameter. + When the list is large, this can be an expensive operation.
The quick-fix wraps the list in new java.util.HashSet<>() since the time required to create
diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/slowListContainsAll/after1.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/slowListContainsAll/after1.java
index 748d0724640d..22e278395585 100644
--- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/slowListContainsAll/after1.java
+++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/slowListContainsAll/after1.java
@@ -1,10 +1,12 @@
-// "Wrap 'list' in 'HashSet' constructor" "true"
+// "Fix all 'Call to 'list.containsAll(collection)' may have poor performance' problems in file" "true"
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
class Test {
- boolean check(List