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

Prefer String#valueOf over Objects#toString #192

Merged
merged 1 commit into from
Aug 22, 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,13 +41,11 @@ boolean after(T a, T b) {
// non-null.
static final class EqualsPredicate<T> {
@BeforeTemplate
@SuppressWarnings("NoFunctionalReturnType")
Predicate<T> before(T v) {
return e -> v.equals(e);
}

@AfterTemplate
@SuppressWarnings("NoFunctionalReturnType")
Predicate<T> after(T v) {
return v::equals;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,11 @@ T after(T first, T second) {
/** Prefer {@link Objects#isNull(Object)} over the equivalent lambda function. */
static final class IsNullFunction<T> {
@BeforeTemplate
@SuppressWarnings("NoFunctionalReturnType")
Predicate<T> before() {
return o -> o == null;
}

@AfterTemplate
@SuppressWarnings("NoFunctionalReturnType")
Predicate<T> after() {
return Objects::isNull;
}
Expand All @@ -48,13 +46,11 @@ Predicate<T> after() {
/** Prefer {@link Objects#nonNull(Object)} over the equivalent lambda function. */
static final class NonNullFunction<T> {
@BeforeTemplate
@SuppressWarnings("NoFunctionalReturnType")
Predicate<T> before() {
return o -> o != null;
}

@AfterTemplate
@SuppressWarnings("NoFunctionalReturnType")
Predicate<T> after() {
return Objects::nonNull;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,11 @@ T after(Optional<T> optional) {
// generalization. If/when Refaster is extended to understand this, delete the template above.
static final class OptionalOrElseThrowMethodReference<T> {
@BeforeTemplate
@SuppressWarnings("NoFunctionalReturnType")
Function<Optional<T>, T> before() {
return Optional::get;
}

@AfterTemplate
@SuppressWarnings("NoFunctionalReturnType")
Function<Optional<T>, T> after() {
return Optional::orElseThrow;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
import com.google.errorprone.refaster.annotation.BeforeTemplate;
import java.util.Arrays;
import java.util.Collection;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import javax.annotation.Nullable;

/** Refaster templates related to expressions dealing with {@link String}s. */
Expand Down Expand Up @@ -106,6 +108,40 @@ String after(CharSequence delimiter, Iterable<? extends CharSequence> elements)
}
}

/**
* Prefer direct invocation of {@link String#valueOf(Object)} over the indirection introduced by
* {@link Objects#toString(Object)}.
*/
static final class StringValueOf {
@BeforeTemplate
String before(Object object) {
return Objects.toString(object);
}

@AfterTemplate
String after(Object object) {
return String.valueOf(object);
}
}

/**
* Prefer direct delegation to {@link String#valueOf(Object)} over the indirection introduced by
* {@link Objects#toString(Object)}.
*/
// XXX: This template is analogous to `StringValueOf` above. Arguably this is its generalization.
// If/when Refaster is extended to understand this, delete the template above.
static final class StringValueOfMethodReference<T> {
@BeforeTemplate
Function<Object, String> before() {
return Objects::toString;
}

@AfterTemplate
Function<Object, String> after() {
return String::valueOf;
}
}

/** Don't unnecessarily use the two-argument {@link String#substring(int, int)}. */
static final class SubstringRemainder {
@BeforeTemplate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@
import com.google.common.collect.Iterables;
import com.google.common.collect.Streams;
import java.util.Arrays;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Stream;
import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase;

final class StringTemplatesTest implements RefasterTemplateTestCase {
@Override
public ImmutableSet<?> elidedTypesAndStaticImports() {
return ImmutableSet.of(
Arrays.class, Joiner.class, Stream.class, Streams.class, joining(), UTF_8);
Arrays.class, Joiner.class, Objects.class, Stream.class, Streams.class, joining(), UTF_8);
}

ImmutableSet<Boolean> testStringIsEmpty() {
Expand Down Expand Up @@ -59,6 +61,14 @@ ImmutableSet<String> testJoinStrings() {
ImmutableList.of("foo", "bar").stream().collect(joining("f")));
}

String testStringValueOf() {
return Objects.toString("foo");
}

Function<Object, String> testStringValueOfMethodReference() {
return Objects::toString;
}

String testSubstringRemainder() {
return "foo".substring(1, "foo".length());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@
import com.google.common.collect.Iterables;
import com.google.common.collect.Streams;
import java.util.Arrays;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Stream;
import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase;

final class StringTemplatesTest implements RefasterTemplateTestCase {
@Override
public ImmutableSet<?> elidedTypesAndStaticImports() {
return ImmutableSet.of(
Arrays.class, Joiner.class, Stream.class, Streams.class, joining(), UTF_8);
Arrays.class, Joiner.class, Objects.class, Stream.class, Streams.class, joining(), UTF_8);
}

ImmutableSet<Boolean> testStringIsEmpty() {
Expand Down Expand Up @@ -59,6 +61,14 @@ ImmutableSet<String> testJoinStrings() {
String.join("f", ImmutableList.of("foo", "bar")));
}

String testStringValueOf() {
return String.valueOf("foo");
}

Function<Object, String> testStringValueOfMethodReference() {
return String::valueOf;
}

String testSubstringRemainder() {
return "foo".substring(1);
}
Expand Down