Re: [PATCH v6 6/6] rust: enable `clippy::cast_lossless` lint
From: Benno Lossin
Date: Tue Mar 25 2025 - 06:40:22 EST
On Mon Mar 24, 2025 at 11:01 PM CET, Tamir Duberstein wrote:
> Before Rust 1.29.0, Clippy introduced the `cast_lossless` lint [1]:
>
>> Rust’s `as` keyword will perform many kinds of conversions, including
>> silently lossy conversions. Conversion functions such as `i32::from`
>> will only perform lossless conversions. Using the conversion functions
>> prevents conversions from becoming silently lossy if the input types
>> ever change, and makes it clear for people reading the code that the
>> conversion is lossless.
>
> While this doesn't eliminate unchecked `as` conversions, it makes such
> conversions easier to scrutinize. It also has the slight benefit of
> removing a degree of freedom on which to bikeshed. Thus apply the
> changes and enable the lint -- no functional change intended.
>
> Link: https://rust-lang.github.io/rust-clippy/master/index.html#cast_lossless [1]
> Suggested-by: Benno Lossin <benno.lossin@xxxxxxxxx>
> Link: https://lore.kernel.org/all/D8ORTXSUTKGL.1KOJAGBM8F8TN@xxxxxxxxx/
> Signed-off-by: Tamir Duberstein <tamird@xxxxxxxxx>
One nit below, but you may add:
Reviewed-by: Benno Lossin <benno.lossin@xxxxxxxxx>
> ---
> Makefile | 1 +
> drivers/gpu/drm/drm_panic_qr.rs | 10 +++++-----
> rust/bindings/lib.rs | 2 +-
> rust/kernel/net/phy.rs | 4 ++--
> 4 files changed, 9 insertions(+), 8 deletions(-)
> diff --git a/rust/bindings/lib.rs b/rust/bindings/lib.rs
> index 0486a32ed314..591e4ca9bc54 100644
> --- a/rust/bindings/lib.rs
> +++ b/rust/bindings/lib.rs
> @@ -25,7 +25,7 @@
> )]
>
> #[allow(dead_code)]
> -#[allow(clippy::ptr_as_ptr)]
> +#[allow(clippy::cast_lossless, clippy::ptr_as_ptr)]
Not sure if we instead want this in a separate attribute, ultimately it
doesn't really matter, but why should `undocumented_unsafe_blocks` be
special?
---
Cheers,
Benno
> #[allow(clippy::undocumented_unsafe_blocks)]
> mod bindings_raw {
> // Manual definition for blocklisted types.