Skip to content

catch manual next_multiple_of #16728

@nyurik

Description

@nyurik

What it does

detect manual implementation of the next_multiple_of, e.g. a.div_ceil(b) * b. Note that this may combine clippy::manual_div_ceil detection as well, e.g. (x + (y - 1)) / y * y.

Make sure to include checked_next_multiple_of variant detection

Advantage

  • shows clear intent rather
  • reduces code complexity
  • prevents accidental bugs

Drawbacks

No response

Example

use std::ops::Mul;

fn mult1(a: u32, b: u32) -> u32 {
    a.div_ceil(b) * b
}

fn mult2(a: u32, b: u32) -> u32 {
    a.div_ceil(b).mul(b)
}

fn mult3(a: u32, b: u32) -> u32 {
    (a + (b - 1)) / b * b
}

Could be written as:

a.next_multiple_of(b)

Comparison with existing lints

manual_div_ceil does something similar

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lints

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions