From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id D0BE2C7115A for ; Sun, 15 Jun 2025 20:55:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D01508D0005; Sun, 15 Jun 2025 16:55:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CB0588D0001; Sun, 15 Jun 2025 16:55:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B51218D0005; Sun, 15 Jun 2025 16:55:27 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 9DC508D0001 for ; Sun, 15 Jun 2025 16:55:27 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 741CD10062C for ; Sun, 15 Jun 2025 20:55:27 +0000 (UTC) X-FDA: 83558840694.18.47A3BAF Received: from mail-qv1-f52.google.com (mail-qv1-f52.google.com [209.85.219.52]) by imf28.hostedemail.com (Postfix) with ESMTP id 9D94FC0004 for ; Sun, 15 Jun 2025 20:55:25 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Y+3NyHA1; spf=pass (imf28.hostedemail.com: domain of tamird@gmail.com designates 209.85.219.52 as permitted sender) smtp.mailfrom=tamird@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1750020925; h=from:from:sender: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:dkim-signature; bh=eOVACA1eFI8MiX8ijFRfc0eSr1nhHyzNP288zOTRdlY=; b=iHGc7xOFnIlUOjYSm+T8f7YLiAxBksymXNFkX2I6ViEZWPgguijyBQs9p/F9wQ7Hl4Rrm8 PMxMRxACkoyLnXsO88Av7Qwu5QxRLG5TVqlvEO+beKFnCCJnQFrL+xE909ok/idtVhV+Sq Vf8eXYvfFCdSjM7nMSGmyZjhnLNSm+E= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1750020925; a=rsa-sha256; cv=none; b=Shs5eNcz15Vb8muxlPvBm7sTeMvLu5TqcJ8hCMwVUnxY1lDHRxdo8Ai4ytOY8pKE1/T3O1 46B/LnTiHfP6R34GLlD6RZUElLNptSVeeEpSXFNiCDyhLJyLt5DFcghi8ssBISWgDfC/4+ XYsdoVsPL8D7ZdFPa6epjxMUITXBuZI= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Y+3NyHA1; spf=pass (imf28.hostedemail.com: domain of tamird@gmail.com designates 209.85.219.52 as permitted sender) smtp.mailfrom=tamird@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-qv1-f52.google.com with SMTP id 6a1803df08f44-6fb3bba0730so36735096d6.0 for ; Sun, 15 Jun 2025 13:55:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1750020924; x=1750625724; darn=kvack.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=eOVACA1eFI8MiX8ijFRfc0eSr1nhHyzNP288zOTRdlY=; b=Y+3NyHA1yzHQ3Y7h74zcAoypX1rVdPNVM8IQuJYdNDBzDE2X7VnGa3876cxll4MwQP 3kU4N3H/JVn+/y0VBRdvd0PnAe3mOWDOnS6f6lGHgrUBcpmhwOBXIHXOR2EV/+vjxZnO Hfen4qKuCGNHsHDL9M/lKzlz6+3i745+2AEGdV+mixmVHESQ0har8Oj8VyE5QZ99VWDM YytGTNqVBrPJEjyGsRpT2HWkGZJchnH5lGLEVO+hCGVNTfa4C9QKi0kHB3tkxQJtnVFg 2xrB9KBPviXhxSLafNk/B4BjFp++Zclk8PZXbzEEuFE65bjF8azzZo3uxiSbpyJXEtXS NDgA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1750020924; x=1750625724; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=eOVACA1eFI8MiX8ijFRfc0eSr1nhHyzNP288zOTRdlY=; b=dvLpvw04p+rl0623iHL2Y7sPiYafOHrYGBrf5weSeBRhRkotVXmZGXdgkgtwagnwMQ NLQN5mzlrUFuu7ELQ3YYjoUIA/ctm2X+5Aylfsl+acWatdPe64Z7GfcKX5SC8x8J08yl ZGzhjNxn+A/prULiOWMmQPVEYoLdhSiQE7lFa4DygpwMF//UkfMgC3xh9P1zbM/4Bq8h 20BXST26Hl+Spic1rNes6HMk0NhTXM40ba6adDYW+cTtooRWyZhJ1DfMtDjZ47QP+5rr 6Q6x626E9YncBikkbsogRn79yey6y4QUsS6inMJYjqCJX8FrmxzKlTaebTsB5QYSkKxS n05w== X-Forwarded-Encrypted: i=1; AJvYcCUApJ7XUBxZXH1dxRDxuEaeI/yjVaWeylZvuJ4UFdWOT3cpXwFcdBQhrrXVibUXrJvDs6usXiKp5w==@kvack.org X-Gm-Message-State: AOJu0YwlsOF6/y2GtKFEhAzhEz6SzX1uCHak7hSuzKB74ZMJ7YNX3Fpq QitkOa+dXIuwco+2ShvHAaXkb6kuGWQidLatTCAJodxsnrD8urjZBooFEB2G1bbj X-Gm-Gg: ASbGncu0CrGEjKimuR4sUA4F8qR+pl01rAQ6YGFvxn7ihhGJrDOMtFovcyEXjDCPi9P emnnfdCsByPoLZf/dAPuZzOSE9Gwvv7M9JrTWADKf5X3pJfCdzoLyxsH0KJKL+fj5KEvhdPFiNf MKOWhLvYJUkXjUDbz4BbwFiNLiQDjcc9/wcWMMPN4oEo4qk5A7yPkkz4YFGAW0zADt82PXEwex8 YjExncHgv2pizo4KFHbY28xr4qj/+m6JF8d/TspPLd6wWKP6mAc2yVEnQkB/Y9847RTKbFEshLT 5g7e9TSmyCOKmBYP7JCFLD5IZdOj8PfUutzMYszMXPip1bV4JRClqJNmrU2/YKMwQQOqHA== X-Google-Smtp-Source: AGHT+IF3hh4jupFwZOSWdFP81Ho1hBbetMpEgJhDQJSGwSq3vyb7VWe0EmAuiWSZP0mzNWtopZ28zA== X-Received: by 2002:a05:6214:2264:b0:6fa:a428:8b1e with SMTP id 6a1803df08f44-6fb46e1a667mr121430216d6.22.1750020923745; Sun, 15 Jun 2025 13:55:23 -0700 (PDT) Received: from tamird-mac.local ([2600:4041:5be7:7c00:b0c6:92db:d807:21a6]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6fb35c84148sm43468176d6.109.2025.06.15.13.55.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 15 Jun 2025 13:55:23 -0700 (PDT) From: Tamir Duberstein Date: Sun, 15 Jun 2025 16:55:10 -0400 Subject: [PATCH v12 6/6] rust: enable `clippy::ref_as_ptr` lint MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20250615-ptr-as-ptr-v12-6-f43b024581e8@gmail.com> References: <20250615-ptr-as-ptr-v12-0-f43b024581e8@gmail.com> In-Reply-To: <20250615-ptr-as-ptr-v12-0-f43b024581e8@gmail.com> To: 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 , Nicolas Schier 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, Tamir Duberstein , Benno Lossin X-Mailer: b4 0.15-dev X-Rspam-User: X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 9D94FC0004 X-Stat-Signature: hrumpa85hcknhg5h5rhxdi4eiz6x9e9j X-HE-Tag: 1750020925-231188 X-HE-Meta: U2FsdGVkX1+qOLSIR8Mz2eDcbyf5PcHibmNtTA7hhEmVRooom7OSeWnQS5dCq9KNXkaIB2wysdd2QCBrf28YHTnZElji/0+hQFQ4p6lbNPMs/hzJ+Pm3sbr1c+VBeocKfEurpvgFpmkzXzJ2+WxS1ueUudDpWl/kjpgPxeHU9cIF752CCakdOIfonCp7EynjNOM3Uz4PBIBAu4ny48bjXuilCAaL6/iDMSv2wDR9ocPZrQxxjRqRfmUtkgYCeKGquh/wh+NGZwNkKS6nlwoeBEuQj/E5+d90wU649d15iUdn2jYVzWmCZWHjiIFneJsC8BEW99TdyC0PbIWrMaJM3yzQdXoGvF4aLUQ54GvXwM2EFcoOiDLzSZnLZ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0LikUwZsGqFWnodvIp5D/NrkzMP6AiB8+5hA6OT+EJ2vaOtPH49uSQOEMQ9Ru27KsRCiLER2SNIYL7hqV6BGALFuLi3RDojVQ7ZiYxT8M7uwQv7MfBeqeJ81eA41zG5ENtyvkXA== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: In Rust 1.78.0, Clippy introduced the `ref_as_ptr` lint [1]: > Using `as` casts may result in silently changing mutability or type. 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#ref_as_ptr [1] Suggested-by: Benno Lossin Link: https://lore.kernel.org/all/D8PGG7NTWB6U.3SS3A5LN4XWMN@proton.me/ Reviewed-by: Benno Lossin Reviewed-by: Boqun Feng Signed-off-by: Tamir Duberstein --- Makefile | 1 + rust/bindings/lib.rs | 1 + rust/kernel/configfs.rs | 20 ++++++-------------- rust/kernel/device_id.rs | 2 +- rust/kernel/fs/file.rs | 2 +- rust/kernel/str.rs | 4 ++-- rust/kernel/uaccess.rs | 4 ++-- rust/uapi/lib.rs | 1 + 8 files changed, 15 insertions(+), 20 deletions(-) diff --git a/Makefile b/Makefile index 29cf39be14de..1a855f42a34a 100644 --- a/Makefile +++ b/Makefile @@ -489,6 +489,7 @@ export rust_common_flags := --edition=2021 \ -Wclippy::no_mangle_with_rust_abi \ -Wclippy::ptr_as_ptr \ -Wclippy::ptr_cast_constness \ + -Wclippy::ref_as_ptr \ -Wclippy::undocumented_unsafe_blocks \ -Wclippy::unnecessary_safety_comment \ -Wclippy::unnecessary_safety_doc \ diff --git a/rust/bindings/lib.rs b/rust/bindings/lib.rs index 7631c9f6708d..474cc98c48a3 100644 --- a/rust/bindings/lib.rs +++ b/rust/bindings/lib.rs @@ -27,6 +27,7 @@ #[allow(dead_code)] #[allow(clippy::cast_lossless)] #[allow(clippy::ptr_as_ptr)] +#[allow(clippy::ref_as_ptr)] #[allow(clippy::undocumented_unsafe_blocks)] #[cfg_attr(CONFIG_RUSTC_HAS_UNNECESSARY_TRANSMUTES, allow(unnecessary_transmutes))] mod bindings_raw { diff --git a/rust/kernel/configfs.rs b/rust/kernel/configfs.rs index bc8e15dcec18..1ddac786bd0d 100644 --- a/rust/kernel/configfs.rs +++ b/rust/kernel/configfs.rs @@ -426,7 +426,7 @@ impl GroupOperationsVTable }; const fn vtable_ptr() -> *const bindings::configfs_group_operations { - &Self::VTABLE as *const bindings::configfs_group_operations + &Self::VTABLE } } @@ -464,7 +464,7 @@ impl ItemOperationsVTable, Data> }; const fn vtable_ptr() -> *const bindings::configfs_item_operations { - &Self::VTABLE as *const bindings::configfs_item_operations + &Self::VTABLE } } @@ -476,7 +476,7 @@ impl ItemOperationsVTable, Data> { }; const fn vtable_ptr() -> *const bindings::configfs_item_operations { - &Self::VTABLE as *const bindings::configfs_item_operations + &Self::VTABLE } } @@ -717,11 +717,7 @@ impl AttributeList { // SAFETY: By function safety requirements, we have exclusive access to // `self` and the reference created below will be exclusive. - unsafe { - (&mut *self.0.get())[I] = (attribute as *const Attribute) - .cast_mut() - .cast() - }; + unsafe { (&mut *self.0.get())[I] = core::ptr::from_ref(attribute).cast_mut().cast() }; } } @@ -761,9 +757,7 @@ pub const fn new_with_child_ctor( ct_owner: owner.as_ptr(), ct_group_ops: GroupOperationsVTable::::vtable_ptr().cast_mut(), ct_item_ops: ItemOperationsVTable::<$tpe, Data>::vtable_ptr().cast_mut(), - ct_attrs: (attributes as *const AttributeList) - .cast_mut() - .cast(), + ct_attrs: core::ptr::from_ref(attributes).cast_mut().cast(), ct_bin_attrs: core::ptr::null_mut(), }), _p: PhantomData, @@ -780,9 +774,7 @@ pub const fn new( ct_owner: owner.as_ptr(), ct_group_ops: core::ptr::null_mut(), ct_item_ops: ItemOperationsVTable::<$tpe, Data>::vtable_ptr().cast_mut(), - ct_attrs: (attributes as *const AttributeList) - .cast_mut() - .cast(), + ct_attrs: core::ptr::from_ref(attributes).cast_mut().cast(), ct_bin_attrs: core::ptr::null_mut(), }), _p: PhantomData, diff --git a/rust/kernel/device_id.rs b/rust/kernel/device_id.rs index f9d55ac7b9e6..3dc72ca8cfc2 100644 --- a/rust/kernel/device_id.rs +++ b/rust/kernel/device_id.rs @@ -136,7 +136,7 @@ impl IdTable for IdArray { fn as_ptr(&self) -> *const T::RawType { // This cannot be `self.ids.as_ptr()`, as the return pointer must have correct provenance // to access the sentinel. - (self as *const Self).cast() + core::ptr::from_ref(self).cast() } fn id(&self, index: usize) -> &T::RawType { diff --git a/rust/kernel/fs/file.rs b/rust/kernel/fs/file.rs index e9bfbad00755..35fd5db35c46 100644 --- a/rust/kernel/fs/file.rs +++ b/rust/kernel/fs/file.rs @@ -366,7 +366,7 @@ fn deref(&self) -> &LocalFile { // // By the type invariants, there are no `fdget_pos` calls that did not take the // `f_pos_lock` mutex. - unsafe { LocalFile::from_raw_file((self as *const Self).cast()) } + unsafe { LocalFile::from_raw_file(core::ptr::from_ref(self).cast()) } } } diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs index 43597eb7c5c1..cbc8b459ed41 100644 --- a/rust/kernel/str.rs +++ b/rust/kernel/str.rs @@ -29,7 +29,7 @@ pub const fn is_empty(&self) -> bool { #[inline] pub const fn from_bytes(bytes: &[u8]) -> &Self { // SAFETY: `BStr` is transparent to `[u8]`. - unsafe { &*(bytes as *const [u8] as *const BStr) } + unsafe { &*(core::ptr::from_ref(bytes) as *const BStr) } } /// Strip a prefix from `self`. Delegates to [`slice::strip_prefix`]. @@ -290,7 +290,7 @@ pub const fn from_bytes_with_nul(bytes: &[u8]) -> Result<&Self, CStrConvertError #[inline] pub unsafe fn from_bytes_with_nul_unchecked_mut(bytes: &mut [u8]) -> &mut CStr { // SAFETY: Properties of `bytes` guaranteed by the safety precondition. - unsafe { &mut *(bytes as *mut [u8] as *mut CStr) } + unsafe { &mut *(core::ptr::from_mut(bytes) as *mut CStr) } } /// Returns a C pointer to the string. diff --git a/rust/kernel/uaccess.rs b/rust/kernel/uaccess.rs index 6d70edd8086a..4ef13cf13a78 100644 --- a/rust/kernel/uaccess.rs +++ b/rust/kernel/uaccess.rs @@ -240,7 +240,7 @@ pub fn read_raw(&mut self, out: &mut [MaybeUninit]) -> Result { pub fn read_slice(&mut self, out: &mut [u8]) -> Result { // SAFETY: The types are compatible and `read_raw` doesn't write uninitialized bytes to // `out`. - let out = unsafe { &mut *(out as *mut [u8] as *mut [MaybeUninit]) }; + let out = unsafe { &mut *(core::ptr::from_mut(out) as *mut [MaybeUninit]) }; self.read_raw(out) } @@ -355,7 +355,7 @@ pub fn write(&mut self, value: &T) -> Result { let res = unsafe { bindings::_copy_to_user( self.ptr as *mut c_void, - (value as *const T).cast::(), + core::ptr::from_ref(value).cast::(), len, ) }; diff --git a/rust/uapi/lib.rs b/rust/uapi/lib.rs index 08e68ebef606..31c2f713313f 100644 --- a/rust/uapi/lib.rs +++ b/rust/uapi/lib.rs @@ -16,6 +16,7 @@ clippy::all, clippy::cast_lossless, clippy::ptr_as_ptr, + clippy::ref_as_ptr, clippy::undocumented_unsafe_blocks, dead_code, missing_docs, -- 2.49.0