Skip to content

Commit

Permalink
Introduce MapGetOrDefault Refaster rule (#439)
Browse files Browse the repository at this point in the history
Fixes #431.
  • Loading branch information
benhalasi authored Jan 6, 2023
1 parent 560f52b commit feb9abf
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package tech.picnic.errorprone.refasterrules;

import static java.util.Objects.requireNonNullElse;

import com.google.errorprone.refaster.Refaster;
import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
Expand Down Expand Up @@ -43,6 +45,21 @@ V after(Map<K, V> map, T key) {
}
}

/** Prefer {@link Map#getOrDefault(Object, Object)} over more contrived alternatives. */
// XXX: Note that `requireNonNullElse` throws an NPE if the second argument is `null`, while the
// alternative does not.
static final class MapGetOrDefault<K, V, T> {
@BeforeTemplate
V before(Map<K, V> map, T key, V defaultValue) {
return requireNonNullElse(map.get(key), defaultValue);
}

@AfterTemplate
V after(Map<K, V> map, T key, V defaultValue) {
return map.getOrDefault(key, defaultValue);
}
}

/** Prefer {@link Map#isEmpty()} over more contrived alternatives. */
static final class MapIsEmpty<K, V> {
@BeforeTemplate
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package tech.picnic.errorprone.refasterrules;

import static java.util.Objects.requireNonNullElse;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import java.math.RoundingMode;
Expand All @@ -11,7 +13,7 @@
final class MapRulesTest implements RefasterRuleCollectionTestCase {
@Override
public ImmutableSet<?> elidedTypesAndStaticImports() {
return ImmutableSet.of(HashMap.class);
return ImmutableSet.of(HashMap.class, requireNonNullElse(null, null));
}

Map<RoundingMode, String> testCreateEnumMap() {
Expand All @@ -22,6 +24,10 @@ String testMapGetOrNull() {
return ImmutableMap.of(1, "foo").getOrDefault("bar", null);
}

String testMapGetOrDefault() {
return requireNonNullElse(ImmutableMap.of(1, "foo").get("bar"), "baz");
}

ImmutableSet<Boolean> testMapIsEmpty() {
return ImmutableSet.of(
ImmutableMap.of("foo", 1).keySet().isEmpty(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package tech.picnic.errorprone.refasterrules;

import static java.util.Objects.requireNonNullElse;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import java.math.RoundingMode;
Expand All @@ -12,7 +14,7 @@
final class MapRulesTest implements RefasterRuleCollectionTestCase {
@Override
public ImmutableSet<?> elidedTypesAndStaticImports() {
return ImmutableSet.of(HashMap.class);
return ImmutableSet.of(HashMap.class, requireNonNullElse(null, null));
}

Map<RoundingMode, String> testCreateEnumMap() {
Expand All @@ -23,6 +25,10 @@ String testMapGetOrNull() {
return ImmutableMap.of(1, "foo").get("bar");
}

String testMapGetOrDefault() {
return ImmutableMap.of(1, "foo").getOrDefault("bar", "baz");
}

ImmutableSet<Boolean> testMapIsEmpty() {
return ImmutableSet.of(
ImmutableMap.of("foo", 1).isEmpty(),
Expand Down

0 comments on commit feb9abf

Please sign in to comment.