From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 583262FE38A for ; Wed, 18 Jun 2025 17:40:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750268450; cv=none; b=KjdzxKqaz0lkyj+yfPaVOmtuT3NVrzRvZgr6BOsPnM6gPtCfUOaNeyTuKqA3fe/4PiG51EqGPWDohYm+r2oBeMB6QRMTP0MsY3wsSVrlMtdCke1Xxc7E5eAWC29R6avzTvW85/OOajjEznqH52RuSzcsPRK3OTzjuvnArVslhUY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750268450; c=relaxed/simple; bh=4sFWwkFXi8BvpD8IMU8juMRg7f+DirSsf+Ud/E9pRzw=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=K05xPhE04UPLHNLjUt+Qkei74hrr1aS5Pt1OYRtUFwr5TfFUMyvcUjktqgCeI9xDIyEEVgWhouKuftfl8M193pP+2iMEFQlT3BjP18qJX3CJWsG3V4SAjIsL5MhckRoC1uPO4t0OhWUkjaPdSW/lwE6ZNxR2MduK4qY4KZcioGE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=ZKTfdMki; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="ZKTfdMki" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1750268444; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=QyjtI6z4LqPR2c4duviV1oQl9DWLYnzH4cNekvsPaJY=; b=ZKTfdMkiRBxVhKaZlowFwwUCoT2CSQnfLD4e79dpP6Wa9/0f6qvuE+GQ1xHN5JrCtCKQ0L zqCzqWl9QoIhk0Dbz6m7moPI9z0wdAi2jpiAgT4dz8zIGZ6oOmaz/NDPiCY3xB19d9EuqI 3hjr3lB9dR0/y09caxJrw2pAU2G5srQ= Received: from mail-ej1-f69.google.com (mail-ej1-f69.google.com [209.85.218.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-363-0Zknbbn2OhipUboWMTDYwA-1; Wed, 18 Jun 2025 13:40:43 -0400 X-MC-Unique: 0Zknbbn2OhipUboWMTDYwA-1 X-Mimecast-MFC-AGG-ID: 0Zknbbn2OhipUboWMTDYwA_1750268442 Received: by mail-ej1-f69.google.com with SMTP id a640c23a62f3a-ae012f87dd1so109418166b.3 for ; Wed, 18 Jun 2025 10:40:42 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1750268442; x=1750873242; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=QyjtI6z4LqPR2c4duviV1oQl9DWLYnzH4cNekvsPaJY=; b=hnDz+tuYzIWQDcuBCpXq3sxPo3jZJxxMebIeUVnXUg/3UHkm3aDDXsHwpn7soDGVq7 Dox41+4jjBU4YH+p7DU8MyCIOIuAq0v2LtA5O9oXCNHEwDMis0uLnhv/am+GDNmlxBAZ 1GvqEL1+wClaIlAtyy28Nk8GXjrLzRdHGBTXIDLwgCD/48j/zXCP81WqQOYH7ND8e5OO njS8dHhXaqpIiZsCuP+ZKVJZG0Rv+L1EloDgHbOfM2DVAxmCcEgO92rObacRlazAA4RN totcYkwEmAEKN/o/mH5V/wXLC9XcNaUkmlL2+i6KysNk2hEr6NWP4Wj+3yzthZjCr0vg CUyQ== X-Forwarded-Encrypted: i=1; AJvYcCW8lY1HsJkG4UDXGpwbNRrW3Uxh92ksidgB9pwKdgTHkFr9c104bkLUPGqoBZM1U1jXFYuFWPINGalGsor6uw==@vger.kernel.org X-Gm-Message-State: AOJu0YzYCFQJJsouAtyGtsG6grPCV8YtI5AlSNk8XUt6raaswAneNMdZ YdeNrXu4fhVwPoifcDqd0W/u3lAGTj4sTFxMOzz20qstlHv8aB6PutpPVSIZgPtA/VJiiWCUgm1 2IR0cJd5/XC1+zT53B0CoOEnPcTZYVyUI9yqCerXSmObks3dXBbmkOfu9zzmPUZ5SAtYE X-Gm-Gg: ASbGncsNmQp/g1pWRj3s6zwfiGKFyax5F4EeEwaBWlN7xVMXQbGrvijofwGWfml0Ifh 8h3OFyqwc/LyEW33DQI8fW8cIpesNLmjk1vLk/AyY6n136D7ffo2nJyNqBUMPaAZQOthX11uQWl f0qXZA5GYfPqmCfbZ79niDxVUWGpBjKgEfW5YEUV/kTBf3037fkKX9TZeb8wuBT1/usS3WPiril S5yoT9+vcvWKjGq89FWD1WbeuZXtI+8l/AFwjCaLUFoyKKsR7RPsDaWFL4s3MtkLG9ALk52yAyi bnMlt2/9wQT7mb/1szqOR9uQWvRpYY8EcGEp0svy33LEmLO4n6UUzDNbvzCBKg== X-Received: by 2002:a17:907:3f1f:b0:ad2:417b:2ab5 with SMTP id a640c23a62f3a-adfad4f5a5amr1527477166b.60.1750268441546; Wed, 18 Jun 2025 10:40:41 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHJxKyWXcdF99utGWSG81lMA2wUOYJgcYHm0gBuwbodj41pCAPM9kqV+i3SsT8FRKkHC4LNpQ== X-Received: by 2002:a17:907:3f1f:b0:ad2:417b:2ab5 with SMTP id a640c23a62f3a-adfad4f5a5amr1527470666b.60.1750268440967; Wed, 18 Jun 2025 10:40:40 -0700 (PDT) Received: from ?IPV6:2a01:e0a:c:37e0:8998:e0cf:68cc:1b62? ([2a01:e0a:c:37e0:8998:e0cf:68cc:1b62]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-adec897c397sm1066045766b.167.2025.06.18.10.40.38 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 18 Jun 2025 10:40:40 -0700 (PDT) Message-ID: Date: Wed, 18 Jun 2025 19:40:36 +0200 Precedence: bulk X-Mailing-List: rust-for-linux@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v12 5/6] rust: enable `clippy::cast_lossless` lint To: Tamir Duberstein , Masahiro Yamada , Nathan Chancellor , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?UTF-8?Q?Bj=C3=B6rn_Roy_Baron?= , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Greg Kroah-Hartman , "Rafael J. Wysocki" , Brendan Higgins , David Gow , Rae Moar , Bjorn Helgaas , Luis Chamberlain , Russ Weight , Rob Herring , Saravana Kannan , Abdiel Janulgue , Daniel Almeida , Robin Murphy , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Simona Vetter , FUJITA Tomonori , Nicolas Schier , Frederic Weisbecker , Lyude Paul , Thomas Gleixner , Anna-Maria Behnsen , Benno Lossin , =?UTF-8?Q?Krzysztof_Wilczy=C5=84ski?= , John Stultz , Stephen Boyd , Lorenzo Stoakes , "Liam R. Howlett" , Breno Leitao , Viresh Kumar Cc: linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com, linux-pci@vger.kernel.org, linux-block@vger.kernel.org, devicetree@vger.kernel.org, dri-devel@lists.freedesktop.org, netdev@vger.kernel.org, linux-mm@kvack.org, linux-pm@vger.kernel.org, nouveau@lists.freedesktop.org References: <20250615-ptr-as-ptr-v12-0-f43b024581e8@gmail.com> <20250615-ptr-as-ptr-v12-5-f43b024581e8@gmail.com> From: Jocelyn Falempe In-Reply-To: <20250615-ptr-as-ptr-v12-5-f43b024581e8@gmail.com> X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: wd1GM76_cwA9w0X6tMIZ8sXXMh6liAv1bwGnav4i7Ds_1750268442 X-Mimecast-Originator: redhat.com Content-Language: en-US, fr Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 15/06/2025 22:55, 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. Thanks, it looks good to me, for the drm_panic_qr.rs part. Acked-by: Jocelyn Falempe > > Link: https://rust-lang.github.io/rust-clippy/master/index.html#cast_lossless [1] > Suggested-by: Benno Lossin > Link: https://lore.kernel.org/all/D8ORTXSUTKGL.1KOJAGBM8F8TN@proton.me/ > Reviewed-by: Benno Lossin > Reviewed-by: Boqun Feng > Signed-off-by: Tamir Duberstein > --- > Makefile | 1 + > drivers/gpu/drm/drm_panic_qr.rs | 4 ++-- > drivers/gpu/nova-core/regs.rs | 2 +- > drivers/gpu/nova-core/regs/macros.rs | 2 +- > rust/bindings/lib.rs | 1 + > rust/kernel/net/phy.rs | 4 ++-- > rust/uapi/lib.rs | 1 + > 7 files changed, 9 insertions(+), 6 deletions(-) > > diff --git a/Makefile b/Makefile > index 0ba22c361de8..29cf39be14de 100644 > --- a/Makefile > +++ b/Makefile > @@ -481,6 +481,7 @@ export rust_common_flags := --edition=2021 \ > -Wclippy::all \ > -Wclippy::as_ptr_cast_mut \ > -Wclippy::as_underscore \ > + -Wclippy::cast_lossless \ > -Wclippy::ignored_unit_patterns \ > -Wclippy::mut_mut \ > -Wclippy::needless_bitwise_bool \ > diff --git a/drivers/gpu/drm/drm_panic_qr.rs b/drivers/gpu/drm/drm_panic_qr.rs > index dd55b1cb764d..6b59d19ab631 100644 > --- a/drivers/gpu/drm/drm_panic_qr.rs > +++ b/drivers/gpu/drm/drm_panic_qr.rs > @@ -404,7 +404,7 @@ fn pop3(&mut self) -> Option<(u16, usize)> { > let mut out = 0; > let mut exp = 1; > for i in 0..poplen { > - out += self.decimals[self.len + i] as u16 * exp; > + out += u16::from(self.decimals[self.len + i]) * exp; > exp *= 10; > } > Some((out, NUM_CHARS_BITS[poplen])) > @@ -425,7 +425,7 @@ fn next(&mut self) -> Option { > match self.segment { > Segment::Binary(data) => { > if self.offset < data.len() { > - let byte = data[self.offset] as u16; > + let byte = u16::from(data[self.offset]); > self.offset += 1; > Some((byte, 8)) > } else { > diff --git a/drivers/gpu/nova-core/regs.rs b/drivers/gpu/nova-core/regs.rs > index 5a1273230306..c1cb6d4c49ee 100644 > --- a/drivers/gpu/nova-core/regs.rs > +++ b/drivers/gpu/nova-core/regs.rs > @@ -32,7 +32,7 @@ pub(crate) fn architecture(self) -> Result { > pub(crate) fn chipset(self) -> Result { > self.architecture() > .map(|arch| { > - ((arch as u32) << Self::IMPLEMENTATION.len()) | self.implementation() as u32 > + ((arch as u32) << Self::IMPLEMENTATION.len()) | u32::from(self.implementation()) > }) > .and_then(Chipset::try_from) > } > diff --git a/drivers/gpu/nova-core/regs/macros.rs b/drivers/gpu/nova-core/regs/macros.rs > index 7ecc70efb3cd..6851af8b5885 100644 > --- a/drivers/gpu/nova-core/regs/macros.rs > +++ b/drivers/gpu/nova-core/regs/macros.rs > @@ -264,7 +264,7 @@ pub(crate) fn $field(self) -> $res_type { > pub(crate) fn [](mut self, value: $to_type) -> Self { > const MASK: u32 = $name::[<$field:upper _MASK>]; > const SHIFT: u32 = $name::[<$field:upper _SHIFT>]; > - let value = ((value as u32) << SHIFT) & MASK; > + let value = (u32::from(value) << SHIFT) & MASK; > self.0 = (self.0 & !MASK) | value; > > self > diff --git a/rust/bindings/lib.rs b/rust/bindings/lib.rs > index 81b6c7aa4916..7631c9f6708d 100644 > --- a/rust/bindings/lib.rs > +++ b/rust/bindings/lib.rs > @@ -25,6 +25,7 @@ > )] > > #[allow(dead_code)] > +#[allow(clippy::cast_lossless)] > #[allow(clippy::ptr_as_ptr)] > #[allow(clippy::undocumented_unsafe_blocks)] > #[cfg_attr(CONFIG_RUSTC_HAS_UNNECESSARY_TRANSMUTES, allow(unnecessary_transmutes))] > diff --git a/rust/kernel/net/phy.rs b/rust/kernel/net/phy.rs > index 32ea43ece646..65ac4d59ad77 100644 > --- a/rust/kernel/net/phy.rs > +++ b/rust/kernel/net/phy.rs > @@ -142,7 +142,7 @@ pub fn is_autoneg_enabled(&self) -> bool { > // SAFETY: The struct invariant ensures that we may access > // this field without additional synchronization. > let bit_field = unsafe { &(*self.0.get())._bitfield_1 }; > - bit_field.get(13, 1) == bindings::AUTONEG_ENABLE as u64 > + bit_field.get(13, 1) == u64::from(bindings::AUTONEG_ENABLE) > } > > /// Gets the current auto-negotiation state. > @@ -427,7 +427,7 @@ impl Adapter { > // where we hold `phy_device->lock`, so the accessors on > // `Device` are okay to call. > let dev = unsafe { Device::from_raw(phydev) }; > - T::match_phy_device(dev) as i32 > + T::match_phy_device(dev).into() > } > > /// # Safety > diff --git a/rust/uapi/lib.rs b/rust/uapi/lib.rs > index e79a1f49f055..08e68ebef606 100644 > --- a/rust/uapi/lib.rs > +++ b/rust/uapi/lib.rs > @@ -14,6 +14,7 @@ > #![cfg_attr(test, allow(unsafe_op_in_unsafe_fn))] > #![allow( > clippy::all, > + clippy::cast_lossless, > clippy::ptr_as_ptr, > clippy::undocumented_unsafe_blocks, > dead_code, >