Skip to content

Introduce RandomGeneratorRules Refaster rule collection#1756

Merged
rickie merged 9 commits intoPicnicSupermarket:masterfrom
timtebeek:random-rules
Jul 16, 2025
Merged

Introduce RandomGeneratorRules Refaster rule collection#1756
rickie merged 9 commits intoPicnicSupermarket:masterfrom
timtebeek:random-rules

Conversation

@timtebeek
Copy link
Contributor

@github-actions
Copy link

Looks good. No mutations were possible for these changes.
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

1 similar comment
@github-actions
Copy link

Looks good. No mutations were possible for these changes.
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

Copy link
Member

@rickie rickie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR @timtebeek. We'll include this one with your other PR soon. There were some holidays that resulted in some slower reviews 😬.

return ImmutableSet.of();
}

int testRandomNextInt() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We generally have 1 test method per Refaster rule :).

There is a RandomRules.class missing in the RefasterRulesTest#RULE_COLLECTIONS 😄. We'll see that the test will fail as it expects these tests to be combined in one method :)

import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase;

final class RandomRulesTest implements RefasterRuleCollectionTestCase {
@Override
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can simply omit this one :)

@github-actions
Copy link

Looks good. No mutations were possible for these changes.
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

1 similar comment
@github-actions
Copy link

Looks good. No mutations were possible for these changes.
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

@timtebeek timtebeek marked this pull request as draft July 11, 2025 15:08
Copy link
Member

@Stephan202 Stephan202 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a commit; PTAL.

Suggested commit message:

Introduce `RandomGeneratorRules` Refaster rule collection (#1756)

Comment on lines +15 to +23
@BeforeTemplate
int before(Random random, int bound) {
return (int) (random.nextDouble() * bound);
}

@BeforeTemplate
int before2(Random random, int bound) {
return (int) Math.round(random.nextDouble() * bound);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can even use Refaser.anyOf :)

Comment on lines +8 to +10
/** Refaster rules related to expressions dealing with {@link Random}. */
@OnlineDocumentation
final class RandomRules {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Turns out these rules generalize to any RandomGenerator; I'll rename and update accordingly.

Comment on lines +13 to +14
/** Prefer {@link Random#nextInt(int)} over casting a scaled random double to int. */
static final class RandomNextInt {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can introduce a similar rule for nextLong. There it actually fixes a bug, as the conversion to double reduces the effective domain of random longs that may be produced.

@Stephan202 Stephan202 added this to the 0.24.0 milestone Jul 14, 2025
@Stephan202
Copy link
Member

For @rickie: let's first merge #1758.

@github-actions
Copy link

Looks good. No mutations were possible for these changes.
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

@Stephan202
Copy link
Member

I see there's also RandomGenerator#nextDouble(double bound); added another commit.

@github-actions
Copy link

Looks good. No mutations were possible for these changes.
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

@Stephan202
Copy link
Member

I realized that random.nextDouble() * bound is always better written as random.nextDouble(bound), so added another commit.

@github-actions
Copy link

Looks good. No mutations were possible for these changes.
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

@Stephan202
Copy link
Member

Rebased and added one more commit :)

@Stephan202 Stephan202 changed the title Add new RandomRules with two simplifications Introduce RandomGeneratorRules Refaster rule collection Jul 15, 2025
@github-actions
Copy link

Looks good. No mutations were possible for these changes.
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

@Stephan202 Stephan202 marked this pull request as ready for review July 15, 2025 19:07
And document an idea.
@github-actions
Copy link

Looks good. No mutations were possible for these changes.
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

@rickie rickie merged commit 96bab5f into PicnicSupermarket:master Jul 16, 2025
16 checks passed
@timtebeek timtebeek deleted the random-rules branch July 16, 2025 16:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

Prefer Random.nextInt over nextDouble and truncation/rounding

3 participants