Skip to content

cast_possible_truncation triggers when the value is guranteed to not be truncated #7486

Open
@Luro02

Description

@Luro02

Lint name: cast_possible_truncation

I tried this code:

#![deny(clippy::cast_possible_truncation)]

fn modulo(number: u64, other: u32) -> u32 {
    // a u64 with guranteed value to be less than or equal to u32::max_value()
    let other_u64 = other as u64;
    // modulo gurantees that the following invariant holds:
    // result < other_u64
    // which implies that result < u32::max_value()
    let result = number % other_u64;
    result as u32
}

fn main() {
    assert_eq!(modulo(11_u64, 5_u32), 1_u32);
}

I expected to see this happen: the lint should not trigger

Instead, this happened: the lint about a possible truncation has been emitted

Meta

  • cargo clippy -V: clippy 0.1.55 (02718709 2021-07-22)
  • rustc -Vv:
rustc 1.55.0-nightly (027187094 2021-07-22)
binary: rustc
commit-hash: 027187094ee05011d6602f5742f550851ccc7fd6
commit-date: 2021-07-22
host: x86_64-pc-windows-msvc
release: 1.55.0-nightly
LLVM version: 12.0.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions