Skip to content

cast_lossless should not warn about casts to u128 #12492

Closed
@smoelius

Description

@smoelius

Summary

cast_lossless probably shouldn't warn when the "to" type is u128.

My understanding is that u128 is the largest integer type Rust supports. So for the warning to be valid, the programmer would have to change the type of the casted data and Rust would have to support additional integer types.

Apologies if this has been discussed before.

Lint Name

cast_lossless

Reproducer

I tried this code (playground):

#![warn(clippy::cast_lossless)]

fn main() {
    let x: u64 = 1;
    let _y = x as u128;
}

I saw this happen:

warning: casting `u64` to `u128` may become silently lossy if you later change the type
 --> src/main.rs:5:14
  |
5 |     let _y = x as u128;
  |              ^^^^^^^^^ help: try: `u128::from(x)`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_lossless
note: the lint level is defined here
 --> src/main.rs:1:9
  |
1 | #![warn(clippy::cast_lossless)]
  |         ^^^^^^^^^^^^^^^^^^^^^

warning: `playground` (bin "playground") generated 1 warning (run `cargo clippy --fix --bin "playground"` to apply 1 suggestion)

I expected to see this happen:

No warning.

Version

No response

Additional Labels

No response

Metadata

Metadata

Assignees

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