-
Notifications
You must be signed in to change notification settings - Fork 1.7k
cast_possible_truncation
triggers when the value is guranteed to not be truncated
#7486
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
I've hit an even simpler form of this, where the modulus is an integer literal. I believe that Clippy should be able to avoid triggering in the literal case at least. pub fn f(val: u64) {
let digit = (val % 62) as u8;
println!("{}", digit);
} error: casting `u64` to `u8` may truncate the value
--> src/main.rs:2:17
|
2 | let digit = (val % 62) as u8;
| ^^^^^^^^^^^^^^^^
|
= note: `-D clippy::cast-possible-truncation` implied by `-D clippy::pedantic`
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_possible_truncation |
I have a (possibly partial) solution for this in #7819. |
Hmm, #7819 must not have been complete because this still lints on things like pub fn foo(i: u16) -> u8 {
(i % 256) as u8
} |
I know why that is. Lemme fork and PR quick. There's basically a math error going on: Edit: Struggling to nail down how the function works and struggling more to make a good test. |
cast_possible_truncation: Fix some false-positive cases Fix partially #7486 (comment). Fix #9613. changelog: [`cast_possible_truncation`]: fix some false-positive on % operator, valid constants, and size_of
Lint name:
cast_possible_truncation
I tried this code:
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
:The text was updated successfully, but these errors were encountered: