Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce ArraysAsList Refaster rule #1275

Merged
merged 2 commits into from
Aug 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import java.util.Comparator;
import java.util.DoubleSummaryStatistics;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.LongSummaryStatistics;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -122,6 +123,21 @@ Stream<T> after(T[] array) {
}
}

/**
* Prefer {@link Arrays#asList(Object[])} over {@link Stream#toList()} as the former is clearer.
*/
static final class ArraysAsList<T> {
@BeforeTemplate
List<T> before(@NotMatches(IsRefasterAsVarargs.class) T[] array) {
return Arrays.stream(array).toList();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add an XXX to track that we're losing unmodifiability.

(We could also target ImmutableList#copyOf. For now I'll just make a note about this.)

}

@AfterTemplate
List<T> after(T[] array) {
return Arrays.asList(array);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This expression doesn't relate to streams, so it shouldn't be in this class.

}

/** Don't unnecessarily call {@link Streams#concat(Stream...)}. */
static final class ConcatOneStream<T> {
@BeforeTemplate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Streams;
import java.util.Arrays;
import java.util.DoubleSummaryStatistics;
import java.util.IntSummaryStatistics;
import java.util.List;
Expand Down Expand Up @@ -86,6 +87,10 @@ Stream<String> testStreamOfArray() {
return Stream.of(new String[] {"foo", "bar"});
}

List<String> testArraysAsList() {
return Arrays.stream(new String[] {"foo", "bar"}).toList();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Smaller 😄

Suggested change
return Arrays.stream(new String[] {"foo", "bar"}).toList();
return Arrays.stream(new String[0]).toList();

}

Stream<Integer> testConcatOneStream() {
return Streams.concat(Stream.of(1));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ Stream<String> testStreamOfArray() {
return Arrays.stream(new String[] {"foo", "bar"});
}

List<String> testArraysAsList() {
return Arrays.asList(new String[] {"foo", "bar"});
}

Stream<Integer> testConcatOneStream() {
return Stream.of(1);
}
Expand Down