diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/BigDecimalRules.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/BigDecimalRules.java index b23406170b..08e3878fa5 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/BigDecimalRules.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/BigDecimalRules.java @@ -50,17 +50,18 @@ BigDecimal after() { } } - /** Prefer {@link BigDecimal#valueOf(long)} over the associated constructor. */ - // XXX: Ideally we'd also rewrite `BigDecimal.valueOf("")`, 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("")` 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); } } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/BigDecimalRulesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/BigDecimalRulesTestInput.java index 37db5459ce..4f6a02bfe5 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/BigDecimalRulesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/BigDecimalRulesTestInput.java @@ -17,7 +17,7 @@ ImmutableSet testBigDecimalTen() { return ImmutableSet.of(BigDecimal.valueOf(10), BigDecimal.valueOf(10L), new BigDecimal("10")); } - ImmutableSet testBigDecimalFactoryMethod() { - return ImmutableSet.of(new BigDecimal(0), new BigDecimal(0L)); + ImmutableSet testBigDecimalValueOf() { + return ImmutableSet.of(new BigDecimal(2), new BigDecimal(2L), new BigDecimal(2.0)); } } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/BigDecimalRulesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/BigDecimalRulesTestOutput.java index 8be65ad520..37c226eb7a 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/BigDecimalRulesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/BigDecimalRulesTestOutput.java @@ -17,7 +17,7 @@ ImmutableSet testBigDecimalTen() { return ImmutableSet.of(BigDecimal.TEN, BigDecimal.TEN, BigDecimal.TEN); } - ImmutableSet testBigDecimalFactoryMethod() { - return ImmutableSet.of(BigDecimal.valueOf(0), BigDecimal.valueOf(0L)); + ImmutableSet testBigDecimalValueOf() { + return ImmutableSet.of(BigDecimal.valueOf(2), BigDecimal.valueOf(2L), BigDecimal.valueOf(2.0)); } }