diff --git a/java/java-impl/src/com/siyeh/ig/bugs/MismatchedCollectionQueryUpdateInspection.java b/java/java-impl/src/com/siyeh/ig/bugs/MismatchedCollectionQueryUpdateInspection.java index 8be4d384b9a5..71cd09676c93 100644 --- a/java/java-impl/src/com/siyeh/ig/bugs/MismatchedCollectionQueryUpdateInspection.java +++ b/java/java-impl/src/com/siyeh/ig/bugs/MismatchedCollectionQueryUpdateInspection.java @@ -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; diff --git a/java/java-tests/testData/ig/com/siyeh/igtest/bugs/mismatched_collection_query_update/UsedAssignmentResult.java b/java/java-tests/testData/ig/com/siyeh/igtest/bugs/mismatched_collection_query_update/UsedAssignmentResult.java new file mode 100644 index 000000000000..dd8771938055 --- /dev/null +++ b/java/java-tests/testData/ig/com/siyeh/igtest/bugs/mismatched_collection_query_update/UsedAssignmentResult.java @@ -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 test; + Test() { + (test = new ArrayList<>()).add(new Object()); + } + public void printObjects() { + test.forEach(System.out::println); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/inspection/simplifyStreamApiCallChains/CollectCollectorsToList.java b/java/java-tests/testData/inspection/simplifyStreamApiCallChains/CollectCollectorsToList.java index 399c942f2441..0d16ed734bcd 100644 --- a/java/java-tests/testData/inspection/simplifyStreamApiCallChains/CollectCollectorsToList.java +++ b/java/java-tests/testData/inspection/simplifyStreamApiCallChains/CollectCollectorsToList.java @@ -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").collect(Collectors.toList()), String.class); } } diff --git a/java/java-tests/testSrc/com/siyeh/ig/bugs/MismatchedCollectionQueryUpdateInspectionTest.java b/java/java-tests/testSrc/com/siyeh/ig/bugs/MismatchedCollectionQueryUpdateInspectionTest.java index b8d810e2792c..a71e7963315d 100644 --- a/java/java-tests/testSrc/com/siyeh/ig/bugs/MismatchedCollectionQueryUpdateInspectionTest.java +++ b/java/java-tests/testSrc/com/siyeh/ig/bugs/MismatchedCollectionQueryUpdateInspectionTest.java @@ -41,6 +41,10 @@ public class MismatchedCollectionQueryUpdateInspectionTest extends LightJavaInsp public void testMismatchedCollectionQueryUpdate() { doTest(); } + + public void testUsedAssignmentResult() { + doTest(); + } @NotNull @Override