Skip to content

manual_div_ceil lint can suggest broken code #13843

Closed
@neocturne

Description

@neocturne

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thing

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions