[java-inspections] MismatchedCollectionQueryUpdateInspection: better assignment processing (IDEA-349156)

GitOrigin-RevId: a9a07b80381d309a1e70c71953c8030e20ab5e67
This commit is contained in:
Tagir Valeev
2024-04-03 15:36:55 +00:00
committed by intellij-monorepo-bot
parent f2d0a225d9
commit 74ec1e0f2d
4 changed files with 56 additions and 35 deletions
@@ -216,34 +216,31 @@ public final class MismatchedCollectionQueryUpdateInspection extends BaseInspect
processQualifiedCall(qualifiedCall);
return;
}
PsiElement parent = reference.getParent();
PsiElement grandParent = skipAssigmentExprUp(parent);
if (parent instanceof PsiExpressionList ||
(parent instanceof PsiAssignmentExpression && grandParent instanceof PsiExpressionList)) {
PsiExpressionList args = (PsiExpressionList)(parent instanceof PsiExpressionList ? parent : grandParent);
PsiCallExpression surroundingCall = ObjectUtils.tryCast(args.getParent(), PsiCallExpression.class);
if (surroundingCall != null) {
if (surroundingCall instanceof PsiMethodCallExpression &&
processCollectionMethods((PsiMethodCallExpression)surroundingCall, reference)) {
return;
}
makeQueried();
if (!isQueryMethod(surroundingCall) && !COLLECTION_SAFE_ARGUMENT_METHODS.matches(surroundingCall)) {
makeUpdated();
}
return;
}
}
if (parent instanceof PsiMethodReferenceExpression) {
processQualifiedMethodReference(((PsiMethodReferenceExpression)parent));
if (ExpressionUtils.isVoidContext(reference)) {
return;
}
if (parent instanceof PsiForeachStatement && ((PsiForeachStatement)parent).getIteratedValue() == reference) {
PsiElement parent = reference.getParent();
if (parent instanceof PsiExpressionList args && args.getParent() instanceof PsiCallExpression surroundingCall) {
if (surroundingCall instanceof PsiMethodCallExpression methodCall && processCollectionMethods(methodCall, reference)) {
return;
}
makeQueried();
if (!isQueryMethod(surroundingCall) && !COLLECTION_SAFE_ARGUMENT_METHODS.matches(surroundingCall)) {
makeUpdated();
}
return;
}
if (parent instanceof PsiMethodReferenceExpression methodReference) {
processQualifiedMethodReference(methodReference);
return;
}
if (parent instanceof PsiForeachStatement forEach && forEach.getIteratedValue() == reference) {
makeQueried();
return;
}
if (parent instanceof PsiAssignmentExpression && ((PsiAssignmentExpression)parent).getLExpression() == reference) {
PsiExpression rValue = ((PsiAssignmentExpression)parent).getRExpression();
if (parent instanceof PsiAssignmentExpression assignment && assignment.getLExpression() == reference) {
process(findEffectiveReference(assignment));
PsiExpression rValue = assignment.getRExpression();
if (rValue == null) return;
if (ExpressionUtils.nonStructuralChildren(rValue)
.allMatch(MismatchedCollectionQueryUpdateInspection::isEmptyCollectionInitializer)) {
@@ -255,8 +252,8 @@ public final class MismatchedCollectionQueryUpdateInspection extends BaseInspect
return;
}
}
if (parent instanceof PsiPolyadicExpression) {
IElementType tokenType = ((PsiPolyadicExpression)parent).getOperationTokenType();
if (parent instanceof PsiPolyadicExpression polyadic) {
IElementType tokenType = polyadic.getOperationTokenType();
if (tokenType.equals(JavaTokenType.PLUS)) {
// String concatenation
makeQueried();
@@ -266,7 +263,7 @@ public final class MismatchedCollectionQueryUpdateInspection extends BaseInspect
return;
}
}
if (parent instanceof PsiAssertStatement && ((PsiAssertStatement)parent).getAssertDescription() == reference) {
if (parent instanceof PsiAssertStatement assertStatement && assertStatement.getAssertDescription() == reference) {
makeQueried();
return;
}
@@ -479,14 +476,6 @@ public final class MismatchedCollectionQueryUpdateInspection extends BaseInspect
return immutable && !SideEffectChecker.mayHaveSideEffects(call);
}
private static PsiElement skipAssigmentExprUp(@Nullable PsiElement parent) {
parent = PsiUtil.skipParenthesizedExprUp(parent);
while (parent instanceof PsiAssignmentExpression) {
parent = PsiUtil.skipParenthesizedExprUp(parent.getParent());
}
return parent;
}
private static class QueryUpdateInfo {
boolean updated;
boolean queried;
@@ -0,0 +1,28 @@
/*
* Copyright 2000-2017 JetBrains s.r.o.
*
* 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.
*/
package com.siyeh.igtest.bugs.mismatched_collection_query_update;
import java.util.*;
class Test {
private final List<Object> test;
Test() {
(test = new ArrayList<>()).add(new Object());
}
public void printObjects() {
test.forEach(System.out::println);
}
}
@@ -133,7 +133,7 @@ class FooTest {
class Test2 {
public void bar() {
Stream.of("foo", "bar").collect(Collectors.toList()).add("baz");
Collections.checkedList(Stream.of("foo", "bar").collect(Collectors.toList()), String.class);
Collections.checkedList(Stream.of("foo", "bar").<warning descr="'collect(toList())' can be replaced with 'toList()'">collect(Collectors.toList())</warning>, String.class);
}
}
@@ -41,6 +41,10 @@ public class MismatchedCollectionQueryUpdateInspectionTest extends LightJavaInsp
public void testMismatchedCollectionQueryUpdate() {
doTest();
}
public void testUsedAssignmentResult() {
doTest();
}
@NotNull
@Override