Skip to content

Commit

Permalink
Introduce {Integer,Long,Math}SignumIs{Positive,Negative} Refaster r…
Browse files Browse the repository at this point in the history
…ules
  • Loading branch information
Stephan202 committed Oct 7, 2023
1 parent da5eea8 commit 1de627a
Show file tree
Hide file tree
Showing 3 changed files with 196 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,54 @@ boolean testFloatIsFinite() {
boolean testDoubleIsFinite() {
return Doubles.isFinite(1);
}

ImmutableSet<Boolean> testIntegerSignumIsPositive() {
return ImmutableSet.of(
Integer.signum(1) > 0,
Integer.signum(2) >= 1,
Integer.signum(3) <= 0,
Integer.signum(4) < 1);
}

ImmutableSet<Boolean> testIntegerSignumIsNegative() {
return ImmutableSet.of(
Integer.signum(1) < 0,
Integer.signum(2) <= -1,
Integer.signum(3) >= 0,
Integer.signum(4) > -1);
}

ImmutableSet<Boolean> testLongSignumIsPositive() {
return ImmutableSet.of(
Long.signum(1L) > 0, Long.signum(2L) >= 1, Long.signum(3L) <= 0, Long.signum(4L) < 1);
}

ImmutableSet<Boolean> testLongSignumIsNegative() {
return ImmutableSet.of(
Long.signum(1L) < 0, Long.signum(2L) <= -1, Long.signum(3L) >= 0, Long.signum(4L) > -1);
}

ImmutableSet<Boolean> 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<Boolean> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,54 @@ boolean testFloatIsFinite() {
boolean testDoubleIsFinite() {
return Double.isFinite(1);
}

ImmutableSet<Boolean> testIntegerSignumIsPositive() {
return ImmutableSet.of(
Integer.signum(1) == 1,
Integer.signum(2) == 1,
Integer.signum(3) != 1,
Integer.signum(4) != 1);
}

ImmutableSet<Boolean> testIntegerSignumIsNegative() {
return ImmutableSet.of(
Integer.signum(1) == -1,
Integer.signum(2) == -1,
Integer.signum(3) != -1,
Integer.signum(4) != -1);
}

ImmutableSet<Boolean> testLongSignumIsPositive() {
return ImmutableSet.of(
Long.signum(1L) == 1, Long.signum(2L) == 1, Long.signum(3L) != 1, Long.signum(4L) != 1);
}

ImmutableSet<Boolean> testLongSignumIsNegative() {
return ImmutableSet.of(
Long.signum(1L) == -1, Long.signum(2L) == -1, Long.signum(3L) != -1, Long.signum(4L) != -1);
}

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

0 comments on commit 1de627a

Please sign in to comment.