diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/PrimitiveRules.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/PrimitiveRules.java index f0d39d03202..8a6e410b8e9 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/PrimitiveRules.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/PrimitiveRules.java @@ -8,7 +8,9 @@ import com.google.common.primitives.Ints; import com.google.common.primitives.Longs; import com.google.common.primitives.Shorts; +import com.google.errorprone.refaster.Refaster; import com.google.errorprone.refaster.annotation.AfterTemplate; +import com.google.errorprone.refaster.annotation.AlsoNegation; import com.google.errorprone.refaster.annotation.BeforeTemplate; import tech.picnic.errorprone.refaster.annotation.OnlineDocumentation; @@ -384,4 +386,98 @@ boolean after(double d) { return Double.isFinite(d); } } + + /** Prefer an {@link Integer#signum(int)} comparison to 1 over less clear alternatives. */ + static final class IntegerSignumIsPositive { + @BeforeTemplate + boolean before(int i) { + return Refaster.anyOf(Integer.signum(i) > 0, Integer.signum(i) >= 1); + } + + @AfterTemplate + @AlsoNegation + boolean after(int i) { + return Integer.signum(i) == 1; + } + } + + /** Prefer an {@link Integer#signum(int)} comparison to -1 over less clear alternatives. */ + static final class IntegerSignumIsNegative { + @BeforeTemplate + boolean before(int i) { + return Refaster.anyOf(Integer.signum(i) < 0, Integer.signum(i) <= -1); + } + + @AfterTemplate + @AlsoNegation + boolean after(int i) { + return Integer.signum(i) == -1; + } + } + + /** Prefer an {@link Long#signum(long)} comparison to 1 over less clear alternatives. */ + static final class LongSignumIsPositive { + @BeforeTemplate + boolean before(long l) { + return Refaster.anyOf(Long.signum(l) > 0, Long.signum(l) >= 1); + } + + @AfterTemplate + @AlsoNegation + boolean after(long l) { + return Long.signum(l) == 1; + } + } + + /** Prefer an {@link Long#signum(long)} comparison to -1 over less clear alternatives. */ + static final class LongSignumIsNegative { + @BeforeTemplate + boolean before(long l) { + return Refaster.anyOf(Long.signum(l) < 0, Long.signum(l) <= -1); + } + + @AfterTemplate + @AlsoNegation + boolean after(long l) { + return Long.signum(l) == -1; + } + } + + /** Prefer a {@link Math#signum(double)} comparison to 1 over less clear alternatives. */ + static final class MathSignumIsPositive { + @BeforeTemplate + boolean before(float v) { + return Refaster.anyOf(Math.signum(v) > 0, Math.signum(v) >= 1); + } + + @BeforeTemplate + boolean before(double v) { + return Refaster.anyOf(Math.signum(v) > 0, Math.signum(v) >= 1); + } + + @AfterTemplate + @AlsoNegation + boolean after(double v) { + return Math.signum(v) == 1; + } + } + + /** Prefer a {@link Math#signum(double)} comparison to -1 over less clear alternatives. */ + static final class MathSignumIsNegative { + @BeforeTemplate + boolean before(float v) { + return Refaster.anyOf(Math.signum(v) < 0, Math.signum(v) <= -1); + } + + @BeforeTemplate + boolean before(double v) { + return Refaster.anyOf(Math.signum(v) < 0, Math.signum(v) <= -1); + } + + @AfterTemplate + @AlsoNegation + boolean after(double v) { + return Math.signum(v) == -1; + } + } } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/PrimitiveRulesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/PrimitiveRulesTestInput.java index c1affa87e5d..8d038ca345f 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/PrimitiveRulesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/PrimitiveRulesTestInput.java @@ -164,4 +164,54 @@ boolean testFloatIsFinite() { boolean testDoubleIsFinite() { return Doubles.isFinite(1); } + + ImmutableSet testIntegerSignumIsPositive() { + return ImmutableSet.of( + Integer.signum(1) > 0, + Integer.signum(2) >= 1, + Integer.signum(3) <= 0, + Integer.signum(4) < 1); + } + + ImmutableSet testIntegerSignumIsNegative() { + return ImmutableSet.of( + Integer.signum(1) < 0, + Integer.signum(2) <= -1, + Integer.signum(3) >= 0, + Integer.signum(4) > -1); + } + + ImmutableSet testLongSignumIsPositive() { + return ImmutableSet.of( + Long.signum(1L) > 0, Long.signum(2L) >= 1, Long.signum(3L) <= 0, Long.signum(4L) < 1); + } + + ImmutableSet testLongSignumIsNegative() { + return ImmutableSet.of( + Long.signum(1L) < 0, Long.signum(2L) <= -1, Long.signum(3L) >= 0, Long.signum(4L) > -1); + } + + ImmutableSet testMathSignumIsPositive() { + return ImmutableSet.of( + Math.signum(1.0) > 0, + Math.signum(2.0) >= 1, + Math.signum(3.0F) > 0, + Math.signum(4.0F) >= 1, + Math.signum(5.0) <= 0, + Math.signum(6.0) < 1, + Math.signum(7.0F) <= 0, + Math.signum(8.0F) < 1); + } + + ImmutableSet testMathSignumIsNegative() { + return ImmutableSet.of( + Math.signum(1.0) < 0, + Math.signum(2.0) <= -1, + Math.signum(3.0F) < 0, + Math.signum(4.0F) <= -1, + Math.signum(5.0) >= 0, + Math.signum(6.0) > -1, + Math.signum(7.0F) >= 0, + Math.signum(8.0F) > -1); + } } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/PrimitiveRulesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/PrimitiveRulesTestOutput.java index 6f91d4d9ee5..58d3cac4389 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/PrimitiveRulesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/PrimitiveRulesTestOutput.java @@ -164,4 +164,54 @@ boolean testFloatIsFinite() { boolean testDoubleIsFinite() { return Double.isFinite(1); } + + ImmutableSet testIntegerSignumIsPositive() { + return ImmutableSet.of( + Integer.signum(1) == 1, + Integer.signum(2) == 1, + Integer.signum(3) != 1, + Integer.signum(4) != 1); + } + + ImmutableSet testIntegerSignumIsNegative() { + return ImmutableSet.of( + Integer.signum(1) == -1, + Integer.signum(2) == -1, + Integer.signum(3) != -1, + Integer.signum(4) != -1); + } + + ImmutableSet testLongSignumIsPositive() { + return ImmutableSet.of( + Long.signum(1L) == 1, Long.signum(2L) == 1, Long.signum(3L) != 1, Long.signum(4L) != 1); + } + + ImmutableSet testLongSignumIsNegative() { + return ImmutableSet.of( + Long.signum(1L) == -1, Long.signum(2L) == -1, Long.signum(3L) != -1, Long.signum(4L) != -1); + } + + ImmutableSet testMathSignumIsPositive() { + return ImmutableSet.of( + Math.signum(1.0) == 1, + Math.signum(2.0) == 1, + Math.signum(3.0F) == 1, + Math.signum(4.0F) == 1, + Math.signum(5.0) != 1, + Math.signum(6.0) != 1, + Math.signum(7.0F) != 1, + Math.signum(8.0F) != 1); + } + + ImmutableSet testMathSignumIsNegative() { + return ImmutableSet.of( + Math.signum(1.0) == -1, + Math.signum(2.0) == -1, + Math.signum(3.0F) == -1, + Math.signum(4.0F) == -1, + Math.signum(5.0) != -1, + Math.signum(6.0) != -1, + Math.signum(7.0F) != -1, + Math.signum(8.0F) != -1); + } }