Skip to content

Commit

Permalink
Extend the refaster rule for new BigDecimal(...) to doubles
Browse files Browse the repository at this point in the history
  • Loading branch information
Venorcis committed Dec 6, 2022
1 parent ae327d8 commit cc81eb8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ BigDecimal after() {
}

/** Prefer {@link BigDecimal#valueOf(long)} over the associated constructor. */
// XXX: Ideally we'd also rewrite `BigDecimal.valueOf("<some-integer-value>")`, but it doesn't
// appear that's currently possible with Error Prone.
static final class BigDecimalFactoryMethod {
static final class BigDecimalLongFactoryMethod {
@BeforeTemplate
BigDecimal before(long value) {
return new BigDecimal(value);
Expand All @@ -64,4 +62,17 @@ BigDecimal after(long value) {
return BigDecimal.valueOf(value);
}
}

/** Prefer {@link BigDecimal#valueOf(double)} over the associated constructor. */
static final class BigDecimalDoubleFactoryMethod {
@BeforeTemplate
BigDecimal before(double value) {
return new BigDecimal(value);
}

@AfterTemplate
BigDecimal after(double value) {
return BigDecimal.valueOf(value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ ImmutableSet<BigDecimal> testBigDecimalTen() {
return ImmutableSet.of(BigDecimal.valueOf(10), BigDecimal.valueOf(10L), new BigDecimal("10"));
}

ImmutableSet<BigDecimal> testBigDecimalFactoryMethod() {
return ImmutableSet.of(new BigDecimal(0), new BigDecimal(0L));
ImmutableSet<BigDecimal> testBigDecimalLongFactoryMethod() {
return ImmutableSet.of(new BigDecimal(2), new BigDecimal(2L));
}

ImmutableSet<BigDecimal> testBigDecimalDoubleFactoryMethod() {
return ImmutableSet.of(new BigDecimal(2.0));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ ImmutableSet<BigDecimal> testBigDecimalTen() {
return ImmutableSet.of(BigDecimal.TEN, BigDecimal.TEN, BigDecimal.TEN);
}

ImmutableSet<BigDecimal> testBigDecimalFactoryMethod() {
return ImmutableSet.of(BigDecimal.valueOf(0), BigDecimal.valueOf(0L));
ImmutableSet<BigDecimal> testBigDecimalLongFactoryMethod() {
return ImmutableSet.of(BigDecimal.valueOf(2), BigDecimal.valueOf(2L));
}

ImmutableSet<BigDecimal> testBigDecimalDoubleFactoryMethod() {
return ImmutableSet.of(BigDecimal.valueOf(2.0));
}
}

0 comments on commit cc81eb8

Please sign in to comment.