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

cast_possible_truncation suggestion problem #13814

Open
leonardo-m opened this issue Dec 11, 2024 · 0 comments
Open

cast_possible_truncation suggestion problem #13814

leonardo-m opened this issue Dec 11, 2024 · 0 comments
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@leonardo-m
Copy link

Summary

Clippy cast_possible_truncation nusery lint gives an wrong suggestion.

Lint Name

cast_possible_truncation

Reproducer

I tried this code:

#![allow(dead_code)]
#![warn(clippy::all)]
#![warn(clippy::nursery)]
#![warn(clippy::pedantic)]

const X1: u16 = 1251_u16.ilog(3) as u16;
const X2: u16 = 1251_u16.ilog(3) as _;

fn main() {}

I saw this happen:

warning: casting `u32` to `u16` may truncate the value
 --> src/main.rs:6:17
  |
6 | const X1: u16 = 1251_u16.ilog(3) as u16;
  |                 ^^^^^^^^^^^^^^^^^^^^^^^
  |
  = help: if this is intentional allow the lint with `#[allow(clippy::cast_possible_truncation)]` ...
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_possible_truncation
note: the lint level is defined here
 --> src/main.rs:4:9
  |
4 | #![warn(clippy::pedantic)]
  |         ^^^^^^^^^^^^^^^^
  = note: `#[warn(clippy::cast_possible_truncation)]` implied by `#[warn(clippy::pedantic)]`
help: ... or use `try_from` and handle the error accordingly
  |
6 | const X1: u16 = u16::try_from(1251_u16.ilog(3));
  |                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

warning: casting `u32` to `u16` may truncate the value
 --> src/main.rs:7:17
  |
7 | const X2: u16 = 1251_u16.ilog(3) as _;
  |                 ^^^^^^^^^^^^^^^^^^^^^
  |
  = help: if this is intentional allow the lint with `#[allow(clippy::cast_possible_truncation)]` ...
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_possible_truncation
help: ... or use `try_from` and handle the error accordingly
  |
7 | const X2: u16 = 1251_u16.ilog(3).try_into();
  |                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~

The suggested code can't compile, even if you add unwrap(), because it's a const context.

Version

v.1.85 Nightly

Additional Labels

No response

@leonardo-m leonardo-m added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Dec 11, 2024
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 I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

No branches or pull requests

1 participant