Skip to content
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

manual_div_ceil lint can suggest broken code #13843

Open
neocturne opened this issue Dec 17, 2024 · 0 comments · May be fixed by #13864
Open

manual_div_ceil lint can suggest broken code #13843

neocturne opened this issue Dec 17, 2024 · 0 comments · May be fixed by #13864
Labels
C-bug Category: Clippy is not doing the correct thing

Comments

@neocturne
Copy link

Summary

The manual_div_ceil list may suggest broken code with a method called on an integer literal of ambiguous type. Clippy should either not make the suggestion or insert the correct type suffix.

Reproducer

I tried this code:

fn main() {
    let blocks = 3usize;
    let _ = (4096 + blocks - 1) / blocks;
}

Clippy suggests the following fix:

warning: manually reimplementing `div_ceil`
 --> src/main.rs:3:13
  |
3 |     let _ = (4096 + blocks - 1) / blocks;
  |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `.div_ceil()`: `4096.div_ceil(blocks)`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_div_ceil
  = note: `#[warn(clippy::manual_div_ceil)]` on by default

The suggested code fails to compile because of the ambiguous integer type:

error[E0689]: can't call method `div_ceil` on ambiguous numeric type `{integer}`
 --> src/main.rs:3:18
  |
3 |     let _ = 4096.div_ceil(blocks);
  |                  ^^^^^^^^
  |
help: you must specify a concrete type for this numeric value, like `i32`
  |
3 |     let _ = 4096_i32.div_ceil(blocks);
  |             ~~~~~~~~

Version

rustc 1.83.0 (90b35a623 2024-11-26)
binary: rustc
commit-hash: 90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf
commit-date: 2024-11-26
host: x86_64-unknown-linux-gnu
release: 1.83.0
LLVM version: 19.1.1

Also affected:

rustc 1.85.0-nightly (c26db435b 2024-12-15)
binary: rustc
commit-hash: c26db435bf8aee2efc397aab50f3a21eb351d6e5
commit-date: 2024-12-15
host: x86_64-unknown-linux-gnu
release: 1.85.0-nightly
LLVM version: 19.1.5

Additional Labels

No response

@neocturne neocturne added the C-bug Category: Clippy is not doing the correct thing label Dec 17, 2024
@lapla-cogito lapla-cogito linked a pull request Dec 22, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant