Skip to content

Commit ac2cbe7

Browse files
committed
Introducing a new RandomGeneratorRule
1 parent 18a126a commit ac2cbe7

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/RandomGeneratorRules.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,19 @@ long after(RandomGenerator random, long bound) {
7575
return random.nextLong(bound);
7676
}
7777
}
78+
79+
/**
80+
* Prefer {@link RandomGenerator#nextInt(int origin, int bound)} over more contrived alternatives.
81+
*/
82+
static final class RandomGeneratorNextIntWithOrigin {
83+
@BeforeTemplate
84+
int before(RandomGenerator random, int a, int b) {
85+
return Refaster.anyOf(a + random.nextInt(b));
86+
}
87+
88+
@AfterTemplate
89+
int after(RandomGenerator random, int a, int b) {
90+
return random.nextInt(a, a + b);
91+
}
92+
}
7893
}

error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/RandomGeneratorRulesTestInput.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,8 @@ ImmutableSet<Long> testRandomGeneratorNextLong() {
2727
(long) new SecureRandom().nextDouble(3L),
2828
Math.round(ThreadLocalRandom.current().nextDouble(4L)));
2929
}
30+
31+
ImmutableSet<Integer> testRandomGeneratorNextIntWithOrigin() {
32+
return ImmutableSet.of(20 + new Random().nextInt(80));
33+
}
3034
}

error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/RandomGeneratorRulesTestOutput.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,8 @@ ImmutableSet<Long> testRandomGeneratorNextLong() {
2626
new SecureRandom().nextLong(3L),
2727
ThreadLocalRandom.current().nextLong(4L));
2828
}
29+
30+
ImmutableSet<Integer> testRandomGeneratorNextIntWithOrigin() {
31+
return ImmutableSet.of(new Random().nextInt(20, 20 + 80));
32+
}
2933
}

0 commit comments

Comments
 (0)