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 BigDecimal.valueOf(double) over new BigDecimal(double) #394

Merged
merged 3 commits into from
Dec 7, 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 @@ -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));
Copy link
Contributor Author

@Venorcis Venorcis Dec 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

felt 2 was a nicer value to use here as there is a rule above to rewrite 0 to ZERO as well

also did not include 2F because there is no explicit BigDecimal(float) constructor, whilst there are separate ones for int, long and double; in this sense you could actually consider dropping the integer valueOfs in the existing tests above because they just map to the valueOf(long) anyway (there is no explicit int version).

}
}
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));
}
}