Open
Description
Summary
Here there are some cases where the manual_is_multiple_of lint suggests the use of code that leads to problems.
Reproducer
Some basic Rust code:
#![allow(dead_code)]
#![warn(clippy::all)]
fn foo() -> Option<u64> {
let mut n: u64 = 150_000_000;
(2 ..)
.find(|p| { while n % p == 0 { n /= p; } n <= 1 })
}
const fn generate_primes<const N: usize>() -> [u64; N] {
let mut result = [0; N];
if N == 0 { return result; }
result[0] = 2;
if N == 1 { return result; }
let mut idx = 1;
let mut p = 3;
while idx < N {
let mut j = 0;
while j < idx && p % result[j] != 0 {
j += 1;
}
if j == idx {
result[idx] = p;
idx += 1;
}
p += 1;
}
result
}
fn bar() -> u32 {
let d = |n| (1 ..= n / 2).filter(|i| n % i == 0).sum();
(1 .. 1_000).filter(|&i| i == d(d(i)) && i != d(i)).sum()
}
fn main() {}
On it Clippy gives three suggestions to use is_multiple_of:
Checking test1 v0.1.0 (...)
warning: manual implementation of `.is_multiple_of()`
--> src\main.rs:8:23
|
8 | .find(|p| { while n % p == 0 { n /= p; } n <= 1 })
| ^^^^^^^^^^ help: replace with: `n.is_multiple_of(p)`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_is_multiple_of
note: the lint level is defined here
--> src\main.rs:2:9
|
2 | #![warn(clippy::all)]
| ^^^^^^^^^^^
= note: `#[warn(clippy::manual_is_multiple_of)]` implied by `#[warn(clippy::all)]`
warning: manual implementation of `.is_multiple_of()`
--> src\main.rs:20:26
|
20 | while j < idx && p % result[j] != 0 {
| ^^^^^^^^^^^^^^^^^^ help: replace with: `!p.is_multiple_of(result[j])`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_is_multiple_of
warning: manual implementation of `.is_multiple_of()`
--> src\main.rs:33:42
|
33 | let d = |n| (1 ..= n / 2).filter(|i| n % i == 0).sum();
| ^^^^^^^^^^ help: replace with: `n.is_multiple_of(i)`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_is_multiple_of
warning: `test1` (bin "test1") generated 3 warnings (run `cargo clippy --fix --bin "test1"` to apply 3 suggestions)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.32s
If I apply those suggestions blindly:
#![allow(dead_code)]
#![warn(clippy::all)]
fn foo() -> Option<u64> {
let mut n: u64 = 150_000_000;
(2 ..)
.find(|p| { while n.is_multiple_of(p) { n /= p; } n <= 1 })
}
const fn generate_primes<const N: usize>() -> [u64; N] {
let mut result = [0; N];
if N == 0 { return result; }
result[0] = 2;
if N == 1 { return result; }
let mut idx = 1;
let mut p = 3;
while idx < N {
let mut j = 0;
while j < idx && !p.is_multiple_of(result[j]) {
j += 1;
}
if j == idx {
result[idx] = p;
idx += 1;
}
p += 1;
}
result
}
fn bar() -> u32 {
let d = |n| (1 ..= n / 2).filter(|i| n.is_multiple_of(i)).sum();
(1 .. 1_000).filter(|&i| i == d(d(i)) && i != d(i)).sum()
}
fn main() {}
The code doesn't compile, with few errors:
error[E0308]: mismatched types
--> ...\main.rs:8:40
|
8 | .find(|p| { while n.is_multiple_of(p) { n /= p; } n <= 1 })
| -------------- ^ expected `u64`, found `&{integer}`
| |
| arguments to this method are incorrect
|
note: method defined here
--> /rustc/6677875279b560442a07a08d5119b4cd6b3c5593\library\core\src\num\mod.rs:1151:5
= note: this error originates in the macro `uint_impl` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider dereferencing the borrow
|
8 | .find(|p| { while n.is_multiple_of(*p) { n /= p; } n <= 1 })
| +
error[E0689]: can't call method `is_multiple_of` on ambiguous numeric type `{integer}`
--> ...\main.rs:20:29
|
20 | while j < idx && !p.is_multiple_of(result[j]) {
| ^^^^^^^^^^^^^^
|
help: you must specify a type for this binding, like `i32`
|
17 | let mut p: i32 = 3;
| +++++
error[E0282]: type annotations needed
--> ...\main.rs:33:14
|
33 | let d = |n| (1 ..= n / 2).filter(|i| n.is_multiple_of(i)).sum();
| ^ - type must be known at this point
|
help: consider giving this closure parameter an explicit type
|
33 | let d = |n: /* Type */| (1 ..= n / 2).filter(|i| n.is_multiple_of(i)).sum();
| ++++++++++++
error: aborting due to 3 previous errors
Should Clippy get smarter in suggesting the use of is_multiple_of?
Version
rustc 1.90.0-nightly (667787527 2025-07-02)
binary: rustc
commit-hash: 6677875279b560442a07a08d5119b4cd6b3c5593
commit-date: 2025-07-02
host: x86_64-pc-windows-gnu
release: 1.90.0-nightly
LLVM version: 20.1.7
Additional Labels
No response