From cc1c1cc844a79a2660d6814c24db9ef9103b502d Mon Sep 17 00:00:00 2001 From: "Andrey.Cherkasov" Date: Sun, 2 Jan 2022 16:20:44 +0400 Subject: [PATCH] [java-inspections] RedundantCollectionOperation: warn 'collection.addAll(List.of("x"))' 'collection.addAll(Set.of("x"))' 'collection.addAll(Collections.singletonList("x"))' GitOrigin-RevId: 4a9185f774f9368fdcb5377eaeddb108b521f174 --- .../afterAddAllCollectionsSingleton.java | 5 ++++- .../beforeAddAllCollectionsSingleton.java | 5 ++++- .../redundancy/RedundantCollectionOperationInspection.java | 4 +--- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/redundantCollectionOperation/afterAddAllCollectionsSingleton.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/redundantCollectionOperation/afterAddAllCollectionsSingleton.java index d04de6b381c2..871d0fb09b7b 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/redundantCollectionOperation/afterAddAllCollectionsSingleton.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/redundantCollectionOperation/afterAddAllCollectionsSingleton.java @@ -1,8 +1,11 @@ -// "Replace with 'add()'" "true" +// "Fix all 'Redundant 'Collection' operation' problems in file" "true" import java.util.*; class Test { void test(List list) { list.add(42); + list.add(42); + list.add(42); + list.add(42); } } \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/redundantCollectionOperation/beforeAddAllCollectionsSingleton.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/redundantCollectionOperation/beforeAddAllCollectionsSingleton.java index 65643dfb9d4a..3973c33db0ba 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/redundantCollectionOperation/beforeAddAllCollectionsSingleton.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/redundantCollectionOperation/beforeAddAllCollectionsSingleton.java @@ -1,8 +1,11 @@ -// "Replace with 'add()'" "true" +// "Fix all 'Redundant 'Collection' operation' problems in file" "true" import java.util.*; class Test { void test(List list) { list.addAll(Collections.singleton(42)); + list.addAll(Collections.singletonList(42)); + list.addAll(List.of(42)); + list.addAll(Set.of(42)); } } \ No newline at end of file diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/redundancy/RedundantCollectionOperationInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/redundancy/RedundantCollectionOperationInspection.java index ea37be26cea8..34bd561c0cfd 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/redundancy/RedundantCollectionOperationInspection.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/redundancy/RedundantCollectionOperationInspection.java @@ -75,8 +75,6 @@ public class RedundantCollectionOperationInspection extends AbstractBaseJavaLoca staticCall(CommonClassNames.JAVA_UTIL_COLLECTIONS, "singletonMap").parameterCount(2)); private static final CallMatcher COLLECTION_ADD_ALL = instanceCall(CommonClassNames.JAVA_UTIL_COLLECTION, "addAll").parameterTypes(CommonClassNames.JAVA_UTIL_COLLECTION); - private static final CallMatcher COLLECTIONS_SINGLETON = - staticCall(CommonClassNames.JAVA_UTIL_COLLECTIONS, "singleton").parameterCount(1); private static final CallMapper HANDLERS = new CallMapper() @@ -90,7 +88,7 @@ public class RedundantCollectionOperationInspection extends AbstractBaseJavaLoca .register(AS_LIST, RedundantSortAsListHandler::handler) .register(ITERABLE_ITERATOR, RedundantEmptyIteratorHandler::handler) .register(MAP_PUT_ALL, call -> ReplaceNestedCallHandler.handler(call, MAP_OF, "put")) - .register(COLLECTION_ADD_ALL, call -> ReplaceNestedCallHandler.handler(call, COLLECTIONS_SINGLETON, "add")); + .register(COLLECTION_ADD_ALL, call -> ReplaceNestedCallHandler.handler(call, SINGLETON, "add")); @NotNull @Override