Skip to content

Commit

Permalink
Prefer BigDecimal.valueOf(double) over new BigDecimal(double) (#394)
Browse files Browse the repository at this point in the history
  • Loading branch information
Venorcis authored Dec 7, 2022
1 parent cf995ec commit bc1f204
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,18 @@ 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 {
/** Prefer {@link BigDecimal#valueOf(double)} over the associated constructor. */
// XXX: Ideally we also rewrite `new BigDecimal("<some-integer-value>")` in cases where the
// specified number can be represented as an `int` or `long`, but that requires a custom
// `BugChecker`.
static final class BigDecimalValueOf {
@BeforeTemplate
BigDecimal before(long value) {
BigDecimal before(double value) {
return new BigDecimal(value);
}

@AfterTemplate
BigDecimal after(long value) {
BigDecimal after(double value) {
return BigDecimal.valueOf(value);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ 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> testBigDecimalValueOf() {
return ImmutableSet.of(new BigDecimal(2), new BigDecimal(2L), new BigDecimal(2.0));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ 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> testBigDecimalValueOf() {
return ImmutableSet.of(BigDecimal.valueOf(2), BigDecimal.valueOf(2L), BigDecimal.valueOf(2.0));
}
}

0 comments on commit bc1f204

Please sign in to comment.