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

Open
@sdroege

Description

@sdroege

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);

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions