You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It should warn on code like u8::max as usize, or similar code with min 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 two u8 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
u8::max asusize
Could be written as:
u8::MAXasusize
The text was updated successfully, but these errors were encountered:
Example code which triggers the assert, but emits no warnings from Cargo or Clippy in pedantic mode:
#[deny(clippy::pedantic)]pubfnmain(){let x:usize = 65_535;// Should be u16::MAXif x < u16::max asusize{println!("fits in a u16!");assert!(x < 65_535);}else{println!("Too big!");}}
Although clippy::fn_to_numeric_cast_any will catch this mistake, it's not enabled in pedantic mode and will flag all other uses where function pointers are converted to integers.
Possible solutions:
Enable clippy::fn_to_numeric_cast_any by default for the special case of standard library functions where associated constants and functions are lookalikes.
Warn when function pointers are cast to usize in a non-equality comparison.
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: