Skip to content

Suggest using next_multiple_of() instead of ((x + N-1) / N) * N or (x + N-1) & !(N-1) (for powers of two) #14543

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
sdroege opened this issue Apr 4, 2025 · 2 comments
Labels
A-lint Area: New lints

Comments

@sdroege
Copy link

sdroege commented Apr 4, 2025

What it does

See title.

Similar for x.is_multiple_of(y) and x % y == 0.

Advantage

More readable and overflow checks

Drawbacks

No response

Example

let x = ((x + 3) / 4) * 4;
let y = (y + 3) & !3;
let z = z % 4 == 0;

Could be written as:

let x = x.next_multiple_of(4);
let y = y.next_multiple_of(4);
let z = z.is_multiple_of(4);
@sdroege sdroege added the A-lint Area: New lints label Apr 4, 2025
@sdroege
Copy link
Author

sdroege commented Apr 16, 2025

There's also let z = (z & 3) == 0 for let z = z.is_multiple_of(4). And probably some other patterns I didn't think of yet.

@samueltardieu
Copy link
Contributor

There's also let z = (z & 3) == 0 for let z = z.is_multiple_of(4). And probably some other patterns I didn't think of yet.

Cf. #14292 (which wouldn't work with 3, by choice, but would work with 7).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

No branches or pull requests

2 participants