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 MapIsEmpty Refaster rule #339

Merged
merged 4 commits into from
Nov 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -41,6 +41,19 @@ static final class MapGetOrNull<K, V, T> {
}
}

/** Prefer {@link Map#isEmpty()} over more contrived alternatives. */
static final class MapIsEmpty<K, V> {
@BeforeTemplate
boolean before(Map<K, V> map) {
return Refaster.anyOf(map.keySet(), map.values(), map.entrySet()).isEmpty();
}

@AfterTemplate
boolean after(Map<K, V> map) {
return map.isEmpty();
}
}

/** Prefer {@link Map#size()} over more contrived alternatives. */
static final class MapSize<K, V> {
@BeforeTemplate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ String testMapGetOrNull() {
return ImmutableMap.of(1, "foo").getOrDefault("bar", null);
}

ImmutableSet<Boolean> testMapIsEmpty() {
return ImmutableSet.of(
ImmutableMap.of("foo", 1).keySet().isEmpty(),
ImmutableMap.of("bar", 2).values().isEmpty(),
ImmutableMap.of("baz", 3).entrySet().isEmpty());
}

ImmutableSet<Integer> testMapSize() {
return ImmutableSet.of(
ImmutableMap.of("foo", 1).keySet().size(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ String testMapGetOrNull() {
return ImmutableMap.of(1, "foo").get("bar");
}

ImmutableSet<Boolean> testMapIsEmpty() {
return ImmutableSet.of(
ImmutableMap.of("foo", 1).isEmpty(),
ImmutableMap.of("bar", 2).isEmpty(),
ImmutableMap.of("baz", 3).isEmpty());
}

ImmutableSet<Integer> testMapSize() {
return ImmutableSet.of(
ImmutableMap.of("foo", 1).size(),
Expand Down