[java-inspections] Uncomment old 'toList() can be used' tests; fix false-positives

Some false-negatives still exist but they are rare cases, so let's skip them for now

GitOrigin-RevId: 7c3af14c332768267123320f1601dd1fd76ff240
This commit is contained in:
Tagir Valeev
2024-10-21 17:40:32 +00:00
committed by intellij-monorepo-bot
parent dd15592444
commit bf9c220dfc
2 changed files with 60 additions and 59 deletions
@@ -659,6 +659,8 @@ public final class MismatchedCollectionQueryUpdateInspection extends BaseInspect
PsiElement parent = effectiveReference.getParent();
Set<PsiReferenceExpression> ignored = new HashSet<>();
if (parent instanceof PsiAssignmentExpression assignmentExpression) {
// Do not process when the result of assignment is used: rare case
if (!ExpressionUtils.isVoidContext(assignmentExpression)) return false;
if(assignmentExpression.getLExpression() instanceof PsiReferenceExpression referenceExpression){
ignored.add(referenceExpression);
parent = referenceExpression.resolve();
@@ -185,64 +185,63 @@ class MissingIteratorRemoveAnalysis {
System.out.println(string);
}
}
}
//class TODO() {
//
// void testMustHave1(Stream<String> stream) {
// List<String> list1;
// list1 = stream.collect(Collectors.toList());
// list1.forEach(System.out::println);
// }
//
// void testMustHave2(Stream<String> stream) {
// List<String> list1;
// list1 = stream.collect(Collectors.toList());
// list1.add("foo");
// }
//
// void test1(Stream<String> stream) {
// List<String> list1;
// List<String> list2;
// list1 = ((list2 = ((stream.collect(Collectors.toList())))));
// list1.forEach(System.out::println);
// list2.forEach(System.out::println);
// }
//
// void test2(Stream<String> stream) {
// List<String> list1;
// List<String> list2;
// list1 = ((list2 = ((stream.collect(Collectors.toList())))));
// list1.add("foo");
// }
//
// void test3(Stream<String> stream) {
// List<String> list1;
// List<String> list2;
// list1 = ((list2 = ((stream.collect(Collectors.toList())))));
// list2.add("foo");
// }
//
// void test4(Stream<String> stream) {
// List<String> list1;
// list1 = stream.collect(Collectors.toList());
// list1.add("foo");
// }
//
// void test5(Stream<String> stream) {
// List<String> subList = stream.collect(Collectors.toList()).subList(0, 3);
// subList.add("foo");
// }
//
// void test6(Stream<String> stream) {
// List<String> subList = stream.collect(Collectors.toList()).subList(0, 3);
// subList.forEach(System.out::println);
// }
void testMustHave1(Stream<String> stream) {
List<String> list1;
list1 = stream.<warning descr="'collect(toList())' can be replaced with 'toList()'">collect(Collectors.toList())</warning>;
list1.forEach(System.out::println);
}
// void test7(Stream<String> stream) {
// List<String> list = stream.collect(Collectors.toList());
// list.forEach(System.out::println);
// list = new ArrayList<>();
// list.add("foo");
// }
//}
void testMustHave2(Stream<String> stream) {
List<String> list1;
list1 = stream.collect(Collectors.toList());
list1.add("foo");
}
void test1(Stream<String> stream) {
List<String> list1;
List<String> list2;
// Theoretically could be supported but for now we ignore cases when assignment result is used
list1 = ((list2 = ((stream.collect(Collectors.toList())))));
list1.forEach(System.out::println);
list2.forEach(System.out::println);
}
void test2(Stream<String> stream) {
List<String> w1;
List<String> w2;
w1 = ((w2 = ((stream.collect(Collectors.toList())))));
w1.add("foo");
}
void test3(Stream<String> stream) {
List<String> w1;
List<String> w2;
w1 = ((w2 = ((stream.collect(Collectors.toList())))));
w2.add("foo");
}
void test4(Stream<String> stream) {
List<String> list1;
list1 = stream.collect(Collectors.toList());
list1.add("foo");
}
void test5(Stream<String> stream) {
List<String> subList = stream.collect(Collectors.toList()).subList(0, 3);
subList.add("foo");
}
void test6(Stream<String> stream) {
List<String> subList = stream.<warning descr="'collect(toList())' can be replaced with 'toList()'">collect(Collectors.toList())</warning>.subList(0, 3);
subList.forEach(System.out::println);
}
// TODO
void test7(Stream<String> stream) {
List<String> list = stream.collect(Collectors.toList());
list.forEach(System.out::println);
list = new ArrayList<>();
list.add("foo");
}
}