From nobody Mon Dec 15 21:26:25 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 1B60014D703; Wed, 4 Sep 2024 20:44:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725482686; cv=none; b=hQQqj9jwA6bHNUcgSQ6CjLjxRwhzHn0+4IU4I0ek5TPWkttyZXtmeXvDeTGbmtoH6KWdBs2UFhp7FDMLBLLC1S0/IpbFk9zWJ74rbsUwbjKT9QB0UYXoXoX0sw9Imjw77wQG2d8wGDF0fgH1sDYSrBW87DsMv1OzFdacT61uPy8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725482686; c=relaxed/simple; bh=vANsDtYmWGHFZ2IuDU/7W7TVVPKtroKugzofcbnxEJM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=T9xCHvSACK9RvOmBZYNukLEH5FFN5Gk2yPFqM1AP8wi17TXxgjzRCt1q4RGkvEgN8/90Yrj4j8yufFXTdbggemRQnIclZruGx1U655ij3IqqI9acpmiMizX7F1kCGlA1KUdB5wyQINA6T3FxKx1iq2xK8q5b7VWhWv1wDrQgV1w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=tIcs2NOt; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="tIcs2NOt" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7A894C4CEC5; Wed, 4 Sep 2024 20:44:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1725482685; bh=vANsDtYmWGHFZ2IuDU/7W7TVVPKtroKugzofcbnxEJM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=tIcs2NOtwaR0voinJjko/gjpXM3MWLUkOQZVv0Okm+AiSP5VwW+1FEEAUaEJ6fHkX wSGa/X2xNI4bprCWqmf5gpPuTuy1Rjb62mxRHLNdbHu5bThGlZFjvsaH6xvEpeBero RDoYnkYKvLgdwcRgf15VAzFAhshs4OkU0qRpnVfQKjC01L/+HDh4r3USoA3dC/6kTE AvU9BvQuYdZ8+QuR5pgA38vb9BAxbYLjhb3ahSepvHQO7GofiviMFLOkiyhhA09Oi4 mNfMeFTREGbm2aptSeTthhmAuaPRP7O5VTPpw6iTlznHUAjDMFNl2qzxcsAh0WrSll XZcDFtEbXho+Q== From: Miguel Ojeda To: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho Cc: Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev Subject: [PATCH 01/19] rust: workqueue: remove unneeded ``#[allow(clippy::new_ret_no_self)]` Date: Wed, 4 Sep 2024 22:43:29 +0200 Message-ID: <20240904204347.168520-2-ojeda@kernel.org> In-Reply-To: <20240904204347.168520-1-ojeda@kernel.org> References: <20240904204347.168520-1-ojeda@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Perform the same clean commit b2516f7af9d2 ("rust: kernel: remove `#[allow(clippy::new_ret_no_self)]`") did for a case that appeared in workqueue in parallel in commit 7324b88975c5 ("rust: workqueue: add helper for defining work_struct fields"): Clippy triggered a false positive on its `new_ret_no_self` lint when using the `pin_init!` macro. Since Rust 1.67.0, that does not happen anymore, since Clippy learnt to not warn about `-> impl Trait` [1][2]. The kernel nowadays uses Rust 1.72.1, thus remove the `#[allow]`. Link: https://github.com/rust-lang/rust-clippy/issues/7344 [1] Link: https://github.com/rust-lang/rust-clippy/pull/9733 [2] Signed-off-by: Miguel Ojeda Reviewed-by: Alice Ryhl Reviewed-by: Gary Guo Reviewed-by: Trevor Gross Tested-by: Gary Guo --- rust/kernel/workqueue.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/rust/kernel/workqueue.rs b/rust/kernel/workqueue.rs index 553a5cba2adc..493288dc1de0 100644 --- a/rust/kernel/workqueue.rs +++ b/rust/kernel/workqueue.rs @@ -366,7 +366,6 @@ unsafe impl Sync for Work {} impl Work { /// Creates a new instance of [`Work`]. #[inline] - #[allow(clippy::new_ret_no_self)] pub fn new(name: &'static CStr, key: &'static LockClassKey) -> impl Pi= nInit where T: WorkItem, --=20 2.46.0 From nobody Mon Dec 15 21:26:25 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 AF1E3155730; Wed, 4 Sep 2024 20:44:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725482689; cv=none; b=I+68vnro5Gs6GEvTcld7isdPjRgtV4oYEcbtLs7JIjr+NCxPPWRgP028eA42pb9WLEFncDUhOqhseb0zLQ91wkvYYNl5Gd3326yVvclDp0CJf+4i0MtyozXU3Dk7pofjXFyfAjDTIqaoLUmn1QU2283bjD5UONeF9UKz8aGEkNQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725482689; c=relaxed/simple; bh=n+H0WVK71AlS2DbgqqLYwugZDAwCnU45IJ9wYNEw5Y8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=A3ubY5AgmlNtD5uql80dX25vNzARGYo3fR7UDObZyL2S5+WKYTqHTY3DyvE+R1XKmwp04NcPHysoD5a8/KOwtxeAkhdKfl9VFZAJCZSBp6zrotARx7WUJDpOE24kJX5aLoX/R5ONIT2Q9LHHKNe9VTZz2XECXTg7sERYdmJ+89Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=E/mxmall; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="E/mxmall" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 24C7FC4CEC8; Wed, 4 Sep 2024 20:44:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1725482689; bh=n+H0WVK71AlS2DbgqqLYwugZDAwCnU45IJ9wYNEw5Y8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=E/mxmall0XlIt8dpA7ujiYjHOS1YypBu7tqQJRR0h55ErzVgoHST/P+vD40wLhL8U Z+5/D0uI7SeR36G2pzypU4qOoXz4woAs+/eQXu75PoIXduQnTO0rh0sRb3S5sVQ68Y Nz5V1waC5/vofwTxZFiI3q1ZdgviOgc8eGbOpHXwQTasVuAcwcj3SB62bFze42eLX4 1RjaTzN6xCrH/WbJDhuoX7wSZZEx9FXq2pwJSLnYIpivX/h52+C4+GsfBwfM2k7hQ/ FUxLNFnJmyRYkhgDCIbD2Zhd+/Z/4rLhUNR29I20HVialbuvf0CR+ygZytMJv0at5O QlO7EI67crlNw== From: Miguel Ojeda To: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho Cc: Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev Subject: [PATCH 02/19] rust: sort global Rust flags Date: Wed, 4 Sep 2024 22:43:30 +0200 Message-ID: <20240904204347.168520-3-ojeda@kernel.org> In-Reply-To: <20240904204347.168520-1-ojeda@kernel.org> References: <20240904204347.168520-1-ojeda@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Sort the global Rust flags so that it is easier to follow along when we have more, like this patch series does. Signed-off-by: Miguel Ojeda Reviewed-by: Alice Ryhl Reviewed-by: Gary Guo Reviewed-by: Trevor Gross Tested-by: Gary Guo --- Makefile | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 68ebd6d6b444..7a97726f54f7 100644 --- a/Makefile +++ b/Makefile @@ -445,18 +445,18 @@ KBUILD_USERLDFLAGS :=3D $(USERLDFLAGS) # host programs. export rust_common_flags :=3D --edition=3D2021 \ -Zbinary_dep_depinfo=3Dy \ - -Dunsafe_op_in_unsafe_fn \ -Dnon_ascii_idents \ + -Dunsafe_op_in_unsafe_fn \ + -Wmissing_docs \ -Wrust_2018_idioms \ -Wunreachable_pub \ - -Wmissing_docs \ - -Wrustdoc::missing_crate_level_docs \ -Wclippy::all \ + -Wclippy::dbg_macro \ -Wclippy::mut_mut \ -Wclippy::needless_bitwise_bool \ -Wclippy::needless_continue \ -Wclippy::no_mangle_with_rust_abi \ - -Wclippy::dbg_macro + -Wrustdoc::missing_crate_level_docs =20 KBUILD_HOSTCFLAGS :=3D $(KBUILD_USERHOSTCFLAGS) $(HOST_LFS_CFLAGS) \ $(HOSTCFLAGS) -I $(srctree)/scripts/include --=20 2.46.0 From nobody Mon Dec 15 21:26:25 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 5041416F826; Wed, 4 Sep 2024 20:44:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725482693; cv=none; b=ViiEMDOaDmLrJ/KM+oKPc9QRIWiymGflP8cyJOXOQ46JW2S8FLxpZ7H53yyjUHqms23V+mgc+Wc1Yjys08BJXlvf5HYu8n0be4dUT+bnSaLriOI64gTYm6+ChmG6OP8Rsk5rzRhkdA8Yr84Pbg3k2sLN6g/GDPEBkhUyJ/wKaQw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725482693; c=relaxed/simple; bh=BDCJg1BW/c/WNJP3GkIb+wNmN8ALfi+qN1JhvFLZ3EE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=QazBtgOR/cLUWqttxkNkt8B36R3os/bRVhI1oDjMN4Oxfht1bsvc732O5JiQrjlitddDiCDYinoEuxM/cEPBuphYSr/jX1SXA1xpcF/YiLxkw6flaQZgujBAMEOg3ZD6ejYOAAtk82IMujEE+RRExxqsCk/IwWkIMnvBmGTbz7k= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=HhkfSfof; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="HhkfSfof" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C3590C4CEC2; Wed, 4 Sep 2024 20:44:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1725482692; bh=BDCJg1BW/c/WNJP3GkIb+wNmN8ALfi+qN1JhvFLZ3EE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HhkfSfofGKkQGfCIuBvNAxqs+wXC0cnrvgli+VSLQICP+daaSeemNQvOvAZsuhKYC cGe47SWz3SVGkVVIlUoKHdyHg2ynfsKRXhQCP4ydBdLqgfAnRZNXZlgRhgHjtCILPM 4z7vkqbOwIzWVOdSvgKC7SLPuaNH5uUBjeTlMjYL2QquinvEYxtPtlxfDbW5gwRTLS sjysBP1Y8z7Vo6IdIE4BGHBex5aTNB93hKAUL0eUVge50+ZWg5JZH+keGmb8V/RRgL Aw8Fj92o7JM7+4xNLpXz+rMVwCk65AZqWZbd2MGsr8aQWczWgsk8W4YvVGUNMHeihJ zHAPhVwuXlMgQ== From: Miguel Ojeda To: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho Cc: Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev Subject: [PATCH 03/19] rust: types: avoid repetition in `{As,From}Bytes` impls Date: Wed, 4 Sep 2024 22:43:31 +0200 Message-ID: <20240904204347.168520-4-ojeda@kernel.org> In-Reply-To: <20240904204347.168520-1-ojeda@kernel.org> References: <20240904204347.168520-1-ojeda@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In order to provide `// SAFETY` comments for every `unsafe impl`, we would need to repeat them, which is not very useful and would be harder to read. We could perhaps allow the lint (ideally within a small module), but we can take the chance to avoid the repetition of the `impl`s themselves too by using a small local macro, like in other places where we have had to do this sort of thing. Thus add the straightforward `impl_{from,as}bytes!` macros and use them to implement `FromBytes`. This, in turn, will allow us in the next patch to place a `// SAFETY` comment that defers to the actual invocation of the macro. Signed-off-by: Miguel Ojeda Reviewed-by: Alice Ryhl Reviewed-by: Gary Guo Reviewed-by: Trevor Gross Tested-by: Gary Guo --- rust/kernel/types.rs | 68 +++++++++++++++++++++++--------------------- 1 file changed, 35 insertions(+), 33 deletions(-) diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs index 9e7ca066355c..70e173f15d87 100644 --- a/rust/kernel/types.rs +++ b/rust/kernel/types.rs @@ -481,21 +481,22 @@ pub enum Either { /// All bit-patterns must be valid for this type. This type must not have = interior mutability. pub unsafe trait FromBytes {} =20 -// SAFETY: All bit patterns are acceptable values of the types below. -unsafe impl FromBytes for u8 {} -unsafe impl FromBytes for u16 {} -unsafe impl FromBytes for u32 {} -unsafe impl FromBytes for u64 {} -unsafe impl FromBytes for usize {} -unsafe impl FromBytes for i8 {} -unsafe impl FromBytes for i16 {} -unsafe impl FromBytes for i32 {} -unsafe impl FromBytes for i64 {} -unsafe impl FromBytes for isize {} -// SAFETY: If all bit patterns are acceptable for individual values in an = array, then all bit -// patterns are also acceptable for arrays of that type. -unsafe impl FromBytes for [T] {} -unsafe impl FromBytes for [T; N] {} +macro_rules! impl_frombytes { + ($($({$($generics:tt)*})? $t:ty, )*) =3D> { + $(unsafe impl$($($generics)*)? FromBytes for $t {})* + }; +} + +impl_frombytes! { + // SAFETY: All bit patterns are acceptable values of the types below. + u8, u16, u32, u64, usize, + i8, i16, i32, i64, isize, + + // SAFETY: If all bit patterns are acceptable for individual values in= an array, then all bit + // patterns are also acceptable for arrays of that type. + {} [T], + {} [T; N], +} =20 /// Types that can be viewed as an immutable slice of initialized bytes. /// @@ -514,21 +515,22 @@ unsafe impl FromBytes for [T] {} /// mutability. pub unsafe trait AsBytes {} =20 -// SAFETY: Instances of the following types have no uninitialized portions. -unsafe impl AsBytes for u8 {} -unsafe impl AsBytes for u16 {} -unsafe impl AsBytes for u32 {} -unsafe impl AsBytes for u64 {} -unsafe impl AsBytes for usize {} -unsafe impl AsBytes for i8 {} -unsafe impl AsBytes for i16 {} -unsafe impl AsBytes for i32 {} -unsafe impl AsBytes for i64 {} -unsafe impl AsBytes for isize {} -unsafe impl AsBytes for bool {} -unsafe impl AsBytes for char {} -unsafe impl AsBytes for str {} -// SAFETY: If individual values in an array have no uninitialized portions= , then the array itself -// does not have any uninitialized portions either. -unsafe impl AsBytes for [T] {} -unsafe impl AsBytes for [T; N] {} +macro_rules! impl_asbytes { + ($($({$($generics:tt)*})? $t:ty, )*) =3D> { + $(unsafe impl$($($generics)*)? AsBytes for $t {})* + }; +} + +impl_asbytes! { + // SAFETY: Instances of the following types have no uninitialized port= ions. + u8, u16, u32, u64, usize, + i8, i16, i32, i64, isize, + bool, + char, + str, + + // SAFETY: If individual values in an array have no uninitialized port= ions, then the array + // itself does not have any uninitialized portions either. + {} [T], + {} [T; N], +} --=20 2.46.0 From nobody Mon Dec 15 21:26:25 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 2736C17ADFF; Wed, 4 Sep 2024 20:44:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725482697; cv=none; b=ZA9nanukErSY0SHc1ZN/ux55ReROrgXIrsGg8JONAjIOYKD/271xcDM3lFpFQ1pCFF2LGab22qk0yGDwnPLJtPT2wxJoukqWjB2ZbMEr0R9+iQfsjnMOGzB1fhIHq1ApRPMPKagWBGJO/a1IY0TfI5owkfAhVww5De83hH+BXpE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725482697; c=relaxed/simple; bh=uiIxh/wPBQkf3FPlv5jcM30CrOjR7oi7W4Ice0LSTMU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=C/Hw9vV416Ps35HQ1iaDKCj4nWuEOPMRrexk1vLYchkm+PrKd28GmFVnYpS3cXaOlzHTik4J/cNGEY42FXXt14l9kwMHdAt10HJymEVM3RAzCDR4jD0NGs4eVdCpoPUwWroENwxXt7Dnn9VEcFVf+Hgzw69ru/Hz/pq9EcB4ahI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=eXJCmKMs; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="eXJCmKMs" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 71487C4CEC5; Wed, 4 Sep 2024 20:44:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1725482696; bh=uiIxh/wPBQkf3FPlv5jcM30CrOjR7oi7W4Ice0LSTMU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=eXJCmKMsbSUo0y0dtd66qYorIbYFOFm1eqc6tEWIU+JUc+hLxfkF5DiXk++8tBTrR YZwXJSzi185/z+4Ax5W6LXR8ZT4qwpNRSUX6dQBa2BivjzznGf309Ka1sT0PmW0bB7 fYgjt1sQOIgeMxlq5Mq/eOiNm32kr2c7BU7I38Vk/s5jf4jSAuD8eHqHtf80xvIj6I FZfu01wcGEpuIlC8ddEBGQ/+eTFuyqQH9sng7Ai5ezXnIJdRxLoVNde8ql4fwWtSxR 2DKkoin/M5letE6FwitIQefa4ieq1ZDlgrdy2glDpBYf07BLj9ceZ1efU+ZLwqKH+1 HXLhVwjcQka3w== From: Miguel Ojeda To: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho Cc: Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev Subject: [PATCH 04/19] rust: enable `clippy::undocumented_unsafe_blocks` lint Date: Wed, 4 Sep 2024 22:43:32 +0200 Message-ID: <20240904204347.168520-5-ojeda@kernel.org> In-Reply-To: <20240904204347.168520-1-ojeda@kernel.org> References: <20240904204347.168520-1-ojeda@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Checking that we are not missing any `// SAFETY` comments in our `unsafe` blocks is something we have wanted to do for a long time, as well as cleaning up the remaining cases that were not documented [1]. Back when Rust for Linux started, this was something that could have been done via a script, like Rust's `tidy`. Soon after, in Rust 1.58.0, Clippy implemented the `undocumented_unsafe_blocks` lint [2]. Even though the lint has a few false positives, e.g. in some cases where attributes appear between the comment and the `unsafe` block [3], there are workarounds and the lint seems quite usable already. Thus enable the lint now. We still have a few cases to clean up, so just allow those for the moment by writing a `TODO` comment -- some of those may be good candidates for new contributors. Link: https://github.com/Rust-for-Linux/linux/issues/351 [1] Link: https://rust-lang.github.io/rust-clippy/master/#/undocumented_unsafe_= blocks [2] Link: https://github.com/rust-lang/rust-clippy/issues/13189 [3] Signed-off-by: Miguel Ojeda Reviewed-by: Alice Ryhl Reviewed-by: Gary Guo Reviewed-by: Trevor Gross Tested-by: Gary Guo --- Makefile | 1 + rust/bindings/lib.rs | 1 + rust/kernel/alloc/allocator.rs | 2 ++ rust/kernel/error.rs | 9 ++++++--- rust/kernel/init.rs | 5 +++++ rust/kernel/init/__internal.rs | 2 ++ rust/kernel/init/macros.rs | 9 +++++++++ rust/kernel/list.rs | 1 + rust/kernel/print.rs | 2 ++ rust/kernel/str.rs | 7 ++++--- rust/kernel/sync/condvar.rs | 2 +- rust/kernel/sync/lock.rs | 6 +++--- rust/kernel/types.rs | 4 ++++ rust/kernel/workqueue.rs | 4 ++++ rust/uapi/lib.rs | 1 + 15 files changed, 46 insertions(+), 10 deletions(-) diff --git a/Makefile b/Makefile index 7a97726f54f7..c7a4f313728d 100644 --- a/Makefile +++ b/Makefile @@ -456,6 +456,7 @@ export rust_common_flags :=3D --edition=3D2021 \ -Wclippy::needless_bitwise_bool \ -Wclippy::needless_continue \ -Wclippy::no_mangle_with_rust_abi \ + -Wclippy::undocumented_unsafe_blocks \ -Wrustdoc::missing_crate_level_docs =20 KBUILD_HOSTCFLAGS :=3D $(KBUILD_USERHOSTCFLAGS) $(HOST_LFS_CFLAGS) \ diff --git a/rust/bindings/lib.rs b/rust/bindings/lib.rs index 93a1a3fc97bc..d6da3011281a 100644 --- a/rust/bindings/lib.rs +++ b/rust/bindings/lib.rs @@ -25,6 +25,7 @@ )] =20 #[allow(dead_code)] +#[allow(clippy::undocumented_unsafe_blocks)] mod bindings_raw { // Use glob import here to expose all helpers. // Symbols defined within the module will take precedence to the glob = import. diff --git a/rust/kernel/alloc/allocator.rs b/rust/kernel/alloc/allocator.rs index e6ea601f38c6..91216b36af69 100644 --- a/rust/kernel/alloc/allocator.rs +++ b/rust/kernel/alloc/allocator.rs @@ -31,6 +31,7 @@ pub(crate) unsafe fn krealloc_aligned(ptr: *mut u8, new_l= ayout: Layout, flags: F unsafe { bindings::krealloc(ptr as *const core::ffi::c_void, size, fla= gs.0) as *mut u8 } } =20 +// SAFETY: TODO. unsafe impl GlobalAlloc for KernelAllocator { unsafe fn alloc(&self, layout: Layout) -> *mut u8 { // SAFETY: `ptr::null_mut()` is null and `layout` has a non-zero s= ize by the function safety @@ -39,6 +40,7 @@ unsafe fn alloc(&self, layout: Layout) -> *mut u8 { } =20 unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) { + // SAFETY: TODO. unsafe { bindings::kfree(ptr as *const core::ffi::c_void); } diff --git a/rust/kernel/error.rs b/rust/kernel/error.rs index 6f1587a2524e..639bc7572f90 100644 --- a/rust/kernel/error.rs +++ b/rust/kernel/error.rs @@ -171,9 +171,11 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Resu= lt { match self.name() { // Print out number if no name can be found. None =3D> f.debug_tuple("Error").field(&-self.0).finish(), - // SAFETY: These strings are ASCII-only. Some(name) =3D> f - .debug_tuple(unsafe { core::str::from_utf8_unchecked(name)= }) + .debug_tuple( + // SAFETY: These strings are ASCII-only. + unsafe { core::str::from_utf8_unchecked(name) }, + ) .finish(), } } @@ -277,6 +279,8 @@ pub(crate) fn from_err_ptr(ptr: *mut T) -> Result<*m= ut T> { if unsafe { bindings::IS_ERR(const_ptr) } { // SAFETY: The FFI function does not deref the pointer. let err =3D unsafe { bindings::PTR_ERR(const_ptr) }; + + #[allow(clippy::unnecessary_cast)] // CAST: If `IS_ERR()` returns `true`, // then `PTR_ERR()` is guaranteed to return a // negative value greater-or-equal to `-bindings::MAX_ERRNO`, @@ -286,7 +290,6 @@ pub(crate) fn from_err_ptr(ptr: *mut T) -> Result<*m= ut T> { // // SAFETY: `IS_ERR()` ensures `err` is a // negative value greater-or-equal to `-bindings::MAX_ERRNO`. - #[allow(clippy::unnecessary_cast)] return Err(unsafe { Error::from_errno_unchecked(err as core::ffi::= c_int) }); } Ok(ptr) diff --git a/rust/kernel/init.rs b/rust/kernel/init.rs index a17ac8762d8f..08b9d695c285 100644 --- a/rust/kernel/init.rs +++ b/rust/kernel/init.rs @@ -541,6 +541,7 @@ macro_rules! stack_try_pin_init { /// } /// pin_init!(&this in Buf { /// buf: [0; 64], +/// // SAFETY: TODO. /// ptr: unsafe { addr_of_mut!((*this.as_ptr()).buf).cast() }, /// pin: PhantomPinned, /// }); @@ -875,6 +876,7 @@ pub unsafe trait PinInit: = Sized { /// } /// /// let foo =3D pin_init!(Foo { + /// // SAFETY: TODO. /// raw <- unsafe { /// Opaque::ffi_init(|s| { /// init_foo(s); @@ -1162,6 +1164,7 @@ pub fn pin_init_array_from_fn( // SAFETY: Every type can be initialized by-value. unsafe impl Init for T { unsafe fn __init(self, slot: *mut T) -> Result<(), E> { + // SAFETY: TODO. unsafe { slot.write(self) }; Ok(()) } @@ -1170,6 +1173,7 @@ unsafe fn __init(self, slot: *mut T) -> Result<(), E>= { // SAFETY: Every type can be initialized by-value. `__pinned_init` calls `= __init`. unsafe impl PinInit for T { unsafe fn __pinned_init(self, slot: *mut T) -> Result<(), E> { + // SAFETY: TODO. unsafe { self.__init(slot) } } } @@ -1411,6 +1415,7 @@ pub fn zeroed() -> impl Init { =20 macro_rules! impl_zeroable { ($($({$($generics:tt)*})? $t:ty, )*) =3D> { + // SAFETY: Safety comments written in the macro invocation. $(unsafe impl$($($generics)*)? Zeroable for $t {})* }; } diff --git a/rust/kernel/init/__internal.rs b/rust/kernel/init/__internal.rs index 13cefd37512f..29f4fd00df3d 100644 --- a/rust/kernel/init/__internal.rs +++ b/rust/kernel/init/__internal.rs @@ -112,10 +112,12 @@ fn clone(&self) -> Self { =20 impl Copy for AllData {} =20 +// SAFETY: TODO. unsafe impl InitData for AllData { type Datee =3D T; } =20 +// SAFETY: TODO. unsafe impl HasInitData for T { type InitData =3D AllData; =20 diff --git a/rust/kernel/init/macros.rs b/rust/kernel/init/macros.rs index 02ecedc4ae7a..93af8f3d8d4d 100644 --- a/rust/kernel/init/macros.rs +++ b/rust/kernel/init/macros.rs @@ -513,6 +513,7 @@ fn drop($($sig:tt)*) { } ), ) =3D> { + // SAFETY: TODO. unsafe $($impl_sig)* { // Inherit all attributes and the type/ident tokens for the si= gnature. $(#[$($attr)*])* @@ -872,6 +873,7 @@ unsafe fn __pin_data() -> Self::PinData { } } =20 + // SAFETY: TODO. unsafe impl<$($impl_generics)*> $crate::init::__internal::PinData for __ThePinData<$($ty_g= enerics)*> where $($whr)* @@ -997,6 +999,7 @@ impl<$($impl_generics)*> $pin_data<$($ty_generics)*> slot: *mut $p_type, init: impl $crate::init::PinInit<$p_type, E>, ) -> ::core::result::Result<(), E> { + // SAFETY: TODO. unsafe { $crate::init::PinInit::__pinned_init(init, sl= ot) } } )* @@ -1007,6 +1010,7 @@ impl<$($impl_generics)*> $pin_data<$($ty_generics)*> slot: *mut $type, init: impl $crate::init::Init<$type, E>, ) -> ::core::result::Result<(), E> { + // SAFETY: TODO. unsafe { $crate::init::Init::__init(init, slot) } } )* @@ -1121,6 +1125,8 @@ macro_rules! __init_internal { // no possibility of returning without `unsafe`. struct __InitOk; // Get the data about fields from the supplied type. + // + // SAFETY: TODO. let data =3D unsafe { use $crate::init::__internal::$has_data; // Here we abuse `paste!` to retokenize `$t`. Declarative macr= os have some internal @@ -1176,6 +1182,7 @@ fn assert_zeroable(_: *mut= T) {} let init =3D move |slot| -> ::core::result::Result<(), $err> { init(slot).map(|__InitOk| ()) }; + // SAFETY: TODO. let init =3D unsafe { $crate::init::$construct_closure::<_, $err>(= init) }; init }}; @@ -1324,6 +1331,8 @@ fn assert_zeroable(_: *mut= T) {} // Endpoint, nothing more to munch, create the initializer. // Since we are in the closure that is never called, this will nev= er get executed. // We abuse `slot` to get the correct type inference here: + // + // SAFETY: TODO. unsafe { // Here we abuse `paste!` to retokenize `$t`. Declarative macr= os have some internal // information that is associated to already parsed fragments,= so a path fragment diff --git a/rust/kernel/list.rs b/rust/kernel/list.rs index 5b4aec29eb67..fb93330f4af4 100644 --- a/rust/kernel/list.rs +++ b/rust/kernel/list.rs @@ -354,6 +354,7 @@ pub fn pop_front(&mut self) -> Option> { /// /// `item` must not be in a different linked list (with the same id). pub unsafe fn remove(&mut self, item: &T) -> Option> { + // SAFETY: TODO. let mut item =3D unsafe { ListLinks::fields(T::view_links(item)) }; // SAFETY: The user provided a reference, and reference are never = dangling. // diff --git a/rust/kernel/print.rs b/rust/kernel/print.rs index 508b0221256c..fe53fc469c4f 100644 --- a/rust/kernel/print.rs +++ b/rust/kernel/print.rs @@ -23,6 +23,7 @@ use fmt::Write; // SAFETY: The C contract guarantees that `buf` is valid if it's less = than `end`. let mut w =3D unsafe { RawFormatter::from_ptrs(buf.cast(), end.cast())= }; + // SAFETY: TODO. let _ =3D w.write_fmt(unsafe { *(ptr as *const fmt::Arguments<'_>) }); w.pos().cast() } @@ -102,6 +103,7 @@ pub unsafe fn call_printk( ) { // `_printk` does not seem to fail in any path. #[cfg(CONFIG_PRINTK)] + // SAFETY: TODO. unsafe { bindings::_printk( format_string.as_ptr() as _, diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs index bb8d4f41475b..66d4527f6c6f 100644 --- a/rust/kernel/str.rs +++ b/rust/kernel/str.rs @@ -162,10 +162,10 @@ pub const fn len(&self) -> usize { /// Returns the length of this string with `NUL`. #[inline] pub const fn len_with_nul(&self) -> usize { - // SAFETY: This is one of the invariant of `CStr`. - // We add a `unreachable_unchecked` here to hint the optimizer that - // the value returned from this function is non-zero. if self.0.is_empty() { + // SAFETY: This is one of the invariant of `CStr`. + // We add a `unreachable_unchecked` here to hint the optimizer= that + // the value returned from this function is non-zero. unsafe { core::hint::unreachable_unchecked() }; } self.0.len() @@ -301,6 +301,7 @@ pub fn to_str(&self) -> Result<&str, core::str::Utf8Err= or> { /// ``` #[inline] pub unsafe fn as_str_unchecked(&self) -> &str { + // SAFETY: TODO. unsafe { core::str::from_utf8_unchecked(self.as_bytes()) } } =20 diff --git a/rust/kernel/sync/condvar.rs b/rust/kernel/sync/condvar.rs index 2b306afbe56d..7e00048bf4b1 100644 --- a/rust/kernel/sync/condvar.rs +++ b/rust/kernel/sync/condvar.rs @@ -92,8 +92,8 @@ pub struct CondVar { _pin: PhantomPinned, } =20 -// SAFETY: `CondVar` only uses a `struct wait_queue_head`, which is safe t= o use on any thread. #[allow(clippy::non_send_fields_in_send_ty)] +// SAFETY: `CondVar` only uses a `struct wait_queue_head`, which is safe t= o use on any thread. unsafe impl Send for CondVar {} =20 // SAFETY: `CondVar` only uses a `struct wait_queue_head`, which is safe t= o use on multiple threads diff --git a/rust/kernel/sync/lock.rs b/rust/kernel/sync/lock.rs index f6c34ca4d819..07fcf2d8efc6 100644 --- a/rust/kernel/sync/lock.rs +++ b/rust/kernel/sync/lock.rs @@ -150,9 +150,9 @@ pub(crate) fn do_unlocked(&mut self, cb: impl FnOnce= () -> U) -> U { // SAFETY: The caller owns the lock, so it is safe to unlock it. unsafe { B::unlock(self.lock.state.get(), &self.state) }; =20 - // SAFETY: The lock was just unlocked above and is being relocked = now. - let _relock =3D - ScopeGuard::new(|| unsafe { B::relock(self.lock.state.get(), &= mut self.state) }); + let _relock =3D ScopeGuard::new(|| + // SAFETY: The lock was just unlocked above and is being r= elocked now. + unsafe { B::relock(self.lock.state.get(), &mut self.state)= }); =20 cb() } diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs index 70e173f15d87..6c2d5fa9bce3 100644 --- a/rust/kernel/types.rs +++ b/rust/kernel/types.rs @@ -410,6 +410,7 @@ pub unsafe fn from_raw(ptr: NonNull) -> Self { /// /// struct Empty {} /// + /// # // SAFETY: TODO. /// unsafe impl AlwaysRefCounted for Empty { /// fn inc_ref(&self) {} /// unsafe fn dec_ref(_obj: NonNull) {} @@ -417,6 +418,7 @@ pub unsafe fn from_raw(ptr: NonNull) -> Self { /// /// let mut data =3D Empty {}; /// let ptr =3D NonNull::::new(&mut data as *mut _).unwrap(); + /// # // SAFETY: TODO. /// let data_ref: ARef =3D unsafe { ARef::from_raw(ptr) }; /// let raw_ptr: NonNull =3D ARef::into_raw(data_ref); /// @@ -483,6 +485,7 @@ pub unsafe trait FromBytes {} =20 macro_rules! impl_frombytes { ($($({$($generics:tt)*})? $t:ty, )*) =3D> { + // SAFETY: Safety comments written in the macro invocation. $(unsafe impl$($($generics)*)? FromBytes for $t {})* }; } @@ -517,6 +520,7 @@ pub unsafe trait AsBytes {} =20 macro_rules! impl_asbytes { ($($({$($generics:tt)*})? $t:ty, )*) =3D> { + // SAFETY: Safety comments written in the macro invocation. $(unsafe impl$($($generics)*)? AsBytes for $t {})* }; } diff --git a/rust/kernel/workqueue.rs b/rust/kernel/workqueue.rs index 493288dc1de0..3b3f1dbe8192 100644 --- a/rust/kernel/workqueue.rs +++ b/rust/kernel/workqueue.rs @@ -519,6 +519,7 @@ unsafe fn raw_get_work(ptr: *mut Self) -> *mut $crate::= workqueue::Work<$work_typ impl{T} HasWork for ClosureWork { self.work } } =20 +// SAFETY: TODO. unsafe impl WorkItemPointer for Arc where T: WorkItem, @@ -536,6 +537,7 @@ unsafe impl WorkItemPointer for A= rc } } =20 +// SAFETY: TODO. unsafe impl RawWorkItem for Arc where T: WorkItem, @@ -564,6 +566,7 @@ unsafe fn __enqueue(self, queue_work_on: F) -> Self:= :EnqueueOutput } } =20 +// SAFETY: TODO. unsafe impl WorkItemPointer for Pin> where T: WorkItem, @@ -583,6 +586,7 @@ unsafe impl WorkItemPointer for P= in> } } =20 +// SAFETY: TODO. unsafe impl RawWorkItem for Pin> where T: WorkItem, diff --git a/rust/uapi/lib.rs b/rust/uapi/lib.rs index 80a00260e3e7..fea2de330d19 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::undocumented_unsafe_blocks, dead_code, missing_docs, non_camel_case_types, --=20 2.46.0 From nobody Mon Dec 15 21:26:25 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 D3C6D14A617; Wed, 4 Sep 2024 20:45:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725482700; cv=none; b=opywEbYQ/iNAzPaANV5DQxd2af1JqS/o0ykekWRrDvKscY1qJKDCtnupBkp+4AHd7uygWbvML2s+s5gc6gZ7WBv+bvwJCr8drp4TK3BdC90dDsfl+pumwrt5G+MuF1+7LrOG2pD1WO7dix3+ejkLgw+yxIOOerIwT2yl9uYBXUg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725482700; c=relaxed/simple; bh=ScGZgrKvu1t/7yMx+LbTEU/fmoPyoLxUOZaQ8KvNmP4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=V4W5k1i/bHtgrg5yaAu2A8FydTKdWV/9oyaqfD+mKpGIXbhLvx+x2qpJx5W8sqVvNpz9xHEcxjbjigB01a4oxC2MFqVL47eV8rQ3l2yB6/IQ0LXl7Y44dCsQc0Q1uBe8ZNUB/d3tO+uLuWFyijGRV7UjObrH+22Lr8ifOyFGCRU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=NKagXVE6; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="NKagXVE6" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3ACDBC4CEC8; Wed, 4 Sep 2024 20:44:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1725482700; bh=ScGZgrKvu1t/7yMx+LbTEU/fmoPyoLxUOZaQ8KvNmP4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=NKagXVE6AImx1gmkJDP7U3fT9eXUG4lfdbOL/Fc5J5gDilVVgAYpOO+J2M26/txJh wW0g5brE25mbzl5gzzFasZI7corU93P99oeWHto1ARmgC5UCYXmC6I72oRVnDeXpGp zBd7eSp4xxPf3j0UzukVXSh/pZ7eNEUsLdWICyw9VaPRgpm6qGoDeOHtcv0ONg5XLf F0L+XhtgfAQb8ta/gTKY5jEy8eZe9qYGB11ZT2StUF91LwyiajGNiBOfsPIRpotc0w yH2h4fAsVNH5dpu+dOr1hhmAd3mzSYKVuqAtlt2xCwA8O0yamEwR+Wvn2i/KFBf5Yd Wn79Cn46Q8YTw== From: Miguel Ojeda To: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho Cc: Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev Subject: [PATCH 05/19] rust: enable `clippy::unnecessary_safety_comment` lint Date: Wed, 4 Sep 2024 22:43:33 +0200 Message-ID: <20240904204347.168520-6-ojeda@kernel.org> In-Reply-To: <20240904204347.168520-1-ojeda@kernel.org> References: <20240904204347.168520-1-ojeda@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In Rust 1.67.0, Clippy added the `unnecessary_safety_comment` lint [1], which is the "inverse" of `undocumented_unsafe_blocks`: it finds places where safe code has a `// SAFETY` comment attached. The lint currently finds 3 places where we had such mistakes, thus it seems already quite useful. Thus clean those and enable it. Link: https://rust-lang.github.io/rust-clippy/master/index.html#/unnecessar= y_safety_comment [1] Signed-off-by: Miguel Ojeda Reviewed-by: Alice Ryhl Reviewed-by: Gary Guo Reviewed-by: Trevor Gross Reviewed-by: Vincenzo Palazzo Tested-by: Gary Guo --- Makefile | 1 + rust/kernel/sync/arc.rs | 2 +- rust/kernel/workqueue.rs | 4 ++-- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index c7a4f313728d..85f37ac9fef8 100644 --- a/Makefile +++ b/Makefile @@ -457,6 +457,7 @@ export rust_common_flags :=3D --edition=3D2021 \ -Wclippy::needless_continue \ -Wclippy::no_mangle_with_rust_abi \ -Wclippy::undocumented_unsafe_blocks \ + -Wclippy::unnecessary_safety_comment \ -Wrustdoc::missing_crate_level_docs =20 KBUILD_HOSTCFLAGS :=3D $(KBUILD_USERHOSTCFLAGS) $(HOST_LFS_CFLAGS) \ diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs index 3021f30fd822..84c7cbfb096d 100644 --- a/rust/kernel/sync/arc.rs +++ b/rust/kernel/sync/arc.rs @@ -341,7 +341,7 @@ fn into_foreign(self) -> *const core::ffi::c_void { } =20 unsafe fn borrow<'a>(ptr: *const core::ffi::c_void) -> ArcBorrow<'a, T= > { - // SAFETY: By the safety requirement of this function, we know tha= t `ptr` came from + // By the safety requirement of this function, we know that `ptr` = came from // a previous call to `Arc::into_foreign`. let inner =3D NonNull::new(ptr as *mut ArcInner).unwrap(); =20 diff --git a/rust/kernel/workqueue.rs b/rust/kernel/workqueue.rs index 3b3f1dbe8192..10d2bc62e2cf 100644 --- a/rust/kernel/workqueue.rs +++ b/rust/kernel/workqueue.rs @@ -526,7 +526,7 @@ unsafe impl WorkItemPointer for A= rc T: HasWork, { unsafe extern "C" fn run(ptr: *mut bindings::work_struct) { - // SAFETY: The `__enqueue` method always uses a `work_struct` stor= ed in a `Work`. + // The `__enqueue` method always uses a `work_struct` stored in a = `Work`. let ptr =3D ptr as *mut Work; // SAFETY: This computes the pointer that `__enqueue` got from `Ar= c::into_raw`. let ptr =3D unsafe { T::work_container_of(ptr) }; @@ -573,7 +573,7 @@ unsafe impl WorkItemPointer for P= in> T: HasWork, { unsafe extern "C" fn run(ptr: *mut bindings::work_struct) { - // SAFETY: The `__enqueue` method always uses a `work_struct` stor= ed in a `Work`. + // The `__enqueue` method always uses a `work_struct` stored in a = `Work`. let ptr =3D ptr as *mut Work; // SAFETY: This computes the pointer that `__enqueue` got from `Ar= c::into_raw`. let ptr =3D unsafe { T::work_container_of(ptr) }; --=20 2.46.0 From nobody Mon Dec 15 21:26:25 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 73E82156678; Wed, 4 Sep 2024 20:45:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725482704; cv=none; b=lEK/mpV8X8Q2pefcxFJtrTcPWTInTRVS0PXYKrHSZ6aStlq2KmaCxSqwC7w65Ej20MY4CIq92sLYmxwLZUQTZ30ugfZlk20dKYCL7XwEQQKaZ22RXnSrM5qrTI0/LgkWPgW+XnTGDrvEIvfu1cWXiFS7ntKrs2eyNCHtfaTff6w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725482704; c=relaxed/simple; bh=7iy+IlKbPFkwvYJYWxLR5kHiJlYjkMePAs9ibg/0adA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=IRKuJZARwT357mPJy06pHnWvfVPLdQ2N70GZ0GeVjGPRz/wYZ41egCwYDG5uoBRDRJfVQzN02EtgMcRip4yz15zL/srcmVhWdYCnMHBTOM47wiCJDHEKNZgUv1aaEOtP6q4I/og3rGfEjUuxuNYDtEVIJQsceeZhfbom5CfdqO0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=q6jhst+5; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="q6jhst+5" Received: by smtp.kernel.org (Postfix) with ESMTPSA id DC381C4CEC2; Wed, 4 Sep 2024 20:45:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1725482704; bh=7iy+IlKbPFkwvYJYWxLR5kHiJlYjkMePAs9ibg/0adA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=q6jhst+5qhRA2z0fr6p58F+mLJu+N/S6dPX8FdRzuPUzGjlnt5IqDqLpD15RmRS8S HYBf7RBQ8M0ltDfGpIpjE8rjkX1zTe9XEduSkWkvvzDFEBtD8kFcWlBkzVM1P1Iedq ADoquiN+IncIMWlusxrL96qz7CN/1LYXR5Je/dCb8TdFpIs/2X04ehXfjYFurLOBIx 7KhQ98bOJYIRVRCK3uFZHgVCpw3+XEiC3MKLbZQQ40/cdNnJSYfjZ6noW+3g0Rw5HU DEp187Jm+rEGYk1NJ4sWKPRKa2gEFE7KMhZwIsubKD9Y+847xgav6GX8z5OPeho3KG BO596oTTk5luw== From: Miguel Ojeda To: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho Cc: Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev Subject: [PATCH 06/19] rust: enable `clippy::unnecessary_safety_doc` lint Date: Wed, 4 Sep 2024 22:43:34 +0200 Message-ID: <20240904204347.168520-7-ojeda@kernel.org> In-Reply-To: <20240904204347.168520-1-ojeda@kernel.org> References: <20240904204347.168520-1-ojeda@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In Rust 1.67.0, Clippy added the `unnecessary_safety_doc` lint [1], which is similar to `unnecessary_safety_comment`, but for `# Safety` sections, i.e. safety preconditions in the documentation. This is something that should not happen with our coding guidelines in mind. Thus enable the lint to have it machine-checked. Link: https://rust-lang.github.io/rust-clippy/master/index.html#/unnecessar= y_safety_doc [1] Signed-off-by: Miguel Ojeda Reviewed-by: Alice Ryhl Reviewed-by: Gary Guo Reviewed-by: Trevor Gross Tested-by: Gary Guo --- Makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/Makefile b/Makefile index 85f37ac9fef8..f903309db91b 100644 --- a/Makefile +++ b/Makefile @@ -458,6 +458,7 @@ export rust_common_flags :=3D --edition=3D2021 \ -Wclippy::no_mangle_with_rust_abi \ -Wclippy::undocumented_unsafe_blocks \ -Wclippy::unnecessary_safety_comment \ + -Wclippy::unnecessary_safety_doc \ -Wrustdoc::missing_crate_level_docs =20 KBUILD_HOSTCFLAGS :=3D $(KBUILD_USERHOSTCFLAGS) $(HOST_LFS_CFLAGS) \ --=20 2.46.0 From nobody Mon Dec 15 21:26:25 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 10501153824; Wed, 4 Sep 2024 20:45:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725482708; cv=none; b=X7lK1MYECyvxS4sSDwfvQfzhhQ6FpHYBz4iIKr8mLNZr9IYfrJnIzZtvo43wiyXsV6T0uv61sYGQ7CKzqHBMTX9tLD3cJElKccZLhQ7LdH7Qqf6VcRh/tODadRWppqKCZf5t+9HdAwmUXovzZpujUMr4D6Bh4MPLwna1MGnyY2I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725482708; c=relaxed/simple; bh=glvDHd6iNw4JzzL+e5mlgcx23zzqkBX2iil+RJ4+6/w=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=IGPEEETLms3X1R+i6V5Gi4/IDifKcsCuttpwaVNoCNLFf1ogmhEM6ZGl0Tc/slM+NApuDRFlzb0ZaGEV5VvlctpDLSmILsOhE7e0aNbJgf4Z+afi91tPpEMfxeUu2Ci+l6rA+l/p3YZLk3/xljZs3lMxdMWVMBhR2IKQQTFyze0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=BM0sidpr; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="BM0sidpr" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7D6B0C4CEC5; Wed, 4 Sep 2024 20:45:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1725482707; bh=glvDHd6iNw4JzzL+e5mlgcx23zzqkBX2iil+RJ4+6/w=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BM0sidprk7i3FSoeF4LZErYZbC4hUpGWVcsPRQPtpaWa53tAAclBbMIk+ZiIcS2p7 gtCDtwVWHxCejWNCeWaExcSHwu/6P4wZK+xxqlSnO/AqTWBouh6Vc0zrPw4urpMI31 LxjWfQznX7s8Qf8udk6lg/0metfmeggiFLAwKMOolbEjEjycwJIEPpekWWkWyeHPJt UoeDAjFFZnRcPN/C9RnWsW+wwHpL2U6vDfNTWa9MksEmRKvz3ez0cVXo7C6bNCT1mG 3VfO1JmVl7P/hnPGtt8NlysRKXUEerE8CqCwX/DGc8QKiFD4t0lSRoazva+yo7uNst 2yYfv6Foq5itw== From: Miguel Ojeda To: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho Cc: Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev Subject: [PATCH 07/19] rust: enable `clippy::ignored_unit_patterns` lint Date: Wed, 4 Sep 2024 22:43:35 +0200 Message-ID: <20240904204347.168520-8-ojeda@kernel.org> In-Reply-To: <20240904204347.168520-1-ojeda@kernel.org> References: <20240904204347.168520-1-ojeda@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In Rust 1.73.0, Clippy introduced the `ignored_unit_patterns` lint [1]: > Matching with `()` explicitly instead of `_` outlines the fact that > the pattern contains no data. Also it would detect a type change > that `_` would ignore. There is only a single case that requires a change: error: matching over `()` is more explicit --> rust/kernel/types.rs:176:45 | 176 | ScopeGuard::new_with_data((), move |_| cleanup()) | ^ help: use `()` inst= ead of `_`: `()` | =3D help: for further information visit https://rust-lang.github.io= /rust-clippy/master/index.html#ignored_unit_patterns =3D note: requested on the command line with `-D clippy::ignored-un= it-patterns` Thus clean it up and enable the lint -- no functional change intended. Link: https://rust-lang.github.io/rust-clippy/master/index.html#/ignored_un= it_patterns [1] Signed-off-by: Miguel Ojeda Reviewed-by: Alice Ryhl Reviewed-by: Gary Guo Reviewed-by: Trevor Gross Tested-by: Gary Guo --- Makefile | 1 + rust/kernel/types.rs | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index f903309db91b..cc1b01590227 100644 --- a/Makefile +++ b/Makefile @@ -452,6 +452,7 @@ export rust_common_flags :=3D --edition=3D2021 \ -Wunreachable_pub \ -Wclippy::all \ -Wclippy::dbg_macro \ + -Wclippy::ignored_unit_patterns \ -Wclippy::mut_mut \ -Wclippy::needless_bitwise_bool \ -Wclippy::needless_continue \ diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs index 6c2d5fa9bce3..4e03df725f3f 100644 --- a/rust/kernel/types.rs +++ b/rust/kernel/types.rs @@ -225,7 +225,7 @@ pub fn dismiss(mut self) -> T { impl ScopeGuard<(), fn(())> { /// Creates a new guarded object with the given cleanup function. pub fn new(cleanup: impl FnOnce()) -> ScopeGuard<(), impl FnOnce(())> { - ScopeGuard::new_with_data((), move |_| cleanup()) + ScopeGuard::new_with_data((), move |()| cleanup()) } } =20 --=20 2.46.0 From nobody Mon Dec 15 21:26:25 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 A2EBD14D457; Wed, 4 Sep 2024 20:45:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725482711; cv=none; b=AMCn17vJbJn8l8VsUt5Rf286LZwZI6m0eG1Dv7CwWxeyU1IbTLNt8aRuXurh4zrtlAFt2LKiDbTiGdMp6u5quW6OT4pCufGuZct/mwP20pXJiUlLdtYK6UUq1svMTcaOxTBDkTpmKjGfk/cCQtN4jgbCSMrJ2m/ixoXfK+x6Y3U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725482711; c=relaxed/simple; bh=GnSFaYVDAVNJbXv/9AtI8T4gMvVKf3w7/yAPMS4ovFI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=RYaVQJb1ViQV1uGFsB/c03Duu4dc62CPQb/o6trZzNp6F/zBa5M22cqfAFioD/kQBjOjcQW09cdfCwyAqEu8RDqD8Q4hPpFSiJviojPBqOAQwaX4f+tbIHxpXMfMXZrVCY4Nii1kcvBBLC4WEFU8nW7RubBG0Wy43RuZePpmimE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=IuM1jjIE; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="IuM1jjIE" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 28BA7C4CEC8; Wed, 4 Sep 2024 20:45:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1725482711; bh=GnSFaYVDAVNJbXv/9AtI8T4gMvVKf3w7/yAPMS4ovFI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=IuM1jjIEjSBxKPLvDFYIIfp1iWElwRH2uO37ZDeP7MXAaWQcS48Qx20WxtfSPpwnp F4FEYtwoz59sMzC+ye1fDUqw0D+kckGSBDi78IkzI1l7KXgIL077BeZY97DPR5x1gX 1UWhDTSoLep7ymbdQB137YogYuS5g1gM1n3e81r1r/zIE8dDQ+4VNY0TRcprZvjObL Lu9WeVYxSInxY9+/xfdj6+W4NqzoCN4F8qcM+hCh+mjWXUptvrEDxVvxjYaLuN0FOe vb/+stYKYPmUKONZETRWo2EhCAL9/XUpjOXuKcXSegoo1UKmIy/PXFQVdHEkKOBnj2 qYCDK8mjs0CvA== From: Miguel Ojeda To: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho Cc: Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev Subject: [PATCH 08/19] rust: enable `rustdoc::unescaped_backticks` lint Date: Wed, 4 Sep 2024 22:43:36 +0200 Message-ID: <20240904204347.168520-9-ojeda@kernel.org> In-Reply-To: <20240904204347.168520-1-ojeda@kernel.org> References: <20240904204347.168520-1-ojeda@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In Rust 1.71.0, `rustdoc` added the `unescaped_backticks` lint, which detects what are typically typos in Markdown formatting regarding inline code [1], e.g. from the Rust standard library: /// ... to `deref`/`deref_mut`` must ... /// ... use [`from_mut`]`. Specifically, ... It does not seem to have almost any false positives, from the experience of enabling it in the Rust standard library [2], which will be checked starting with Rust 1.82.0. The maintainers also confirmed it is ready to be used. Thus enable it. Link: https://doc.rust-lang.org/rustdoc/lints.html#unescaped_backticks [1] Link: https://github.com/rust-lang/rust/pull/128307 [2] Signed-off-by: Miguel Ojeda Reviewed-by: Alice Ryhl Reviewed-by: Gary Guo Reviewed-by: Trevor Gross Tested-by: Gary Guo --- Makefile | 3 ++- rust/Makefile | 5 ++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index cc1b01590227..fc66bac4b4f1 100644 --- a/Makefile +++ b/Makefile @@ -460,7 +460,8 @@ export rust_common_flags :=3D --edition=3D2021 \ -Wclippy::undocumented_unsafe_blocks \ -Wclippy::unnecessary_safety_comment \ -Wclippy::unnecessary_safety_doc \ - -Wrustdoc::missing_crate_level_docs + -Wrustdoc::missing_crate_level_docs \ + -Wrustdoc::unescaped_backticks =20 KBUILD_HOSTCFLAGS :=3D $(KBUILD_USERHOSTCFLAGS) $(HOST_LFS_CFLAGS) \ $(HOSTCFLAGS) -I $(srctree)/scripts/include diff --git a/rust/Makefile b/rust/Makefile index e13d14ec5fe7..bc8ae3cc0537 100644 --- a/rust/Makefile +++ b/rust/Makefile @@ -61,7 +61,7 @@ alloc-cfgs =3D \ quiet_cmd_rustdoc =3D RUSTDOC $(if $(rustdoc_host),H, ) $< cmd_rustdoc =3D \ OBJTREE=3D$(abspath $(objtree)) \ - $(RUSTDOC) $(if $(rustdoc_host),$(rust_common_flags),$(rust_flags)) \ + $(RUSTDOC) $(filter-out $(skip_flags),$(if $(rustdoc_host),$(rust_common_= flags),$(rust_flags))) \ $(rustc_target_flags) -L$(objtree)/$(obj) \ -Zunstable-options --generate-link-to-definition \ --output $(rustdoc_output) \ @@ -98,6 +98,9 @@ rustdoc-macros: private rustc_target_flags =3D --crate-ty= pe proc-macro \ rustdoc-macros: $(src)/macros/lib.rs FORCE +$(call if_changed,rustdoc) =20 +# Starting with Rust 1.82.0, skipping `-Wrustdoc::unescaped_backticks` sho= uld +# not be needed -- see https://github.com/rust-lang/rust/pull/128307. +rustdoc-core: private skip_flags =3D -Wrustdoc::unescaped_backticks rustdoc-core: private rustc_target_flags =3D $(core-cfgs) rustdoc-core: $(RUST_LIB_SRC)/core/src/lib.rs FORCE +$(call if_changed,rustdoc) --=20 2.46.0 From nobody Mon Dec 15 21:26:25 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 0CBED156871; Wed, 4 Sep 2024 20:45:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725482715; cv=none; b=kQXcYbBCfG/v16C9f4bj6QGYF5QvhTKMH02Lai5DqtFQM6bYv1US8atRen9EKsjy9aHXT7rFhNCZGbhp/r+64dDVkTG+8BUcX8/6XwS+VB0dxb+6NErfGy9jQj4+jFR+ZyMa/kCKI1XiyEeudBSt/TtpaOoWXn4/WWIvtIXKl+g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725482715; c=relaxed/simple; bh=+8H8FUXDMNKZ9ZjnDBzrY1hLbGuKfWUHaiLVrZewaZg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Q61YDlCKclBDmfa+m7tY7BcQPyScql9gEv7QgWz5zb8tp/HIhSX9SWQyZz8igPVo/JT+qlJo065751Cu9kWT8U87WJwqYt6zLPnfUGWQGyuRBHL7XCdWrRb2G/ndclQtTmLFGVgdIWcc61o4RvbaEPb98yk4R9kB7jMuoSWlyh0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=NNsPMS55; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="NNsPMS55" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C7F1FC4CEC2; Wed, 4 Sep 2024 20:45:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1725482714; bh=+8H8FUXDMNKZ9ZjnDBzrY1hLbGuKfWUHaiLVrZewaZg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=NNsPMS552RWK+owQPtI2STdb/epZTyYtnaibyWv4qk6s9H+ITD/DPpFPcAL4EAk62 oyWyCO1wwNWx+7l3Ox9VYmCzh2Su4d7R7WbM903Sj2UVp8IZeCDbbb5fGBepuuJsau zBp2pkMtYyFdc/M9bPY/8/dbHrwXJOnxqXlBzFoSSH8+UrgBz+oRvhNCrtgSnwhy74 RQzCt0jtx4RcWFhedewhjf/z7LnZn81KQ3gwjlCxAT1Yh8eH0GVIPEu5USIWRC48bJ EJeioRht5ayLdag+/XN+q0VyTGwaPJNi9bnQXKb+B+w17dd1gPfzltnJCfPfrPfHsf nEknHpKx1XvJg== From: Miguel Ojeda To: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho Cc: Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev Subject: [PATCH 09/19] rust: init: remove unneeded `#[allow(clippy::disallowed_names)]` Date: Wed, 4 Sep 2024 22:43:37 +0200 Message-ID: <20240904204347.168520-10-ojeda@kernel.org> In-Reply-To: <20240904204347.168520-1-ojeda@kernel.org> References: <20240904204347.168520-1-ojeda@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" These few cases, unlike others in the same file, did not need the `allow`. Thus clean them up. Signed-off-by: Miguel Ojeda Reviewed-by: Alice Ryhl Reviewed-by: Gary Guo Reviewed-by: Trevor Gross Tested-by: Gary Guo --- rust/kernel/init.rs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/rust/kernel/init.rs b/rust/kernel/init.rs index 08b9d695c285..aec26a4decb1 100644 --- a/rust/kernel/init.rs +++ b/rust/kernel/init.rs @@ -87,7 +87,6 @@ //! To declare an init macro/function you just return an [`impl PinInit`]: //! //! ```rust -//! # #![allow(clippy::disallowed_names)] //! # use kernel::{sync::Mutex, new_mutex, init::PinInit, try_pin_init}; //! #[pin_data] //! struct DriverData { @@ -368,7 +367,6 @@ macro_rules! stack_try_pin_init { /// The syntax is almost identical to that of a normal `struct` initialize= r: /// /// ```rust -/// # #![allow(clippy::disallowed_names)] /// # use kernel::{init, pin_init, macros::pin_data, init::*}; /// # use core::pin::Pin; /// #[pin_data] @@ -413,7 +411,6 @@ macro_rules! stack_try_pin_init { /// To create an initializer function, simply declare it like this: /// /// ```rust -/// # #![allow(clippy::disallowed_names)] /// # use kernel::{init, pin_init, init::*}; /// # use core::pin::Pin; /// # #[pin_data] @@ -468,7 +465,6 @@ macro_rules! stack_try_pin_init { /// They can also easily embed it into their own `struct`s: /// /// ```rust -/// # #![allow(clippy::disallowed_names)] /// # use kernel::{init, pin_init, macros::pin_data, init::*}; /// # use core::pin::Pin; /// # #[pin_data] --=20 2.46.0 From nobody Mon Dec 15 21:26:25 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 26EEB156871; Wed, 4 Sep 2024 20:45:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725482719; cv=none; b=NjGNOCwF/1AfgGavjLPB4Uqqn8kF0/t2wcGOYx5Eyw3DzA093mnBYH0v7kkA+NYdrsZPMYrO5D8ok7ioKMWWn2L4LowognuqGIZUSp6h0UQPfze5qs6ldgu4mgEdlmuMm2pn5M4zus77K0Z5EN0OHlQuJH7KG/8Rm+lxZKeRO5g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725482719; c=relaxed/simple; bh=vJQvhymk7IQJA94TrCwgY0z7LvJcOwadrzPK6L20pWw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=DPnoH1nn43MIICzFDfn0dQcJakk3FCaL2hVDVyQlrXdAJzC+7IngOKI7lYUaIO6KxbyZ9eWEQO1AmaH6/8Ka8uL4yPb5Dk8x7Xlxf3YEQDcRSfZ4bpGugoBnOYGr1oYiRoesBMpcFhBldQj0oLP+jQ67cr4Bpg0C9lIfzvu+Zj8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=r4gsCmiA; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="r4gsCmiA" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 72845C4CEC5; Wed, 4 Sep 2024 20:45:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1725482718; bh=vJQvhymk7IQJA94TrCwgY0z7LvJcOwadrzPK6L20pWw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=r4gsCmiA8R/FnmPj8VVbJjBYfoAo9rBJ9G0VqScpwzFkZJfj+fh7QsaQYjQtsb3DC nwflgYO5lrAdez2Ehh5klsua6l6GD600MW+67TCoZj7y94OehMLI4XWuiXMM+1JAJo uTOPCN/Zq8/jPPRdMleUG0oonwVptTKqjbjD7p1wNpoUoURCtZPC3p+rmY/nu54siy Bmh2CPbHoPXkAxXABZ6KLMJ5UHNmRRtEgcdyU5u093kzQojDZnYx6PAjhcKJ58LrvZ n8TrwBIFR9bQgobmFE7uSQT6rw1pN4wO9xbvoqPE6AfaraYuAP7AYKmIlaHsqRMT75 k/fPeRMfsOs6w== From: Miguel Ojeda To: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho Cc: Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev Subject: [PATCH 10/19] rust: sync: remove unneeded `#[allow(clippy::non_send_fields_in_send_ty)]` Date: Wed, 4 Sep 2024 22:43:38 +0200 Message-ID: <20240904204347.168520-11-ojeda@kernel.org> In-Reply-To: <20240904204347.168520-1-ojeda@kernel.org> References: <20240904204347.168520-1-ojeda@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Rust 1.58.0 (before Rust was merged into the kernel) made Clippy's `non_send_fields_in_send_ty` lint part of the `suspicious` lint group for a brief window of time [1] until the minor version 1.58.1 got released a week after, where the lint was moved back to `nursery`. By that time, we had already upgraded to that Rust version, and thus we had `allow`ed the lint here for `CondVar`. Nowadays, Clippy's `non_send_fields_in_send_ty` would still trigger here if it were enabled. Moreover, if enabled, `Lock` and `Task` would also require an `allow`. Therefore, it does not seem like someone is actually enabling it (in, e.g., a custom flags build). Finally, the lint does not appear to have had major improvements since then [2]. Thus remove the `allow` since it is unneeded. Link: https://github.com/rust-lang/rust/blob/master/RELEASES.md#version-158= 1-2022-01-20 [1] Link: https://github.com/rust-lang/rust-clippy/issues/8045 [2] Signed-off-by: Miguel Ojeda Reviewed-by: Alice Ryhl Reviewed-by: Gary Guo Reviewed-by: Trevor Gross Tested-by: Gary Guo --- rust/kernel/sync/condvar.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/rust/kernel/sync/condvar.rs b/rust/kernel/sync/condvar.rs index 7e00048bf4b1..dec2e5ffc919 100644 --- a/rust/kernel/sync/condvar.rs +++ b/rust/kernel/sync/condvar.rs @@ -92,7 +92,6 @@ pub struct CondVar { _pin: PhantomPinned, } =20 -#[allow(clippy::non_send_fields_in_send_ty)] // SAFETY: `CondVar` only uses a `struct wait_queue_head`, which is safe t= o use on any thread. unsafe impl Send for CondVar {} =20 --=20 2.46.0 From nobody Mon Dec 15 21:26:25 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 6014D156871; Wed, 4 Sep 2024 20:45:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725482722; cv=none; b=W9kdyr/KbW6BXtAZo09yjK/4qQ1J37tgopguoN+3GuJ0+8v/0wZ9TYWjxMDSpMvRQ+q1GcY67z8i+oIN8lOr927EUZwNTXQub92nYnQEr7oz0tHczf8wIAbseMfmmjTYFz7DXcM+Gj5Y1b0wV5RZP/NAejDjrBp17dLx2oFvYVI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725482722; c=relaxed/simple; bh=c3kGndlFIp+qZPOCF675bBzcX634NpzyVuX7M0WNkek=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=fPGBlQON2wMBM4WcGKLVMqCZw9LTf6rtlJp44X0UgElTxFqHpCii8HjW9IQ586PyMhExL9NI4kpCCrGzJFVZGzreWyxQoyKpYMdjrBXrdybCVXirs+7/I8QKdw0VbmZuPbdsSZKox3ViNJeihOUX8vMwM8PXthbXlttpAO+1Nfg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=qsaii+C2; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="qsaii+C2" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1BF31C4CEC2; Wed, 4 Sep 2024 20:45:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1725482722; bh=c3kGndlFIp+qZPOCF675bBzcX634NpzyVuX7M0WNkek=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qsaii+C2vqxbx7WMAcWvZb7r4tMqSSnquP7CblVaw6t8f+BVJJMCOC5s++1LwrGQ2 W6Ue/ZZ4YIdBpaS2bwkB2R32HGIfrix/tQZ9uBNhEP1ecXEys3DdsLl4yv12GpXyNW QZdwPlVZJ9tQxDkxryhjDdUVF1eyu6CHSjRbmHU9QLFQrEG0FThJUwX8WggCG4F7uC b6JTgWzQZfTFiRjurZ+RvJPTMeXY8dVFykJW1p0FLKREjfZogVO1+Rrtgpp/PqlUQa pLRvSSYhE75d5f5CWpHGTfrfK03MLLk9ieozVHlO6upgLvGSKmxPSbHkqVBYd7qGfZ wIn3O4rtHZ4zg== From: Miguel Ojeda To: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho Cc: Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev Subject: [PATCH 11/19] rust: introduce `.clippy.toml` Date: Wed, 4 Sep 2024 22:43:39 +0200 Message-ID: <20240904204347.168520-12-ojeda@kernel.org> In-Reply-To: <20240904204347.168520-1-ojeda@kernel.org> References: <20240904204347.168520-1-ojeda@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Some Clippy lints can be configured/tweaked. We will use these knobs to our advantage in later commits. This is done via a configuration file, `.clippy.toml` [1]. The file is currently unstable. This may be a problem in the future, but we can adapt as needed. In addition, we proposed adding Clippy to the Rust CI's RFL job [2], so we should be able to catch issues pre-merge. Thus introduce the file. Link: https://doc.rust-lang.org/clippy/configuration.html [1] Link: https://github.com/rust-lang/rust/pull/128928 [2] Signed-off-by: Miguel Ojeda Reviewed-by: Alice Ryhl Reviewed-by: Gary Guo Reviewed-by: Trevor Gross Tested-by: Gary Guo --- .clippy.toml | 1 + .gitignore | 1 + MAINTAINERS | 1 + Makefile | 3 +++ 4 files changed, 6 insertions(+) create mode 100644 .clippy.toml diff --git a/.clippy.toml b/.clippy.toml new file mode 100644 index 000000000000..f66554cd5c45 --- /dev/null +++ b/.clippy.toml @@ -0,0 +1 @@ +# SPDX-License-Identifier: GPL-2.0 diff --git a/.gitignore b/.gitignore index 7902adf4f7f1..907c782962d2 100644 --- a/.gitignore +++ b/.gitignore @@ -102,6 +102,7 @@ modules.order # We don't want to ignore the following even if they are dot-files # !.clang-format +!.clippy.toml !.cocciconfig !.editorconfig !.get_maintainer.ignore diff --git a/MAINTAINERS b/MAINTAINERS index 77b395476a80..cb63f1f97556 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -19922,6 +19922,7 @@ B: https://github.com/Rust-for-Linux/linux/issues C: zulip://rust-for-linux.zulipchat.com P: https://rust-for-linux.com/contributing T: git https://github.com/Rust-for-Linux/linux.git rust-next +F: .clippy.toml F: Documentation/rust/ F: rust/ F: samples/rust/ diff --git a/Makefile b/Makefile index fc66bac4b4f1..234ab97de796 100644 --- a/Makefile +++ b/Makefile @@ -590,6 +590,9 @@ endif # Allows the usage of unstable features in stable compilers. export RUSTC_BOOTSTRAP :=3D 1 =20 +# Allows finding `.clippy.toml` in out-of-srctree builds. +export CLIPPY_CONF_DIR :=3D $(srctree) + export ARCH SRCARCH CONFIG_SHELL BASH HOSTCC KBUILD_HOSTCFLAGS CROSS_COMPI= LE LD CC HOSTPKG_CONFIG export RUSTC RUSTDOC RUSTFMT RUSTC_OR_CLIPPY_QUIET RUSTC_OR_CLIPPY BINDGEN export HOSTRUSTC KBUILD_HOSTRUSTFLAGS --=20 2.46.0 From nobody Mon Dec 15 21:26:25 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 52FDE1714B4; Wed, 4 Sep 2024 20:45:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725482726; cv=none; b=OO4GJAHimwwiqSvaK/RfDgotZQ7Dz+NPStj14bAmKfnUosVncRHplRuWecR4OhbU3kg36EkrI9ePEcTm/gZ7Sy2Q+SLKkB6klz/+XKqNlCop+juefYW3d9OwXAyCDHSM0jA8GThd01MaUPtPoOd8ihrRAGcnV66QRxUK6HuG+AU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725482726; c=relaxed/simple; bh=GCoMjmNj5/XSFld4YyYz7jnEzqA8ETrIM9dohxikmcU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=i4snJsm6MJzICnUqGfF8bD/sEzQrE4v6pu87HnCzc322kewoefUeqaYqocqs25rWU2cTu13XNmxqvXiJh1jTcyl/B4NllJ322i5+NGhvBzIuqn2jlN9WmMV4wMCTuirkb4Q9COwCYQDkOfC0AUgG/aSChuO9De3XgKXbEYYMMxQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=T2WgcxrF; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="T2WgcxrF" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C39B6C4CEC2; Wed, 4 Sep 2024 20:45:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1725482725; bh=GCoMjmNj5/XSFld4YyYz7jnEzqA8ETrIM9dohxikmcU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=T2WgcxrFj+PUSL2LxBGgpdLHtv5MR2gLSHpwMesVKiqcLk1cZ/pyO520zKtr4vf+M 7GvN+3ChqkCmg5yrslYmyBPvv2PZ/ecmRL/dPXNG96NEH7RdpoauEablZlX+qybywm CYuSd6OVcMaSj6ZCMPP8m8MDQgPT6oqdld2ftjmm9/g3I+FvB6bChxyWryWBjOpt9n VfdBK5eXPXC3iNEE3NsTSNK+JfoEmgQaeDuDGKz87W9nkwHWKJHCxeilT7AtOguK5t Ix7bLzU4mOybjMjy0wq1HftpT1Ip2JoKRv8iQ6f9g53lUeh6DvIbWJgxXN/SL0oQn3 vGyubBvQPFrtQ== From: Miguel Ojeda To: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho Cc: Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev Subject: [PATCH 12/19] rust: replace `clippy::dbg_macro` with `disallowed_macros` Date: Wed, 4 Sep 2024 22:43:40 +0200 Message-ID: <20240904204347.168520-13-ojeda@kernel.org> In-Reply-To: <20240904204347.168520-1-ojeda@kernel.org> References: <20240904204347.168520-1-ojeda@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Back when we used Rust 1.60.0 (before Rust was merged in the kernel), we added `-Wclippy::dbg_macro` to the compilation flags. This worked great with our custom `dbg!` macro (vendored from `std`, but slightly modified to use the kernel printing facilities). However, in the very next version, 1.61.0, it stopped working [1] since the lint started to use a Rust diagnostic item rather than a path to find the `dbg!` macro [1]. This behavior remains until the current nightly (1.83.0). Therefore, currently, the `dbg_macro` is not doing anything, which explains why we can invoke `dbg!` in samples/rust/rust_print.rs`, as well as why changing the `#[allow()]`s to `#[expect()]`s in `std_vendor.rs` doctests does not work since they are not fulfilled. One possible workaround is using `rustc_attrs` like the standard library does. However, this is intended to be internal, and we just started supporting several Rust compiler versions, so it is best to avoid it. Therefore, instead, use `disallowed_macros`. It is a stable lint and is more flexible (in that we can provide different macros), although its diagnostic message(s) are not as nice as the specialized one (yet), and does not allow to set different lint levels per macro/path [2]. In turn, this requires allowing the (intentional) `dbg!` use in the sample, as one would have expected. Finally, in a single case, the `allow` is fixed to be an inner attribute, since otherwise it was not being applied. Link: https://github.com/rust-lang/rust-clippy/issues/11303 [1] Link: https://github.com/rust-lang/rust-clippy/issues/11307 [2] Signed-off-by: Miguel Ojeda Reviewed-by: Gary Guo Tested-by: Gary Guo --- .clippy.toml | 6 ++++++ Makefile | 1 - rust/kernel/std_vendor.rs | 10 +++++----- samples/rust/rust_print.rs | 1 + 4 files changed, 12 insertions(+), 6 deletions(-) diff --git a/.clippy.toml b/.clippy.toml index f66554cd5c45..ad9f804fb677 100644 --- a/.clippy.toml +++ b/.clippy.toml @@ -1 +1,7 @@ # SPDX-License-Identifier: GPL-2.0 + +disallowed-macros =3D [ + # The `clippy::dbg_macro` lint only works with `std::dbg!`, thus we si= mulate + # it here, see: https://github.com/rust-lang/rust-clippy/issues/11303. + { path =3D "kernel::dbg", reason =3D "the `dbg!` macro is intended as = a debugging tool" }, +] diff --git a/Makefile b/Makefile index 234ab97de796..f236dd5fb6d9 100644 --- a/Makefile +++ b/Makefile @@ -451,7 +451,6 @@ export rust_common_flags :=3D --edition=3D2021 \ -Wrust_2018_idioms \ -Wunreachable_pub \ -Wclippy::all \ - -Wclippy::dbg_macro \ -Wclippy::ignored_unit_patterns \ -Wclippy::mut_mut \ -Wclippy::needless_bitwise_bool \ diff --git a/rust/kernel/std_vendor.rs b/rust/kernel/std_vendor.rs index 67bf9d37ddb5..085b23312c65 100644 --- a/rust/kernel/std_vendor.rs +++ b/rust/kernel/std_vendor.rs @@ -14,7 +14,7 @@ /// /// ```rust /// let a =3D 2; -/// # #[allow(clippy::dbg_macro)] +/// # #[allow(clippy::disallowed_macros)] /// let b =3D dbg!(a * 2) + 1; /// // ^-- prints: [src/main.rs:2] a * 2 =3D 4 /// assert_eq!(b, 5); @@ -52,7 +52,7 @@ /// With a method call: /// /// ```rust -/// # #[allow(clippy::dbg_macro)] +/// # #[allow(clippy::disallowed_macros)] /// fn foo(n: usize) { /// if dbg!(n.checked_sub(4)).is_some() { /// // ... @@ -71,7 +71,7 @@ /// Naive factorial implementation: /// /// ```rust -/// # #[allow(clippy::dbg_macro)] +/// # #[allow(clippy::disallowed_macros)] /// # { /// fn factorial(n: u32) -> u32 { /// if dbg!(n <=3D 1) { @@ -118,7 +118,7 @@ /// a tuple (and return it, too): /// /// ``` -/// # #[allow(clippy::dbg_macro)] +/// # #![allow(clippy::disallowed_macros)] /// assert_eq!(dbg!(1usize, 2u32), (1, 2)); /// ``` /// @@ -127,7 +127,7 @@ /// invocations. You can use a 1-tuple directly if you need one: /// /// ``` -/// # #[allow(clippy::dbg_macro)] +/// # #[allow(clippy::disallowed_macros)] /// # { /// assert_eq!(1, dbg!(1u32,)); // trailing comma ignored /// assert_eq!((1,), dbg!((1u32,))); // 1-tuple diff --git a/samples/rust/rust_print.rs b/samples/rust/rust_print.rs index 6eabb0d79ea3..ed1137ab2018 100644 --- a/samples/rust/rust_print.rs +++ b/samples/rust/rust_print.rs @@ -15,6 +15,7 @@ =20 struct RustPrint; =20 +#[allow(clippy::disallowed_macros)] fn arc_print() -> Result { use kernel::sync::*; =20 --=20 2.46.0 From nobody Mon Dec 15 21:26:25 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 1373017623F; Wed, 4 Sep 2024 20:45:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725482732; cv=none; b=YjBdT+FxVJ2CoqzOemLEJgMnFuC3U6yc0f3wRMBVX7jGUtMbz1s87v78g6UHtN5VeqI1RnkiX9s9VrCmGX6kUcFKpvlILHm9U5EJigtn9fIAop9s5tBRUxG5F2pGDXhVx1EnGfAeicHgud/KHCQDKyP6pRe5mUSvMSJ6ZRmyhbY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725482732; c=relaxed/simple; bh=Ssl2889E1Mv3hIu22VdL53Z2yZNkmUfjHy2j46S1dHU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=YXrlLbIPBX6fzm6gBfLO3eBXk8jelTxUHhPhmBhVHaMDsW+JOhRTJOdoHD51THpnazGacz2FxH//hazoGvj+IjHrfeySDmgtUaKPcXfwUUTQhihp6CHv3Ur+H0RKeGX0FgnS+wlqwXiHZ+L41/O3MyNgTQezlAH6pbUSuoWPVi8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=F0qIz8o6; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="F0qIz8o6" Received: by smtp.kernel.org (Postfix) with ESMTPSA id CC5BBC4CEC2; Wed, 4 Sep 2024 20:45:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1725482729; bh=Ssl2889E1Mv3hIu22VdL53Z2yZNkmUfjHy2j46S1dHU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=F0qIz8o637Pfw6aqEIzaiKbAl/YwPYiKx9t10pD/8bXV2M/Gc2OfBZte+bV7BcvyB R82nuuOhx0aRtBMWToA82Jmr9XdE/IUEcAsXWAhNaRiu7zDyXy6APCkFjW0E+f6Zhe dr2av2UuQgivstItV3XBw3ND94BL780gzvJ/r58NfOqhdAFDrxvEDybWhOKX4RR1WY vJpeuKTRPs9IP2yhNUVuKdfs9SC8UsP9aetJb1XXBDo5LEyZ1Hu/VWd6wNYiyr6Jl/ 5tN8ExSQ9R0V9e3UKb8GJuB+KVz+2eUvfXVTuW8eNbi8G6gr4s2+d6u9HGkqvDPQjj JlwMwpzNOMWsw== From: Miguel Ojeda To: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho Cc: Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev Subject: [PATCH 13/19] rust: rbtree: fix `SAFETY` comments that should be `# Safety` sections Date: Wed, 4 Sep 2024 22:43:41 +0200 Message-ID: <20240904204347.168520-14-ojeda@kernel.org> In-Reply-To: <20240904204347.168520-1-ojeda@kernel.org> References: <20240904204347.168520-1-ojeda@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The tag `SAFETY` is used for safety comments, i.e. `// SAFETY`, while a `Safety` section is used for safety preconditions in code documentation, i.e. `/// # Safety`. Fix the three instances recently added in `rbtree` that Clippy would have normally caught in a public item, so that we can enable checking of private items in the next commit. Fixes: 98c14e40e07a ("rust: rbtree: add cursor") Signed-off-by: Miguel Ojeda Reviewed-by: Alice Ryhl Reviewed-by: Gary Guo Reviewed-by: Trevor Gross Tested-by: Gary Guo --- rust/kernel/rbtree.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/rust/kernel/rbtree.rs b/rust/kernel/rbtree.rs index 48ceb9560bf5..48e552799e17 100644 --- a/rust/kernel/rbtree.rs +++ b/rust/kernel/rbtree.rs @@ -884,7 +884,8 @@ fn get_neighbor_raw(&self, direction: Direction) -> Opt= ion(node: NonNull) -> (&'b K= , &'b V) { @@ -894,7 +895,8 @@ unsafe fn to_key_value<'b>(node: NonNull) -> (&'b K, &'b V) { (k, unsafe { &*v }) } =20 - /// SAFETY: + /// # Safety + /// /// - `node` must be a valid pointer to a node in an [`RBTree`]. /// - The caller has mutable access to `node` for the duration of 'b. unsafe fn to_key_value_mut<'b>(node: NonNull) -> (&= 'b K, &'b mut V) { @@ -904,7 +906,8 @@ unsafe fn to_key_value_mut<'b>(node: NonNull) -> (&'b K, &'b (k, unsafe { &mut *v }) } =20 - /// SAFETY: + /// # Safety + /// /// - `node` must be a valid pointer to a node in an [`RBTree`]. /// - The caller has immutable access to the key for the duration of '= b. unsafe fn to_key_value_raw<'b>(node: NonNull) -> (&= 'b K, *mut V) { --=20 2.46.0 From nobody Mon Dec 15 21:26:25 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 B4F1B188A37; Wed, 4 Sep 2024 20:45:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725482733; cv=none; b=dVXY6xbt3mEtsSWdXCxcZCSKRlu9dq6t3cvs77/NqWayNMdm4L/fZpxjGqS1g/Oire1LLekU479REgkKq5R9OCxiaAn3WvtzW1HHCUlrh1PiIOJkYle3giCaq8BEumzb1s1oi0+cGg4ET4B2jdq0gjcfY891ZmDAY1SfoV54rRc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725482733; c=relaxed/simple; bh=Dgm67RF7sLef7Ai2nvEooNOfaSun/YAmxcXhHOZZ8NM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=HlFmdtwL8yiH9QBZ0veNe8eR7CGNNDiogF1abJJoI2nYKz0G2ErCHXzeC7iXuahapfUfmENUblWLJW/6ftFobM1tEJWJC2rNiO5gx/PmUijEL95p/AiPjh9q0iNp2fG/FstOGkK6zNrsAzjSK/X9QXLm+4oGJpmffxoqrQPz4vM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=CoailegH; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="CoailegH" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 76E71C4CEC9; Wed, 4 Sep 2024 20:45:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1725482733; bh=Dgm67RF7sLef7Ai2nvEooNOfaSun/YAmxcXhHOZZ8NM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=CoailegHKDAx5oBscDootf2VIpPONvw037tsRr3O0eFfIBUVyQvu77MWmwpL2d8xv eiUhBzdu7CzOz/4s/dzFPMuFbEhoFu1PVkCAvjHRHcvaGk0cgw36eMx/5OagOSldSU fZ7rs3GSrl+ogN0Smk1hX6E6zv+HxPb6LMTrzom6ZiTEFzddPD4rQLwxaF7mJjY9u+ i+dlshORdBUH8aUFYh6FxwfDFF82hJAF33+GTQNwdctlv9LoKEw+gshje8kDt0mhiU EJ/YebvqI7viOJR6R5vsyipNQz156gOoeXtnyAkEhEexmgE/oeGFD5Szq8+KnJ5ezR 7cvRePDSGNlgQ== From: Miguel Ojeda To: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho Cc: Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev Subject: [PATCH 14/19] rust: provide proper code documentation titles Date: Wed, 4 Sep 2024 22:43:42 +0200 Message-ID: <20240904204347.168520-15-ojeda@kernel.org> In-Reply-To: <20240904204347.168520-1-ojeda@kernel.org> References: <20240904204347.168520-1-ojeda@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Rust 1.82.0's Clippy is introducing [1][2] a new warn-by-default lint, `too_long_first_doc_paragraph` [3], which is intended to catch titles of code documentation items that are too long (likely because no title was provided and the item documentation starts with a paragraph). This lint does not currently trigger anywhere, but it does detect a couple cases we had in private cases if checking for private items gets enabled (which we will do in the next commit): error: first doc comment paragraph is too long --> rust/kernel/init/__internal.rs:18:1 | 18 | / /// This is the module-internal type implementing `PinInit` and = `Init`. It is unsafe to create this 19 | | /// type, since the closure needs to fulfill the same safety req= uirement as the 20 | | /// `__pinned_init`/`__init` functions. | |_ | =3D help: for further information visit https://rust-lang.github.io/= rust-clippy/master/index.html#too_long_first_doc_paragraph =3D note: `-D clippy::too-long-first-doc-paragraph` implied by `-D w= arnings` =3D help: to override `-D warnings` add `#[allow(clippy::too_long_fi= rst_doc_paragraph)]` error: first doc comment paragraph is too long --> rust/kernel/sync/arc/std_vendor.rs:3:1 | 3 | / //! The contents of this file come from the Rust standard library= , hosted in 4 | | //! the repository, licensed = under 5 | | //! "Apache-2.0 OR MIT" and adapted for kernel use. For copyright= details, 6 | | //! see . | |_ | =3D help: for further information visit https://rust-lang.github.io/r= ust-clippy/master/index.html#too_long_first_doc_paragraph Thus clean the two instances we hit and enable the lint. In addition, since we have a second `std_vendor.rs` file with a similar header, do the same there too (even if that one does not trigger the lint, because it is `doc(hidden)`). Link: https://github.com/rust-lang/rust/pull/129531 [1] Link: https://github.com/rust-lang/rust-clippy/pull/12993 [2] Link: https://rust-lang.github.io/rust-clippy/master/index.html#/too_long_f= irst_doc_paragraph [3] Signed-off-by: Miguel Ojeda Reviewed-by: Alice Ryhl Reviewed-by: Gary Guo Reviewed-by: Trevor Gross Tested-by: Gary Guo --- rust/kernel/init/__internal.rs | 7 ++++--- rust/kernel/std_vendor.rs | 2 ++ rust/kernel/sync/arc/std_vendor.rs | 2 ++ 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/rust/kernel/init/__internal.rs b/rust/kernel/init/__internal.rs index 29f4fd00df3d..163eb072f296 100644 --- a/rust/kernel/init/__internal.rs +++ b/rust/kernel/init/__internal.rs @@ -15,9 +15,10 @@ /// [this table]: https://doc.rust-lang.org/nomicon/phantom-data.html#tabl= e-of-phantomdata-patterns pub(super) type Invariant =3D PhantomData *mut T>; =20 -/// This is the module-internal type implementing `PinInit` and `Init`. It= is unsafe to create this -/// type, since the closure needs to fulfill the same safety requirement a= s the -/// `__pinned_init`/`__init` functions. +/// Module-internal type implementing `PinInit` and `Init`. +/// +/// It is unsafe to create this type, since the closure needs to fulfill t= he same safety +/// requirement as the `__pinned_init`/`__init` functions. pub(crate) struct InitClosure(pub(crate) F, pub(crate) In= variant<(E, T)>); =20 // SAFETY: While constructing the `InitClosure`, the user promised that it= upholds the diff --git a/rust/kernel/std_vendor.rs b/rust/kernel/std_vendor.rs index 085b23312c65..d59e4cf4b252 100644 --- a/rust/kernel/std_vendor.rs +++ b/rust/kernel/std_vendor.rs @@ -1,5 +1,7 @@ // SPDX-License-Identifier: Apache-2.0 OR MIT =20 +//! Rust standard library vendored code. +//! //! The contents of this file come from the Rust standard library, hosted = in //! the repository, licensed under //! "Apache-2.0 OR MIT" and adapted for kernel use. For copyright details, diff --git a/rust/kernel/sync/arc/std_vendor.rs b/rust/kernel/sync/arc/std_= vendor.rs index a66a0c2831b3..11b3f4ecca5f 100644 --- a/rust/kernel/sync/arc/std_vendor.rs +++ b/rust/kernel/sync/arc/std_vendor.rs @@ -1,5 +1,7 @@ // SPDX-License-Identifier: Apache-2.0 OR MIT =20 +//! Rust standard library vendored code. +//! //! The contents of this file come from the Rust standard library, hosted = in //! the repository, licensed under //! "Apache-2.0 OR MIT" and adapted for kernel use. For copyright details, --=20 2.46.0 From nobody Mon Dec 15 21:26:25 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 98CD9189901; Wed, 4 Sep 2024 20:45:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725482737; cv=none; b=SHwWW8pTYTKd7B+sqnWSp0RSvKSGWzO4tqWoi69KSO0Ell46Tms9O4EUcpQua04NEnNTES7HwR14qQNnsjKTcnVY/7mDRUU9z6k9ejs94frpY7ln59ESZRWrNbkCOqiadhT8veq3EkIk6hiceM+wiHpK1dlFSIN4+i2biI1JaSw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725482737; c=relaxed/simple; bh=cBnyB+G22zZM8jvLBjn3o4X1EmQK4MqorlwyKyCTf9I=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=BWif67RDiRhDUHVlahwhSwanceEkFUkWPPMhM0yYu4iBBQHZXrH84AEzVVbujHqUWVPqQfy5J5I6KeOXxZspclBt45P6AVgolZoPx6dOLnV6y+AvZUZsS5rE7AGTuCYVtt8+UnqCrM5VCt+d1jgxQGc0VRQhn9VKW8LsWnKihW4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=WMqpNZmv; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="WMqpNZmv" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 24F9BC4CEC8; Wed, 4 Sep 2024 20:45:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1725482737; bh=cBnyB+G22zZM8jvLBjn3o4X1EmQK4MqorlwyKyCTf9I=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WMqpNZmv8k3eO1eYwnnKP187I5CEFxSqg+kzToGlDhRdxXTB42lj2sTFI0iMS4JUo nrWIB28EDnsepmQ5gqAm91KjoV/SiJt9j1KeD4GiEfnRsBzENCHZ61sz+nZieVXa2h fsST0DSvEBX8wGoLvzJfmpK+aAkIP7bsHg0mCnd/wToE51h+bC4xIyLBPLeshb1Gkn XFrekKtPXA6A0Z857mb5q8yESoPWH4P79UqfhAGpOLtLsL9x75AJZPNbLboiR2rj0T n/X/iYXTWq0Kd9JfAh9+VGEKq710LI8JCBhRBs8nXBl3+n4AfumbktIIX0kHByLyir YkukUqhpwZdzw== From: Miguel Ojeda To: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho Cc: Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev Subject: [PATCH 15/19] rust: enable Clippy's `check-private-items` Date: Wed, 4 Sep 2024 22:43:43 +0200 Message-ID: <20240904204347.168520-16-ojeda@kernel.org> In-Reply-To: <20240904204347.168520-1-ojeda@kernel.org> References: <20240904204347.168520-1-ojeda@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In Rust 1.76.0, Clippy added the `check-private-items` lint configuration option. When turned on (the default is off), it makes several lints check private items as well. In our case, it affects two lints we have enabled [1]: `missing_safety_doc` and `unnecessary_safety_doc`. It also seems to affect the new `too_long_first_doc_paragraph` lint [2], even though the documentation does not mention it. Thus allow the few instances remaining we currently hit and enable the lint. Link: https://doc.rust-lang.org/nightly/clippy/lint_configuration.html#chec= k-private-items [1] Link: https://rust-lang.github.io/rust-clippy/master/index.html#/too_long_f= irst_doc_paragraph [2] Signed-off-by: Miguel Ojeda Reviewed-by: Alice Ryhl Reviewed-by: Gary Guo Reviewed-by: Trevor Gross Tested-by: Gary Guo --- .clippy.toml | 2 ++ rust/kernel/init.rs | 1 + rust/kernel/init/__internal.rs | 2 ++ rust/kernel/init/macros.rs | 1 + rust/kernel/print.rs | 1 + 5 files changed, 7 insertions(+) diff --git a/.clippy.toml b/.clippy.toml index ad9f804fb677..e4c4eef10b28 100644 --- a/.clippy.toml +++ b/.clippy.toml @@ -1,5 +1,7 @@ # SPDX-License-Identifier: GPL-2.0 =20 +check-private-items =3D true + disallowed-macros =3D [ # The `clippy::dbg_macro` lint only works with `std::dbg!`, thus we si= mulate # it here, see: https://github.com/rust-lang/rust-clippy/issues/11303. diff --git a/rust/kernel/init.rs b/rust/kernel/init.rs index aec26a4decb1..10ec90a5f5d8 100644 --- a/rust/kernel/init.rs +++ b/rust/kernel/init.rs @@ -125,6 +125,7 @@ //! use core::{ptr::addr_of_mut, marker::PhantomPinned, pin::Pin}; //! # mod bindings { //! # #![allow(non_camel_case_types)] +//! # #![allow(clippy::missing_safety_doc)] //! # pub struct foo; //! # pub unsafe fn init_foo(_ptr: *mut foo) {} //! # pub unsafe fn destroy_foo(_ptr: *mut foo) {} diff --git a/rust/kernel/init/__internal.rs b/rust/kernel/init/__internal.rs index 163eb072f296..549ae227c2ea 100644 --- a/rust/kernel/init/__internal.rs +++ b/rust/kernel/init/__internal.rs @@ -54,6 +54,7 @@ unsafe fn __pinned_init(self, slot: *mut T) -> Result<(),= E> { pub unsafe trait HasPinData { type PinData: PinData; =20 + #[allow(clippy::missing_safety_doc)] unsafe fn __pin_data() -> Self::PinData; } =20 @@ -83,6 +84,7 @@ fn make_closure(self, f: F) -> F pub unsafe trait HasInitData { type InitData: InitData; =20 + #[allow(clippy::missing_safety_doc)] unsafe fn __init_data() -> Self::InitData; } =20 diff --git a/rust/kernel/init/macros.rs b/rust/kernel/init/macros.rs index 93af8f3d8d4d..8733ba3834ab 100644 --- a/rust/kernel/init/macros.rs +++ b/rust/kernel/init/macros.rs @@ -989,6 +989,7 @@ fn drop(&mut self) { // // The functions are `unsafe` to prevent accidentally calling them. #[allow(dead_code)] + #[allow(clippy::missing_safety_doc)] impl<$($impl_generics)*> $pin_data<$($ty_generics)*> where $($whr)* { diff --git a/rust/kernel/print.rs b/rust/kernel/print.rs index fe53fc469c4f..45af17095a24 100644 --- a/rust/kernel/print.rs +++ b/rust/kernel/print.rs @@ -14,6 +14,7 @@ use crate::str::RawFormatter; =20 // Called from `vsprintf` with format specifier `%pA`. +#[allow(clippy::missing_safety_doc)] #[no_mangle] unsafe extern "C" fn rust_fmt_argument( buf: *mut c_char, --=20 2.46.0 From nobody Mon Dec 15 21:26:25 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 4D1C317BEBA; Wed, 4 Sep 2024 20:45:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725482741; cv=none; b=XPR6j2i4fxybQtcCNLlnZ6/PRLJZ2Fp4+u/Do4g2i/paHFT8BFqn5DU4l04Z3JXC+4O54M9YWbkXvN3pqPvHlpi70ovpn0Qddt5ilRSxcVE/eKgu+gelvCX9l+HdpJpwgwPkxskIhfkjxwVOZiY3j9uhGcoYUUs5O0H5WMTa/ig= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725482741; c=relaxed/simple; bh=9m9JuBDyRlVPdIAlBLEt9SXRJe2c6TVhJRYZmonhyAo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=PRchWZOw/gfu1hb2XfQ7zoRPvP23Tnkscrbs57VKKVQM4383xuJ+yirvGTGaNglrgwYxrxOmia9bLa/SqbOJ2SGWAsjBLwZWOVA+C9ipw46lnxGvfsZu9y2mPbyt2lGGa79k/zPmkSqoGkWwPiIAsswdYqxLZYq4bKaeps9y/2Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=rGC3OYSN; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="rGC3OYSN" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C4D61C4CEC2; Wed, 4 Sep 2024 20:45:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1725482740; bh=9m9JuBDyRlVPdIAlBLEt9SXRJe2c6TVhJRYZmonhyAo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rGC3OYSNmww7COr3wah7oXCH76t16EyGHkkxPKuvAZFXR+knc7VHX1tBtYsvR/JJ9 0rNCR3Jpq/VW91wEHz6Fy0WLigsmpvhKk5YJNh1FtOXzg5WqmjunTKSMTkOWHNOY/3 3eqJd2Y5xs2yPKlRWSPCm9BYS/bD1gCs5zOpLJTBl/fwC/oKJ/uqrRflSoWoBBRztd 1opz1yXNEHz5BgJ/eYXxso63ARNNxgYkkck7ysIqx7uuzqwHINSVQkR63WkwcHw2Fu A3JGmzyLtrxTKMaZT8rxNA1gv52ympt7eXn7t+/seNtfyQs8FPnW5CbHlBdijMWObm VGdXKIoGX9+sA== From: Miguel Ojeda To: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho Cc: Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev Subject: [PATCH 16/19] Documentation: rust: add coding guidelines on lints Date: Wed, 4 Sep 2024 22:43:44 +0200 Message-ID: <20240904204347.168520-17-ojeda@kernel.org> In-Reply-To: <20240904204347.168520-1-ojeda@kernel.org> References: <20240904204347.168520-1-ojeda@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In the C side, disabling diagnostics locally, i.e. within the source code, is rare (at least in the kernel). Sometimes warnings are manipulated via the flags at the translation unit level, but that is about it. In Rust, it is easier to change locally the "level" of lints (e.g. allowing them locally). In turn, this means it is easier to globally enable more lints that may trigger a few false positives here and there that need to be allowed ocally, but that generally can spot issues or bugs. Thus document this. Signed-off-by: Miguel Ojeda Reviewed-by: Alice Ryhl Reviewed-by: Gary Guo Reviewed-by: Trevor Gross Tested-by: Gary Guo --- Documentation/rust/coding-guidelines.rst | 29 ++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/Documentation/rust/coding-guidelines.rst b/Documentation/rust/= coding-guidelines.rst index 05542840b16c..185d3b51117d 100644 --- a/Documentation/rust/coding-guidelines.rst +++ b/Documentation/rust/coding-guidelines.rst @@ -227,3 +227,32 @@ The equivalent in Rust may look like (ignoring documen= tation): That is, the equivalent of ``GPIO_LINE_DIRECTION_IN`` would be referred to= as ``gpio::LineDirection::In``. In particular, it should not be named ``gpio::gpio_line_direction::GPIO_LINE_DIRECTION_IN``. + + +Lints +----- + +In Rust, it is possible to ``allow`` particular warnings (diagnostics, lin= ts) +locally, making the compiler ignore instances of a given warning within a = given +function, module, block, etc. + +It is similar to ``#pragma GCC diagnostic push`` + ``ignored`` + ``pop`` i= n C: + +.. code-block:: c + + #pragma GCC diagnostic push + #pragma GCC diagnostic ignored "-Wunused-function" + static void f(void) {} + #pragma GCC diagnostic pop + +But way less verbose: + +.. code-block:: rust + + #[allow(dead_code)] + fn f() {} + +By that virtue, it makes it possible to comfortably enable more diagnostic= s by +default (i.e. outside ``W=3D`` levels). In particular, those that may have= some +false positives but that are otherwise quite useful to keep enabled to cat= ch +potential mistakes. --=20 2.46.0 From nobody Mon Dec 15 21:26:25 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 1DBEE18C341; Wed, 4 Sep 2024 20:45:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725482745; cv=none; b=Zx3Z55SYDkA2PNg9UKjGHuCyIr4Hg5zk+1ifAJkHwBTOwAJ9e3GQ1fxO8ERy+wxPqlopY6BpPj7EZlP4J9McDzcYxHHudSlkdnqSxGG7OCTXwq4mCMA6Gng8MpsbVNHtzPTx4vIrOptKYXxa26A+wKdiUDWDKKrgzq7gi8ZkrrA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725482745; c=relaxed/simple; bh=GPaBT0lhSCZJqNDFb5GZKBTR5vuksnYyjnuKqKF+iss=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=dJE7RsNVSDNanhFTC6kjkXd030TExCgIVrQlgp5qYovnN0xZzgTUDsmzJ7VRfLmSh383rDMFcwdlMroQW+uck9xACQ9LOzMRXZj8pHIKKuS3qCsUY9FvivdacV5ANzr8x//7RmKIEV+QpyR/CyXvYYV3ehlUx1zjiBiHz9arZ9Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Fmrn9iqR; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Fmrn9iqR" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6ECDDC4CEC5; Wed, 4 Sep 2024 20:45:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1725482745; bh=GPaBT0lhSCZJqNDFb5GZKBTR5vuksnYyjnuKqKF+iss=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Fmrn9iqRNW705AbUXKmoIJ9MsywhG3lUGf42Mn7wdhy96f6tGI9GCY4NMbXMZKu+M Y9kUeMEir/D/WG+5py4qeiaOUrmK/Q+4pIiDs9YXKBIy5Z7hYVqIH3qlgrtzdhhIPr i5LX53mYt+NhHcpaj9OHN0QL+BPqJeDoncBQb6suu5G0X+q6msBMfQnSN/6c8IpoP2 2goAfYITzshAmxRN4Gg6+O6vzZZqSSZ1GiXmO+jPvdab7XWMuonjO/UwhK+G4liCBu rFHMCNTUi9HiXBMHWHEyBwP+srABwhNoMqLa4/5yOqC4H2CVzK/aLQelW1uhBUCMIs NteP6i/vqdfjQ== From: Miguel Ojeda To: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho Cc: Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev, Fridtjof Stoldt , Urgau Subject: [PATCH 17/19] rust: start using the `#[expect(...)]` attribute Date: Wed, 4 Sep 2024 22:43:45 +0200 Message-ID: <20240904204347.168520-18-ojeda@kernel.org> In-Reply-To: <20240904204347.168520-1-ojeda@kernel.org> References: <20240904204347.168520-1-ojeda@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In Rust, it is possible to `allow` particular warnings (diagnostics, lints) locally, making the compiler ignore instances of a given warning within a given function, module, block, etc. It is similar to `#pragma GCC diagnostic push` + `ignored` + `pop` in C: #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wunused-function" static void f(void) {} #pragma GCC diagnostic pop But way less verbose: #[allow(dead_code)] fn f() {} By that virtue, it makes it possible to comfortably enable more diagnostics by default (i.e. outside `W=3D` levels) that may have some false positives but that are otherwise quite useful to keep enabled to catch potential mistakes. The `#[expect(...)]` attribute [1] takes this further, and makes the compiler warn if the diagnostic was _not_ produced. For instance, the following will ensure that, when `f()` is called somewhere, we will have to remove the attribute: #[expect(dead_code)] fn f() {} If we do not, we get a warning from the compiler: warning: this lint expectation is unfulfilled --> x.rs:3:10 | 3 | #[expect(dead_code)] | ^^^^^^^^^ | =3D note: `#[warn(unfulfilled_lint_expectations)]` on by default This means that `expect`s do not get forgotten when they are not needed. See the next commit for more details, nuances on its usage and documentation on the feature. The attribute requires the `lint_reasons` [2] unstable feature, but it is becoming stable in 1.81.0 (to be released on 2024-09-05) and it has already been useful to clean things up in this patch series, finding cases where the `allow`s should not have been there. Thus, enable `lint_reasons` and convert some of our `allow`s to `expect`s where possible. This feature was also an example of the ongoing collaboration between Rust and the kernel -- we tested it in the kernel early on and found an issue that was quickly resolved [3]. Cc: Fridtjof Stoldt Cc: Urgau Link: https://rust-lang.github.io/rfcs/2383-lint-reasons.html#expect-lint-a= ttribute [1] Link: https://github.com/rust-lang/rust/issues/54503 [2] Link: https://github.com/rust-lang/rust/issues/114557 [3] Signed-off-by: Miguel Ojeda Reviewed-by: Alice Ryhl Reviewed-by: Gary Guo Reviewed-by: Trevor Gross Tested-by: Gary Guo --- rust/kernel/error.rs | 2 +- rust/kernel/init.rs | 22 +++++++++++----------- rust/kernel/init/__internal.rs | 4 ++-- rust/kernel/init/macros.rs | 10 +++++----- rust/kernel/ioctl.rs | 2 +- rust/kernel/lib.rs | 1 + rust/kernel/list/arc_field.rs | 2 +- rust/kernel/print.rs | 4 ++-- rust/kernel/std_vendor.rs | 10 +++++----- samples/rust/rust_print.rs | 2 +- scripts/Makefile.build | 2 +- 11 files changed, 31 insertions(+), 30 deletions(-) diff --git a/rust/kernel/error.rs b/rust/kernel/error.rs index 639bc7572f90..a681acda87ce 100644 --- a/rust/kernel/error.rs +++ b/rust/kernel/error.rs @@ -133,7 +133,7 @@ pub(crate) fn to_blk_status(self) -> bindings::blk_stat= us_t { } =20 /// Returns the error encoded as a pointer. - #[allow(dead_code)] + #[expect(dead_code)] pub(crate) fn to_ptr(self) -> *mut T { #[cfg_attr(target_pointer_width =3D "32", allow(clippy::useless_co= nversion))] // SAFETY: `self.0` is a valid error due to its invariant. diff --git a/rust/kernel/init.rs b/rust/kernel/init.rs index 10ec90a5f5d8..25057cbed40b 100644 --- a/rust/kernel/init.rs +++ b/rust/kernel/init.rs @@ -35,7 +35,7 @@ //! that you need to write `<-` instead of `:` for fields that you want to= initialize in-place. //! //! ```rust -//! # #![allow(clippy::disallowed_names)] +//! # #![expect(clippy::disallowed_names)] //! use kernel::sync::{new_mutex, Mutex}; //! # use core::pin::Pin; //! #[pin_data] @@ -55,7 +55,7 @@ //! (or just the stack) to actually initialize a `Foo`: //! //! ```rust -//! # #![allow(clippy::disallowed_names)] +//! # #![expect(clippy::disallowed_names)] //! # use kernel::sync::{new_mutex, Mutex}; //! # use core::pin::Pin; //! # #[pin_data] @@ -120,12 +120,12 @@ //! `slot` gets called. //! //! ```rust -//! # #![allow(unreachable_pub, clippy::disallowed_names)] +//! # #![expect(unreachable_pub, clippy::disallowed_names)] //! use kernel::{init, types::Opaque}; //! use core::{ptr::addr_of_mut, marker::PhantomPinned, pin::Pin}; //! # mod bindings { -//! # #![allow(non_camel_case_types)] -//! # #![allow(clippy::missing_safety_doc)] +//! # #![expect(non_camel_case_types)] +//! # #![expect(clippy::missing_safety_doc)] //! # pub struct foo; //! # pub unsafe fn init_foo(_ptr: *mut foo) {} //! # pub unsafe fn destroy_foo(_ptr: *mut foo) {} @@ -238,7 +238,7 @@ /// # Examples /// /// ```rust -/// # #![allow(clippy::disallowed_names)] +/// # #![expect(clippy::disallowed_names)] /// # use kernel::{init, macros::pin_data, pin_init, stack_pin_init, init:= :*, sync::Mutex, new_mutex}; /// # use core::pin::Pin; /// #[pin_data] @@ -290,7 +290,7 @@ macro_rules! stack_pin_init { /// # Examples /// /// ```rust,ignore -/// # #![allow(clippy::disallowed_names)] +/// # #![expect(clippy::disallowed_names)] /// # use kernel::{init, pin_init, stack_try_pin_init, init::*, sync::Mute= x, new_mutex}; /// # use macros::pin_data; /// # use core::{alloc::AllocError, pin::Pin}; @@ -316,7 +316,7 @@ macro_rules! stack_pin_init { /// ``` /// /// ```rust,ignore -/// # #![allow(clippy::disallowed_names)] +/// # #![expect(clippy::disallowed_names)] /// # use kernel::{init, pin_init, stack_try_pin_init, init::*, sync::Mute= x, new_mutex}; /// # use macros::pin_data; /// # use core::{alloc::AllocError, pin::Pin}; @@ -438,7 +438,7 @@ macro_rules! stack_try_pin_init { /// Users of `Foo` can now create it like this: /// /// ```rust -/// # #![allow(clippy::disallowed_names)] +/// # #![expect(clippy::disallowed_names)] /// # use kernel::{init, pin_init, macros::pin_data, init::*}; /// # use core::pin::Pin; /// # #[pin_data] @@ -852,7 +852,7 @@ pub unsafe trait PinInit: = Sized { /// # Examples /// /// ```rust - /// # #![allow(clippy::disallowed_names)] + /// # #![expect(clippy::disallowed_names)] /// use kernel::{types::Opaque, init::pin_init_from_closure}; /// #[repr(C)] /// struct RawFoo([u8; 16]); @@ -964,7 +964,7 @@ pub unsafe trait Init: Pin= Init { /// # Examples /// /// ```rust - /// # #![allow(clippy::disallowed_names)] + /// # #![expect(clippy::disallowed_names)] /// use kernel::{types::Opaque, init::{self, init_from_closure}}; /// struct Foo { /// buf: [u8; 1_000_000], diff --git a/rust/kernel/init/__internal.rs b/rust/kernel/init/__internal.rs index 549ae227c2ea..44431fba7aab 100644 --- a/rust/kernel/init/__internal.rs +++ b/rust/kernel/init/__internal.rs @@ -54,7 +54,7 @@ unsafe fn __pinned_init(self, slot: *mut T) -> Result<(),= E> { pub unsafe trait HasPinData { type PinData: PinData; =20 - #[allow(clippy::missing_safety_doc)] + #[expect(clippy::missing_safety_doc)] unsafe fn __pin_data() -> Self::PinData; } =20 @@ -84,7 +84,7 @@ fn make_closure(self, f: F) -> F pub unsafe trait HasInitData { type InitData: InitData; =20 - #[allow(clippy::missing_safety_doc)] + #[expect(clippy::missing_safety_doc)] unsafe fn __init_data() -> Self::InitData; } =20 diff --git a/rust/kernel/init/macros.rs b/rust/kernel/init/macros.rs index 8733ba3834ab..dcc280c7581f 100644 --- a/rust/kernel/init/macros.rs +++ b/rust/kernel/init/macros.rs @@ -182,13 +182,13 @@ //! // Normally `Drop` bounds do not have the correct semantics, but f= or this purpose they do //! // (normally people want to know if a type has any kind of drop gl= ue at all, here we want //! // to know if it has any kind of custom drop glue, which is exactl= y what this bound does). -//! #[allow(drop_bounds)] +//! #[expect(drop_bounds)] //! impl MustNotImplDrop for T {} //! impl MustNotImplDrop for Bar {} //! // Here comes a convenience check, if one implemented `PinnedDrop`= , but forgot to add it to //! // `#[pin_data]`, then this will error with the same mechanic as a= bove, this is not needed //! // for safety, but a good sanity check, since no normal code calls= `PinnedDrop::drop`. -//! #[allow(non_camel_case_types)] +//! #[expect(non_camel_case_types)] //! trait UselessPinnedDropImpl_you_need_to_specify_PinnedDrop {} //! impl< //! T: ::kernel::init::PinnedDrop, @@ -925,14 +925,14 @@ impl<'__pin, $($impl_generics)*> ::core::marker::Unpi= n for $name<$($ty_generics) // `Drop`. Additionally we will implement this trait for the struc= t leading to a conflict, // if it also implements `Drop` trait MustNotImplDrop {} - #[allow(drop_bounds)] + #[expect(drop_bounds)] impl MustNotImplDrop for T {} impl<$($impl_generics)*> MustNotImplDrop for $name<$($ty_generics)= *> where $($whr)* {} // We also take care to prevent users from writing a useless `Pinn= edDrop` implementation. // They might implement `PinnedDrop` correctly for the struct, but= forget to give // `PinnedDrop` as the parameter to `#[pin_data]`. - #[allow(non_camel_case_types)] + #[expect(non_camel_case_types)] trait UselessPinnedDropImpl_you_need_to_specify_PinnedDrop {} impl UselessPinnedDropImpl_you_need_to_specify_PinnedDrop for T {} @@ -989,7 +989,7 @@ fn drop(&mut self) { // // The functions are `unsafe` to prevent accidentally calling them. #[allow(dead_code)] - #[allow(clippy::missing_safety_doc)] + #[expect(clippy::missing_safety_doc)] impl<$($impl_generics)*> $pin_data<$($ty_generics)*> where $($whr)* { diff --git a/rust/kernel/ioctl.rs b/rust/kernel/ioctl.rs index cfa7d080b531..2fc7662339e5 100644 --- a/rust/kernel/ioctl.rs +++ b/rust/kernel/ioctl.rs @@ -4,7 +4,7 @@ //! //! C header: [`include/asm-generic/ioctl.h`](srctree/include/asm-generic/= ioctl.h) =20 -#![allow(non_snake_case)] +#![expect(non_snake_case)] =20 use crate::build_assert; =20 diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index f10b06a78b9d..7d1c97a21fe1 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -14,6 +14,7 @@ #![no_std] #![feature(coerce_unsized)] #![feature(dispatch_from_dyn)] +#![feature(lint_reasons)] #![feature(new_uninit)] #![feature(receiver_trait)] #![feature(unsize)] diff --git a/rust/kernel/list/arc_field.rs b/rust/kernel/list/arc_field.rs index 2330f673427a..c4b9dd503982 100644 --- a/rust/kernel/list/arc_field.rs +++ b/rust/kernel/list/arc_field.rs @@ -56,7 +56,7 @@ pub unsafe fn assert_ref(&self) -> &T { /// /// The caller must have mutable access to the `ListArc` containin= g the struct with this /// field for the duration of the returned reference. - #[allow(clippy::mut_from_ref)] + #[expect(clippy::mut_from_ref)] pub unsafe fn assert_mut(&self) -> &mut T { // SAFETY: The caller has exclusive access to the `ListArc`, so th= ey also have exclusive // access to this field. diff --git a/rust/kernel/print.rs b/rust/kernel/print.rs index 45af17095a24..a28077a7cb30 100644 --- a/rust/kernel/print.rs +++ b/rust/kernel/print.rs @@ -14,7 +14,7 @@ use crate::str::RawFormatter; =20 // Called from `vsprintf` with format specifier `%pA`. -#[allow(clippy::missing_safety_doc)] +#[expect(clippy::missing_safety_doc)] #[no_mangle] unsafe extern "C" fn rust_fmt_argument( buf: *mut c_char, @@ -140,7 +140,7 @@ pub fn call_printk_cont(args: fmt::Arguments<'_>) { #[doc(hidden)] #[cfg(not(testlib))] #[macro_export] -#[allow(clippy::crate_in_macro_def)] +#[expect(clippy::crate_in_macro_def)] macro_rules! print_macro ( // The non-continuation cases (most of them, e.g. `INFO`). ($format_string:path, false, $($arg:tt)+) =3D> ( diff --git a/rust/kernel/std_vendor.rs b/rust/kernel/std_vendor.rs index d59e4cf4b252..8b4872b48e97 100644 --- a/rust/kernel/std_vendor.rs +++ b/rust/kernel/std_vendor.rs @@ -16,7 +16,7 @@ /// /// ```rust /// let a =3D 2; -/// # #[allow(clippy::disallowed_macros)] +/// # #[expect(clippy::disallowed_macros)] /// let b =3D dbg!(a * 2) + 1; /// // ^-- prints: [src/main.rs:2] a * 2 =3D 4 /// assert_eq!(b, 5); @@ -54,7 +54,7 @@ /// With a method call: /// /// ```rust -/// # #[allow(clippy::disallowed_macros)] +/// # #[expect(clippy::disallowed_macros)] /// fn foo(n: usize) { /// if dbg!(n.checked_sub(4)).is_some() { /// // ... @@ -73,7 +73,7 @@ /// Naive factorial implementation: /// /// ```rust -/// # #[allow(clippy::disallowed_macros)] +/// # #[expect(clippy::disallowed_macros)] /// # { /// fn factorial(n: u32) -> u32 { /// if dbg!(n <=3D 1) { @@ -120,7 +120,7 @@ /// a tuple (and return it, too): /// /// ``` -/// # #![allow(clippy::disallowed_macros)] +/// # #![expect(clippy::disallowed_macros)] /// assert_eq!(dbg!(1usize, 2u32), (1, 2)); /// ``` /// @@ -129,7 +129,7 @@ /// invocations. You can use a 1-tuple directly if you need one: /// /// ``` -/// # #[allow(clippy::disallowed_macros)] +/// # #[expect(clippy::disallowed_macros)] /// # { /// assert_eq!(1, dbg!(1u32,)); // trailing comma ignored /// assert_eq!((1,), dbg!((1u32,))); // 1-tuple diff --git a/samples/rust/rust_print.rs b/samples/rust/rust_print.rs index ed1137ab2018..ba1606bdbd75 100644 --- a/samples/rust/rust_print.rs +++ b/samples/rust/rust_print.rs @@ -15,7 +15,7 @@ =20 struct RustPrint; =20 -#[allow(clippy::disallowed_macros)] +#[expect(clippy::disallowed_macros)] fn arc_print() -> Result { use kernel::sync::*; =20 diff --git a/scripts/Makefile.build b/scripts/Makefile.build index 72b1232b1f7d..0910f6390c93 100644 --- a/scripts/Makefile.build +++ b/scripts/Makefile.build @@ -263,7 +263,7 @@ $(obj)/%.lst: $(obj)/%.c FORCE # Compile Rust sources (.rs) # ------------------------------------------------------------------------= --- =20 -rust_allowed_features :=3D new_uninit +rust_allowed_features :=3D lint_reasons,new_uninit =20 # `--out-dir` is required to avoid temporaries being created by `rustc` in= the # current working directory, which may be not accessible in the out-of-tree --=20 2.46.0 From nobody Mon Dec 15 21:26:25 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 16C0618D64D; Wed, 4 Sep 2024 20:45:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725482749; cv=none; b=Mh/4uaNEzBs3XSvnEFDr82puo0P0CwnPRpQBsTUUdrtd96QrvibRKHN0DxoJi5Kvt7BMvDAfP0+rOda/Mbwp2aDTfw6BZd3PF6zrKyE5YxvvAlDjB9Hd48xmum68q0HDeyi7ULev3msMnEjRJrFx1/9ghrvufq9CCLbogKSx3ls= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725482749; c=relaxed/simple; bh=U6hGx73rC3AYOaz/2v00ONvDNaSLR3ygbElu0yUb9Ak=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=YfBIgk4kLdjPJpA5L6vecM6C3uaWJV9304PXQ6GTHkJrOmPeHU2ghmhgZxBvTcgDXNQUP+W6q6C3qFO/oa7zZnJjbD3Je6zemI8vlJ7aC4ae3dkz6bL04+s4u7hLAQWMGtyJiJqgmby5vNobKJQQJEA/lJzO04eqXGFaP4bUtBk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=JprDo5FW; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="JprDo5FW" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 846B8C4CEC5; Wed, 4 Sep 2024 20:45:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1725482748; bh=U6hGx73rC3AYOaz/2v00ONvDNaSLR3ygbElu0yUb9Ak=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JprDo5FWn/tiTsXP1EeXBfFqsW00Qa7PjpdmAEvcSSfIx/UZoqqpCBI0sTTvqcyTe gVQDwQeZV21Q94kuatc56B1WJ8m6/LEMVEAFjU5pYLWEV8O4S5CkTG/F/wXTDW7VVl 6jGtr/gMXbndR679UxxmLxcWqzId/YT0KDXKQBcYsp13UWNaDZjxVJBkhPNPvyIq9q TK1K53ufQtbOpA2BcG4IIL3XKzPGMO5TskkDTAKnX1/54IMl0F8mjP+3vzVzVPhDl4 DBxn2cNID1qKb8o1JRRSIB7H4+GEfRMoyWu6zB+qqDa0hPMJ0GSQ48XcsSFDM/2S/Z PKoUeCVSTLNMg== From: Miguel Ojeda To: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho Cc: Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev Subject: [PATCH 18/19] Documentation: rust: discuss `#[expect(...)]` in the guidelines Date: Wed, 4 Sep 2024 22:43:46 +0200 Message-ID: <20240904204347.168520-19-ojeda@kernel.org> In-Reply-To: <20240904204347.168520-1-ojeda@kernel.org> References: <20240904204347.168520-1-ojeda@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Discuss `#[expect(...)]` in the Lints sections of the coding guidelines document, which is an upcoming feature in Rust 1.81.0, and explain that it is generally to be preferred over `allow` unless there is a reason not to use it (e.g. conditional compilation being involved). Signed-off-by: Miguel Ojeda Reviewed-by: Gary Guo Tested-by: Gary Guo --- Documentation/rust/coding-guidelines.rst | 110 +++++++++++++++++++++++ 1 file changed, 110 insertions(+) diff --git a/Documentation/rust/coding-guidelines.rst b/Documentation/rust/= coding-guidelines.rst index 185d3b51117d..5d64bc69acae 100644 --- a/Documentation/rust/coding-guidelines.rst +++ b/Documentation/rust/coding-guidelines.rst @@ -256,3 +256,113 @@ By that virtue, it makes it possible to comfortably e= nable more diagnostics by default (i.e. outside ``W=3D`` levels). In particular, those that may have= some false positives but that are otherwise quite useful to keep enabled to cat= ch potential mistakes. + +On top of that, Rust provides the ``expect`` attribute which takes this fu= rther. +It makes the compiler warn if the warning was not produced. For instance, = the +following will ensure that, when ``f()`` is called somewhere, we will have= to +remove the attribute: + +.. code-block:: rust + + #[expect(dead_code)] + fn f() {} + +If we do not, we get a warning from the compiler:: + + warning: this lint expectation is unfulfilled + --> x.rs:3:10 + | + 3 | #[expect(dead_code)] + | ^^^^^^^^^ + | + =3D note: `#[warn(unfulfilled_lint_expectations)]` on by default + +This means that ``expect``\ s do not get forgotten when they are not neede= d, which +may happen in several situations, e.g.: + +- Temporary attributes added while developing. + +- Improvements in lints in the compiler, Clippy or custom tools which may + remove a false positive. + +- When the lint is not needed anymore because it was expected that it woul= d be + removed at some point, such as the ``dead_code`` example above. + +It also increases the visibility of the remaining ``allow``\ s and reduces= the +chance of misapplying one. + +Thus prefer ``except`` over ``allow`` unless: + +- The lint attribute is intended to be temporary, e.g. while developing. + +- Conditional compilation triggers the warning in some cases but not other= s. + + If there are only a few cases where the warning triggers (or does not + trigger) compared to the total number of cases, then one may consider us= ing + a conditional ``expect`` (i.e. ``cfg_attr(..., expect(...))``). Otherwis= e, + it is likely simpler to just use ``allow``. + +- Inside macros, when the different invocations may create expanded code t= hat + triggers the warning in some cases but not in others. + +- When code may trigger a warning for some architectures but not others, s= uch + as an ``as`` cast to a C FFI type. + +As a more developed example, consider for instance this program: + +.. code-block:: rust + + fn g() {} + + fn main() { + #[cfg(CONFIG_X)] + g(); + } + +Here, function ``g()`` is dead code if ``CONFIG_X`` is not set. Can we use +``expect`` here? + +.. code-block:: rust + + #[expect(dead_code)] + fn g() {} + + fn main() { + #[cfg(CONFIG_X)] + g(); + } + +This would emit a lint if ``CONFIG_X`` is set, since it is not dead code i= n that +configuration. Therefore, in cases like this, we cannot use ``expect`` as-= is. + +A simple possibility is using ``allow``: + +.. code-block:: rust + + #[allow(dead_code)] + fn g() {} + + fn main() { + #[cfg(CONFIG_X)] + g(); + } + +An alternative would be using a conditional ``expect``: + +.. code-block:: rust + + #[cfg_attr(not(CONFIG_X), expect(dead_code))] + fn g() {} + + fn main() { + #[cfg(CONFIG_X)] + g(); + } + +This would ensure that, if someone introduces another call to ``g()`` some= where +(e.g. unconditionally), then it would be spotted that it is not dead code +anymore. However, the ``cfg_attr`` is more complex than a simple ``allow``. + +Therefore, it is likely that it is not worth using conditional ``expect``\= s when +more than one or two configurations are involved or when the lint may be +triggered due to non-local changes (such as ``dead_code``). --=20 2.46.0 From nobody Mon Dec 15 21:26:25 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 9E17218FDBB; Wed, 4 Sep 2024 20:45:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725482752; cv=none; b=j2V1qMu5czQhFu3TeQenjd6oaMyB3O5L3i6T1RuJPo8tXAaS7RFYNjs82/RGaV9xtt47h/LjsrVGRuEIlLPdLLbdSkNxc/Zju9sNJeGyrhU5eICkft67NOTp7wFcVuTR/976ramKhnZwbyOAMvaSVCmhl11Z76nwgWpcUdLZ7RM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725482752; c=relaxed/simple; bh=Gln31nQ0hbxwsmmxUuwdP/R/XMmAD97Munol/XkULzI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=cSZcb/Lsp8uli+KWmi5Hs1tLRHgKYxTULEFoZpCDlStIbKC4qeAZ9miX287WujF5/82bbfXtRglGWmI7a+ZY2bjf+JDVWPuS48vcdlfdNk4wWYz59FVZNP4eGMLKywAB3fqM/m8Vm4Gl9zb4uMnb10j6QDe6r8W1uJVeQEK3SF4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=jHAeA/I4; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="jHAeA/I4" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 322DEC4CEC2; Wed, 4 Sep 2024 20:45:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1725482752; bh=Gln31nQ0hbxwsmmxUuwdP/R/XMmAD97Munol/XkULzI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jHAeA/I4a0F8/1m82vsBGH9+5MMtsuNKp+oGEJSrZqL74/2oUVURo8CPeRh8gEUXh 14Lrta0V8zEGDfOXNyLUo73D+s0KXSCJ86utYnPWH7DSiiAvVeZkWpN/o1QaG+dSwU tUitEzhSDQDwYjW2P9JyJfeihZrB9MleJpW8IvXR0XUY/tpJ35DW6mi3/TkLUyS1/i VospzDdHRjUfg7uxtkiXgJBxg854FsHF2TwDD6Z9CALpde7tddBDpEz1fflZG1BO4G 4F9DmI/ag+XEy56C4+8hVPVBusY+0r3i+11yzgs88lEWkvqjL0CFB8LFhfIFt8qAXU 0m4DyT21aWhaQ== From: Miguel Ojeda To: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho Cc: Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev Subject: [PATCH 19/19] rust: std_vendor: simplify `{ .. macro! .. }` with inner attributes Date: Wed, 4 Sep 2024 22:43:47 +0200 Message-ID: <20240904204347.168520-20-ojeda@kernel.org> In-Reply-To: <20240904204347.168520-1-ojeda@kernel.org> References: <20240904204347.168520-1-ojeda@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" It is cleaner to have a single inner attribute rather than needing several hidden lines to wrap the macro invocations. Thus simplify them. Signed-off-by: Miguel Ojeda Reviewed-by: Alice Ryhl Reviewed-by: Gary Guo Reviewed-by: Vincenzo Palazzo Tested-by: Gary Guo --- rust/kernel/std_vendor.rs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/rust/kernel/std_vendor.rs b/rust/kernel/std_vendor.rs index 8b4872b48e97..8f27d447a7fc 100644 --- a/rust/kernel/std_vendor.rs +++ b/rust/kernel/std_vendor.rs @@ -73,8 +73,7 @@ /// Naive factorial implementation: /// /// ```rust -/// # #[expect(clippy::disallowed_macros)] -/// # { +/// # #![expect(clippy::disallowed_macros)] /// fn factorial(n: u32) -> u32 { /// if dbg!(n <=3D 1) { /// dbg!(1) @@ -84,7 +83,6 @@ /// } /// /// dbg!(factorial(4)); -/// # } /// ``` /// /// This prints to the kernel log: @@ -129,11 +127,9 @@ /// invocations. You can use a 1-tuple directly if you need one: /// /// ``` -/// # #[expect(clippy::disallowed_macros)] -/// # { +/// # #![expect(clippy::disallowed_macros)] /// assert_eq!(1, dbg!(1u32,)); // trailing comma ignored /// assert_eq!((1,), dbg!((1u32,))); // 1-tuple -/// # } /// ``` /// /// [`std::dbg`]: https://doc.rust-lang.org/std/macro.dbg.html --=20 2.46.0