[java-inspections] RedundantCollectionOperation: warn 'collection.addAll(List.of("x"))'

'collection.addAll(Set.of("x"))'
'collection.addAll(Collections.singletonList("x"))'

GitOrigin-RevId: 4a9185f774f9368fdcb5377eaeddb108b521f174
This commit is contained in:
Andrey.Cherkasov
2022-01-04 21:57:31 +00:00
committed by intellij-monorepo-bot
parent e4aa1604d4
commit cc1c1cc844
3 changed files with 9 additions and 5 deletions
@@ -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<Integer> list) {
list.add(42);
list.add(42);
list.add(42);
list.add(42);
}
}
@@ -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<Integer> list) {
list.add<caret>All(Collections.singleton(42));
list.addAll(Collections.singletonList(42));
list.addAll(List.of(42));
list.addAll(Set.of(42));
}
}
@@ -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<RedundantCollectionOperationHandler> HANDLERS =
new CallMapper<RedundantCollectionOperationHandler>()
@@ -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