[java-inspections] Fixes after review (IJ-CR-18136)

GitOrigin-RevId: a0864bcf774d435dc54ab63de1966f1a7779236f
This commit is contained in:
Andrey.Cherkasov
2022-01-12 15:15:03 +00:00
committed by intellij-monorepo-bot
parent 7a61733ce2
commit eb254acc5c
4 changed files with 21 additions and 11 deletions
@@ -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);
}
}
@@ -2,7 +2,9 @@
<body>
Reports calls to <code>containsAll()</code> on <code>java.util.List</code>.
<p>
The time complexity of this method call is O(n&middot;m). When the list is large, this can be an expensive operation.
The time complexity of this method call is O(n&middot;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.
</p>
<p>
The quick-fix wraps the list in <code>new java.util.HashSet&lt;&gt;()</code> since the time required to create
@@ -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<String> list, Collection<String> collection) {
return new HashSet<>(list).containsAll(collection);
void check(List<String> list1, List<String> list2, Collection<String> collection) {
new HashSet<>(list1).containsAll(collection);
new HashSet<>(list1).containsAll(collection);
new HashSet<>(Math.random() > 0.5 ? list1 : list2).containsAll(collection);
}
}
@@ -1,9 +1,11 @@
// "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.List;
class Test {
boolean check(List<String> list, Collection<String> collection) {
return list.containsAll<caret>(collection);
void check(List<String> list1, List<String> list2, Collection<String> collection) {
list1.containsAll<caret>(collection);
(((list1))).containsAll(collection);
((((Math.random() > 0.5 ? list1 : list2)))).containsAll(collection);
}
}