-
Notifications
You must be signed in to change notification settings - Fork 1.7k
u8::max as usize
instead of u8::MAX as usize
should warn
#13973
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
Comments
Example code which triggers the assert, but emits no warnings from Cargo or Clippy in pedantic mode: #[deny(clippy::pedantic)]
pub fn main() {
let x: usize = 65_535;
// Should be u16::MAX
if x < u16::max as usize {
println!("fits in a u16!");
assert!(x < 65_535);
} else {
println!("Too big!");
}
} Although Possible solutions:
|
I don't think we can make I think in this case, the best might be a specialized new lint. Something like |
Another thing to notice too that (though they are deprecated) methods like |
Here's a bug occurring in practice due to this footgun, leading to a CVE: mozilla/gecko-dev@eaa2194#diff-ae44822f5ed147b5ecf38d71f1c752533de39a19d8b786ee8d94b4874ce6af0cL1037 https://www.mozilla.org/en-US/security/advisories/mfsa2025-09/#CVE-2025-1014 |
Fixes #13973. I don't think we can make `fn_to_numeric_cast_any` to be emitted in some special cases. Its category cannot be changed at runtime. I think in this case, the best might be a specialized new lint so we can target exactly what we want. ---- changelog: Add new `confusing_method_to_numeric_cast` lint
What it does
It should warn on code like
u8::max as usize
, or similar code withmin
or with other integer types.Advantage
u8::MAX
is a constant equal to 2^8-1.u8::MAX as usize
casts that value to a larger integer size.u8::max
is a function that takes twou8
values and returns the larger one.u8::max as usize
takes the address of that function, which is almost certainly not intended.Drawbacks
No response
Example
Could be written as:
The text was updated successfully, but these errors were encountered: