diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/MapRules.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/MapRules.java index cb4fd9bf76..2d56fbbc76 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/MapRules.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/MapRules.java @@ -41,6 +41,19 @@ static final class MapGetOrNull { } } + /** Prefer {@link Map#isEmpty()} over more contrived alternatives. */ + static final class MapIsEmpty { + @BeforeTemplate + boolean before(Map map) { + return Refaster.anyOf(map.keySet(), map.values(), map.entrySet()).isEmpty(); + } + + @AfterTemplate + boolean after(Map map) { + return map.isEmpty(); + } + } + /** Prefer {@link Map#size()} over more contrived alternatives. */ static final class MapSize { @BeforeTemplate diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/MapRulesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/MapRulesTestInput.java index 1f303a4f84..a8dc6e7fa4 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/MapRulesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/MapRulesTestInput.java @@ -22,6 +22,13 @@ String testMapGetOrNull() { return ImmutableMap.of(1, "foo").getOrDefault("bar", null); } + ImmutableSet testMapIsEmpty() { + return ImmutableSet.of( + ImmutableMap.of("foo", 1).keySet().isEmpty(), + ImmutableMap.of("bar", 2).values().isEmpty(), + ImmutableMap.of("baz", 3).entrySet().isEmpty()); + } + ImmutableSet testMapSize() { return ImmutableSet.of( ImmutableMap.of("foo", 1).keySet().size(), diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/MapRulesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/MapRulesTestOutput.java index 35f183ee18..650cd4094f 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/MapRulesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/MapRulesTestOutput.java @@ -23,6 +23,13 @@ String testMapGetOrNull() { return ImmutableMap.of(1, "foo").get("bar"); } + ImmutableSet testMapIsEmpty() { + return ImmutableSet.of( + ImmutableMap.of("foo", 1).isEmpty(), + ImmutableMap.of("bar", 2).isEmpty(), + ImmutableMap.of("baz", 3).isEmpty()); + } + ImmutableSet testMapSize() { return ImmutableSet.of( ImmutableMap.of("foo", 1).size(),