diff --git a/java/java-impl/src/inspectionDescriptions/RedundantStreamOptionalCall.html b/java/java-impl/src/inspectionDescriptions/RedundantStreamOptionalCall.html index 18240492f613..081aaf83ac0c 100644 --- a/java/java-impl/src/inspectionDescriptions/RedundantStreamOptionalCall.html +++ b/java/java-impl/src/inspectionDescriptions/RedundantStreamOptionalCall.html @@ -1,12 +1,12 @@ -Reports redundant Stream or Optional calls like map(x -> x) or filter(x -> true), -redundant sorted or distinct. +Reports redundant Stream or Optional calls like map(x -> x), filter(x -> true) or +redundant sorted() or distinct() calls.

Note that a mapping operation in code like streamOfIntegers.map(Integer::valueOf) - works as requireNonNull check: + works as requireNonNull() check: if the stream contains null, it throws a NullPointerException, thus it's not absolutely redundant. - Disable the Report redundant boxing in Stream.map option if you do not want such cases to be reported.

+ Disable the Report redundant boxing in Stream.map() option if you do not want such cases to be reported.

This inspection only reports if the language level of the project or module is 8 or higher.

\ No newline at end of file diff --git a/java/java-tests/testData/inspection/redundantStreamOptionalCall/RedundantStreamOptionalCall.java b/java/java-tests/testData/inspection/redundantStreamOptionalCall/RedundantStreamOptionalCall.java index 4f4ad376a948..5e00d7040d12 100644 --- a/java/java-tests/testData/inspection/redundantStreamOptionalCall/RedundantStreamOptionalCall.java +++ b/java/java-tests/testData/inspection/redundantStreamOptionalCall/RedundantStreamOptionalCall.java @@ -4,79 +4,79 @@ import java.util.stream.*; public class RedundantStreamOptionalCall { public void test() { - List list = Stream.of(1, 2, 3).sorted(Comparator.reverseOrder()) + List list = Stream.of(1, 2, 3).sorted(Comparator.reverseOrder()) .filter(x -> x > 0).sorted().collect(Collectors.toList()); - if(Stream.of(1, 2, 3).sorted().filter(x -> x > 0).allMatch(x -> x < 10)) { + if(Stream.of(1, 2, 3).sorted().filter(x -> x > 0).allMatch(x -> x < 10)) { return; } - if(Stream.of("foo", "bar", "baz").sorted(String.CASE_INSENSITIVE_ORDER).count() > 0) { + if(Stream.of("foo", "bar", "baz").sorted(String.CASE_INSENSITIVE_ORDER).count() > 0) { return; } - long first = Stream.of(1, 2, 3).distinct().sorted().skip(1).limit(2).distinct() + long first = Stream.of(1, 2, 3).distinct().sorted().skip(1).limit(2).distinct() .findFirst().orElse(0); Object[] objects = Stream.of(1, 2, 3).distinct().map(x -> x*2).distinct().toArray(); - Object[] objects2 = Stream.of(1, 2, 3).distinct().filter(x -> true). - distinct().toArray(); - Object xyz = Optional.of(123).map(integer -> Integer.valueOf(integer)).orElse(null); - Object xyz2 = Optional.of(123).map(Integer::valueOf).orElse(null); - Object xyz3 = Optional.of(123).map(Integer::intValue).orElse(null); + Object[] objects2 = Stream.of(1, 2, 3).distinct().filter(x -> true). + distinct().toArray(); + Object xyz = Optional.of(123).map(integer -> Integer.valueOf(integer)).orElse(null); + Object xyz2 = Optional.of(123).map(Integer::valueOf).orElse(null); + Object xyz3 = Optional.of(123).map(Integer::intValue).orElse(null); Object xyz4 = Optional.of(123).map(Integer::longValue).orElse(null); - Object xyz5 = IntStream.of(123).map(i -> Integer.valueOf(i)).count(); - Object xyz6 = Stream.of(123).map(i -> Integer.valueOf(i)).count(); - Optional.of("xyz").flatMap(Optional::ofNullable).ifPresent(System.out::println); - Optional.of("xyz").flatMap(Optional::of).ifPresent(System.out::println); + Object xyz5 = IntStream.of(123).map(i -> Integer.valueOf(i)).count(); + Object xyz6 = Stream.of(123).map(i -> Integer.valueOf(i)).count(); + Optional.of("xyz").flatMap(Optional::ofNullable).ifPresent(System.out::println); + Optional.of("xyz").flatMap(Optional::of).ifPresent(System.out::println); - Optional.of(123).filter(x -> true).ifPresent(System.out::println); + Optional.of(123).filter(x -> true).ifPresent(System.out::println); double avg = IntStream.range(0, 100).distinct() - .asLongStream().distinct().average().orElse(0); + .asLongStream().distinct().average().orElse(0); - LongStream.range(0, 100).parallel() + LongStream.range(0, 100).parallel() .boxed().map(x -> x*2).sorted().sequential().forEach(System.out::println); - Stream.of(0, 100).map(x -> x*2).parallel() + Stream.of(0, 100).map(x -> x*2).parallel() .filter(x -> x > 0).sorted().parallel().forEach(System.out::println); - IntStream.of(0, 100).map(x -> x*2).sequential() + IntStream.of(0, 100).map(x -> x*2).sequential() .filter(x -> x > 0).distinct().sequential().forEach(System.out::println); - Stream.of(0, 100).map(x -> x*2).sequential() + Stream.of(0, 100).map(x -> x*2).sequential() .filter(x -> x > 0).limit(10).parallel().forEach(System.out::println); - Stream.of("xyz").parallel().sorted().collect(Collectors.toList()).stream().sequential().forEach(System.out::println); + Stream.of("xyz").parallel().sorted().collect(Collectors.toList()).stream().sequential().forEach(System.out::println); - IntStream.range(0, 100).unordered().filter(x -> x > 50).unordered().forEach(System.out::println); + IntStream.range(0, 100).unordered().filter(x -> x > 50).unordered().forEach(System.out::println); IntStream.range(0, 100).unordered().filter(x -> x > 50).sorted().unordered().forEach(System.out::println); Collection collection = Arrays.asList("foo", "foo", "bar"); - Set set1 = collection.stream().distinct().collect(Collectors.toSet()); - Set set2 = collection.stream().sorted().collect(Collectors.toSet()); - Set set3 = collection.stream().sorted().distinct().collect(Collectors.toSet()); - Set set4 = collection.stream().distinct().sorted().collect(Collectors.toSet()); + Set set1 = collection.stream().distinct().collect(Collectors.toSet()); + Set set2 = collection.stream().sorted().collect(Collectors.toSet()); + Set set3 = collection.stream().sorted().distinct().collect(Collectors.toSet()); + Set set4 = collection.stream().distinct().sorted().collect(Collectors.toSet()); List list1 = collection.stream().distinct().collect(Collectors.toList()); List list2 = collection.stream().sorted().collect(Collectors.toList()); - Map map1 = collection.stream().sorted().collect(Collectors.toMap(Integer::valueOf, x -> x)); + Map map1 = collection.stream().sorted().collect(Collectors.toMap(Integer::valueOf, x -> x)); Map map2 = collection.stream().sorted().collect(Collectors.toMap(Integer::valueOf, x -> x, (a,b) ->a, LinkedHashMap::new)); - Set set5 = collection.stream().sorted().collect(Collectors.toCollection(HashSet::new)); - Set set6 = collection.stream().sorted().collect(Collectors.toCollection(() -> new HashSet<>())); + Set set5 = collection.stream().sorted().collect(Collectors.toCollection(HashSet::new)); + Set set6 = collection.stream().sorted().collect(Collectors.toCollection(() -> new HashSet<>())); Set set6a = collection.stream().sorted().collect(Collectors.toCollection(() -> new LinkedHashSet<>())); - Set set7 = collection.stream().distinct().collect(Collectors.toCollection(HashSet::new)); - Set set8 = collection.stream().distinct().collect(Collectors.toCollection(() -> new HashSet<>())); - Set set8a = collection.stream().distinct().collect(Collectors.toCollection(() -> new LinkedHashSet<>())); + Set set7 = collection.stream().distinct().collect(Collectors.toCollection(HashSet::new)); + Set set8 = collection.stream().distinct().collect(Collectors.toCollection(() -> new HashSet<>())); + Set set8a = collection.stream().distinct().collect(Collectors.toCollection(() -> new LinkedHashSet<>())); - IntStream.of(123).mapToObj(String::valueOf).sorted(); - LongStream.of(123).filter(x -> x > 0).mapToObj(String::valueOf).distinct(); + IntStream.of(123).mapToObj(String::valueOf).sorted(); + LongStream.of(123).filter(x -> x > 0).mapToObj(String::valueOf).distinct(); LongStream.of(123).filter(x -> x > 0).mapToObj(String::valueOf).flatMap(x -> Stream.of(x, x+x)).distinct(); Stream.of("foo").flatMap(x -> Stream.of(x, x)).parallel(); - Stream.of("foo").flatMap(x -> Stream.of(x, x)).parallel().forEach(System.out::println); + Stream.of("foo").flatMap(x -> Stream.of(x, x)).parallel().forEach(System.out::println); - Stream.of("foo", "bar", "baz").sorted().sorted(Comparator.naturalOrder().reversed()); + Stream.of("foo", "bar", "baz").sorted().sorted(Comparator.naturalOrder().reversed()); Stream.of("foo", "bar", "baz").sorted().sorted(Comparator.comparing(x -> x.charAt(0) == 'b')); - Stream.of("foo", "bar", "baz").sorted().sorted(Comparator.comparing(x -> x.charAt(0) == 'b')).sorted(Comparator.reverseOrder()); + Stream.of("foo", "bar", "baz").sorted().sorted(Comparator.comparing(x -> x.charAt(0) == 'b')).sorted(Comparator.reverseOrder()); - Stream.of("foo", "bar", "baz").sorted(String.CASE_INSENSITIVE_ORDER).max(String.CASE_INSENSITIVE_ORDER.reversed()); - Stream.of("foo", "bar", "baz").sorted(String.CASE_INSENSITIVE_ORDER).min(String.CASE_INSENSITIVE_ORDER); + Stream.of("foo", "bar", "baz").sorted(String.CASE_INSENSITIVE_ORDER).max(String.CASE_INSENSITIVE_ORDER.reversed()); + Stream.of("foo", "bar", "baz").sorted(String.CASE_INSENSITIVE_ORDER).min(String.CASE_INSENSITIVE_ORDER); Stream.of("foo", "bar", "baz").sorted(String.CASE_INSENSITIVE_ORDER).min(Comparator.naturalOrder()); } public static void flatMapStreamOf(String[] arr1, String[] arr2) { - Stream.of(1,2,3,4,5).flatMap(Stream::of).toArray(); - Stream.of(1,2,3,4,5).flatMap(t -> Stream.of(t)).toArray(); + Stream.of(1,2,3,4,5).flatMap(Stream::of).toArray(); + Stream.of(1,2,3,4,5).flatMap(t -> Stream.of(t)).toArray(); Stream.of(arr1, arr2).flatMap(Stream::of).toArray(); } @@ -84,10 +84,10 @@ public class RedundantStreamOptionalCall { return objectStreams.>map(Stream::distinct).flatMap(Function.identity()); } public static Stream fun2(Stream> objectStreams) { - return objectStreams.map(Stream::distinct).flatMap(Function.identity()); + return objectStreams.map(Stream::distinct).flatMap(Function.identity()); } public static Stream fun3(Stream> objectStreams) { - return objectStreams.map(Function.identity()).flatMap(Function.identity()); + return objectStreams.map(Function.identity()).flatMap(Function.identity()); } private static class SomeClazz { } diff --git a/java/java-tests/testData/inspection/redundantStreamOptionalCall/afterMap.java b/java/java-tests/testData/inspection/redundantStreamOptionalCall/afterMap.java index cba2ef929d83..52dd080dd76c 100644 --- a/java/java-tests/testData/inspection/redundantStreamOptionalCall/afterMap.java +++ b/java/java-tests/testData/inspection/redundantStreamOptionalCall/afterMap.java @@ -1,4 +1,4 @@ -// "Remove 'map' call" "true" +// "Remove 'map()' call" "true" import java.util.stream.Stream; public class Test { diff --git a/java/java-tests/testData/inspection/redundantStreamOptionalCall/afterMapFlatMap.java b/java/java-tests/testData/inspection/redundantStreamOptionalCall/afterMapFlatMap.java index fe3c589bef22..38b71a825bdd 100644 --- a/java/java-tests/testData/inspection/redundantStreamOptionalCall/afterMapFlatMap.java +++ b/java/java-tests/testData/inspection/redundantStreamOptionalCall/afterMapFlatMap.java @@ -1,4 +1,4 @@ -// "Remove 'flatMap' call" "true" +// "Remove 'flatMap()' call" "true" import java.util.function.Function; import java.util.stream.Stream; diff --git a/java/java-tests/testData/inspection/redundantStreamOptionalCall/beforeMap.java b/java/java-tests/testData/inspection/redundantStreamOptionalCall/beforeMap.java index 10d12cabbaa1..2c31a3db75d4 100644 --- a/java/java-tests/testData/inspection/redundantStreamOptionalCall/beforeMap.java +++ b/java/java-tests/testData/inspection/redundantStreamOptionalCall/beforeMap.java @@ -1,4 +1,4 @@ -// "Remove 'map' call" "true" +// "Remove 'map()' call" "true" import java.util.stream.Stream; public class Test { diff --git a/java/java-tests/testData/inspection/redundantStreamOptionalCall/beforeMapFlatMap.java b/java/java-tests/testData/inspection/redundantStreamOptionalCall/beforeMapFlatMap.java index c7a28a676cf1..60a328eaf767 100644 --- a/java/java-tests/testData/inspection/redundantStreamOptionalCall/beforeMapFlatMap.java +++ b/java/java-tests/testData/inspection/redundantStreamOptionalCall/beforeMapFlatMap.java @@ -1,4 +1,4 @@ -// "Remove 'flatMap' call" "true" +// "Remove 'flatMap()' call" "true" import java.util.function.Function; import java.util.stream.Stream; diff --git a/java/openapi/resources/messages/JavaBundle.properties b/java/openapi/resources/messages/JavaBundle.properties index 5cd690671b20..e66c62ce011e 100644 --- a/java/openapi/resources/messages/JavaBundle.properties +++ b/java/openapi/resources/messages/JavaBundle.properties @@ -596,21 +596,21 @@ inspection.redundant.record.constructor.description=Redundant record constructor inspection.redundant.record.constructor.fix.family.name=Convert canonical constructor to compact form inspection.redundant.record.constructor.statement.message=Redundant field assignment in compact constructor inspection.redundant.stream.optional.call.explanation.at.most.one=stream contains at most one element -inspection.redundant.stream.optional.call.explanation.distinct=there already was a 'distinct' call in the chain -inspection.redundant.stream.optional.call.explanation.distinct.set=elements will be distinct anyways when collected to the Set -inspection.redundant.stream.optional.call.explanation.filter=predicate is always true -inspection.redundant.stream.optional.call.explanation.map.flatMap=previous 'map' call can replace the 'flatMap' step -inspection.redundant.stream.optional.call.explanation.parallel=there''s subsequent ''{0}'' call which overrides this call +inspection.redundant.stream.optional.call.explanation.distinct=there is a previous 'distinct()' call in the chain +inspection.redundant.stream.optional.call.explanation.distinct.set=elements will be distinct anyway when collected to a Set +inspection.redundant.stream.optional.call.explanation.filter=predicate is always 'true' +inspection.redundant.stream.optional.call.explanation.map.flatMap=previous 'map()' call can replace the 'flatMap()' step +inspection.redundant.stream.optional.call.explanation.parallel=there is a subsequent ''{0}()'' call which overrides this call inspection.redundant.stream.optional.call.explanation.parallel.single=stream created from single element will not be parallelized -inspection.redundant.stream.optional.call.explanation.sorted=subsequent ''{0}'' call doesn''t depend on the sort order -inspection.redundant.stream.optional.call.explanation.sorted.twice=stream content is sorted again after that -inspection.redundant.stream.optional.call.explanation.unordered=there already was an 'unordered' call in the chain +inspection.redundant.stream.optional.call.explanation.sorted=subsequent ''{0}()'' call doesn''t depend on the sort order +inspection.redundant.stream.optional.call.explanation.sorted.twice=there is a subsequent 'sorted()' call in the chain +inspection.redundant.stream.optional.call.explanation.unordered=there is a previous 'unordered()' call in the chain inspection.redundant.stream.optional.call.fix.collect.to.ordered.family.name=Collect to 'LinkedHashSet' inspection.redundant.stream.optional.call.fix.family.name=Remove redundant chain call -inspection.redundant.stream.optional.call.fix.name=Remove ''{0}'' call -inspection.redundant.stream.optional.call.message=Redundant ''{0}'' call -inspection.redundant.stream.optional.call.message.with.explanation=Redundant ''{0}'' call: {1} -inspection.redundant.stream.optional.call.option.streamboxing=Report redundant boxing in Stream.map +inspection.redundant.stream.optional.call.fix.name=Remove ''{0}()'' call +inspection.redundant.stream.optional.call.message=Redundant ''{0}()'' call +inspection.redundant.stream.optional.call.message.with.explanation=Redundant ''{0}()'' call: {1} +inspection.redundant.stream.optional.call.option.streamboxing=Report redundant boxing in Stream.map() inspection.reflect.handle.invocation.argument.not.array=Argument is not an array type inspection.reflect.handle.invocation.argument.not.exact=Argument type should be exactly ''{0}'' inspection.reflect.handle.invocation.primitive.argument.null=Argument of type ''{0}'' cannot be ''null'' @@ -1313,7 +1313,7 @@ inspection.read.write.string.can.be.used.display.name='Files.readString()' or 'F inspection.java.9.collection.factory.display.name=Immutable collection creation can be replaced with collection factory call inspection.explicit.argument.can.be.lambda.display.name=Explicit argument can be lambda inspection.excessive.lambda.usage.display.name=Excessive lambda usage -inspection.redundant.stream.optional.call.display.name=Redundant step in Stream or Optional call chain +inspection.redundant.stream.optional.call.display.name=Redundant step in 'Stream' or 'Optional' call chain inspection.obvious.null.check.display.name=Null-check method is called with obviously non-null argument inspection.simplify.stream.api.call.chains.display.name=Stream API call chain can be simplified inspection.simplify.optional.call.chains.display.name=Optional call chain can be simplified