From 1fbb882a160a7111b3fc71a38b8e71e16648e4c9 Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Wed, 31 Jan 2018 17:18:44 +0700 Subject: [PATCH] MismatchedCollectionQueryUpdate: tuning default method names, more tests --- ...edCollectionQueryUpdateInspectionBase.java | 8 +++--- .../MismatchedCollectionQueryUpdate.java | 27 +++++++++++++++++++ 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/bugs/MismatchedCollectionQueryUpdateInspectionBase.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/bugs/MismatchedCollectionQueryUpdateInspectionBase.java index f412ddf9047d..f4ca307586bf 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/bugs/MismatchedCollectionQueryUpdateInspectionBase.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/bugs/MismatchedCollectionQueryUpdateInspectionBase.java @@ -57,8 +57,8 @@ public class MismatchedCollectionQueryUpdateInspectionBase extends BaseInspectio private static final CallMatcher COLLECTION_SAFE_ARGUMENT_METHODS = CallMatcher.anyOf( - CallMatcher.instanceCall(CommonClassNames.JAVA_UTIL_COLLECTION, "addAll", "removeAll", "containsAll"), - CallMatcher.instanceCall(CommonClassNames.JAVA_UTIL_MAP, "putAll") + CallMatcher.instanceCall(CommonClassNames.JAVA_UTIL_COLLECTION, "addAll", "removeAll", "containsAll", "remove"), + CallMatcher.instanceCall(CommonClassNames.JAVA_UTIL_MAP, "putAll", "remove") ); private static final Set COLLECTIONS_QUERIES = @@ -72,8 +72,8 @@ public class MismatchedCollectionQueryUpdateInspectionBase extends BaseInspectio @SuppressWarnings("PublicField") public final ExternalizableStringSet queryNames = new ExternalizableStringSet( - "contains", "copyInto", "equals", "forEach", "get", "hashCode", "parallelStream", "propertyNames", "replaceAll", "save", - "size", "store", "stream", "toArray", "toString", "write"); + "contains", "copyInto", "equals", "forEach", "get", "hashCode", "iterator", "parallelStream", "propertyNames", + "replaceAll", "save", "size", "store", "stream", "toArray", "toString", "write"); @SuppressWarnings("PublicField") public final ExternalizableStringSet updateNames = new ExternalizableStringSet("add", "clear", "insert", "load", "merge", "offer", "poll", "pop", "push", "put", "remove", "replace", diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/mismatched_collection_query_update/MismatchedCollectionQueryUpdate.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/mismatched_collection_query_update/MismatchedCollectionQueryUpdate.java index 99a274bafd74..6a53a38afb04 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/mismatched_collection_query_update/MismatchedCollectionQueryUpdate.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/mismatched_collection_query_update/MismatchedCollectionQueryUpdate.java @@ -84,6 +84,13 @@ public class MismatchedCollectionQueryUpdate { return l.toArray(); } + Object[] testRemoveFromAnotherCollection(List> list) { + List l = new ArrayList<>(); + list.remove(l); + process(list); + return l.toArray(); + } + native void process(List> list); void testPureMethod() { @@ -147,6 +154,26 @@ public class MismatchedCollectionQueryUpdate { nonInitialized.add(key); } + void testListIterator() { + // IDEA-128168 + List test = new ArrayList(); + ListIterator i = test.listIterator(0); + i.add("hello!"); + System.out.println(i.next()); + } + + void testIterator() { + List test = new ArrayList(); + Iterator i = test.iterator(); + while(i.hasNext()) { + if(i.next() == null) { + // Normally iterator cannot add, remove only. If collection is always empty (not updated in any other way), + // this is useless anyways + i.remove(); + } + } + } + class Node{ private SortedSet mChildren;