Skip to content

Commit 1dee49f

Browse files
Stephan202rickie
authored andcommitted
Multiplication is commutative
And document an idea.
1 parent 4786312 commit 1dee49f

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
import tech.picnic.errorprone.refaster.annotation.OnlineDocumentation;
88

99
/** Refaster rules related to expressions dealing with {@link RandomGenerator} instances. */
10+
// XXX: Consider introducing an Error Prone bug pattern that identifies expressions of the form `c +
11+
// r.next{Int,Long,Double}(b)` for literals `c` and `b`, and replaces them with
12+
// `r.next{Int,Long,Double}(c, s)` where `s = c + b`. Similarly with the operands commuted, and
13+
// for the latter variant also for operator `-`.
1014
@OnlineDocumentation
1115
final class RandomGeneratorRules {
1216
private RandomGeneratorRules() {}
@@ -18,7 +22,7 @@ private RandomGeneratorRules() {}
1822
static final class RandomGeneratorNextDouble {
1923
@BeforeTemplate
2024
double before(RandomGenerator random, double bound) {
21-
return random.nextDouble() * bound;
25+
return Refaster.anyOf(random.nextDouble() * bound, bound * random.nextDouble());
2226
}
2327

2428
@AfterTemplate

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ final class RandomGeneratorRulesTest implements RefasterRuleCollectionTestCase {
1111
ImmutableSet<Double> testRandomGeneratorNextDouble() {
1212
return ImmutableSet.of(
1313
new Random().nextDouble() * 1,
14-
new SplittableRandom().nextDouble() * 2L,
14+
2L * new SplittableRandom().nextDouble(),
1515
new SecureRandom().nextDouble() * 3.0);
1616
}
1717

0 commit comments

Comments
 (0)