From cc81eb834b584140922e157dfd4a27fb8dd94f0a Mon Sep 17 00:00:00 2001 From: Vincent Koeman Date: Tue, 6 Dec 2022 22:12:34 +0100 Subject: [PATCH] Extend the refaster rule for `new BigDecimal(...)` to doubles --- .../refasterrules/BigDecimalRules.java | 17 ++++++++++++++--- .../refasterrules/BigDecimalRulesTestInput.java | 8 ++++++-- .../BigDecimalRulesTestOutput.java | 8 ++++++-- 3 files changed, 26 insertions(+), 7 deletions(-) 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 b23406170bb..508a2cc8dc7 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 @@ -51,9 +51,7 @@ 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 { + static final class BigDecimalLongFactoryMethod { @BeforeTemplate BigDecimal before(long value) { return new BigDecimal(value); @@ -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); + } + } } 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 37db5459ce1..a1d72d27101 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,11 @@ 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 testBigDecimalLongFactoryMethod() { + return ImmutableSet.of(new BigDecimal(2), new BigDecimal(2L)); + } + + ImmutableSet testBigDecimalDoubleFactoryMethod() { + return ImmutableSet.of(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 8be65ad5201..76cb276e24f 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,11 @@ ImmutableSet testBigDecimalTen() { return ImmutableSet.of(BigDecimal.TEN, BigDecimal.TEN, BigDecimal.TEN); } - ImmutableSet testBigDecimalFactoryMethod() { - return ImmutableSet.of(BigDecimal.valueOf(0), BigDecimal.valueOf(0L)); + ImmutableSet testBigDecimalLongFactoryMethod() { + return ImmutableSet.of(BigDecimal.valueOf(2), BigDecimal.valueOf(2L)); + } + + ImmutableSet testBigDecimalDoubleFactoryMethod() { + return ImmutableSet.of(BigDecimal.valueOf(2.0)); } }