public inbox for stable@vger.kernel.org
 help / color / mirror / Atom feed
From: Miguel Ojeda <ojeda@kernel.org>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Sasha Levin <sashal@kernel.org>,
	stable@vger.kernel.org
Cc: Danilo Krummrich <dakr@kernel.org>,
	Alice Ryhl <aliceryhl@google.com>, Alyssa Ross <hi@alyssa.is>,
	NoisyCoil <noisycoil@disroot.org>,
	patches@lists.linux.dev, Miguel Ojeda <ojeda@kernel.org>
Subject: [PATCH 6.12.y 12/60] rust: replace `clippy::dbg_macro` with `disallowed_macros`
Date: Fri,  7 Mar 2025 23:49:19 +0100	[thread overview]
Message-ID: <20250307225008.779961-13-ojeda@kernel.org> (raw)
In-Reply-To: <20250307225008.779961-1-ojeda@kernel.org>

commit 8577c9dca799bd74377f7c30015d8cdc53a53ca2 upstream.

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]
Tested-by: Gary Guo <gary@garyguo.net>
Reviewed-by: Gary Guo <gary@garyguo.net>
Link: https://lore.kernel.org/r/20240904204347.168520-13-ojeda@kernel.org
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
---
 .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 = [
+    # The `clippy::dbg_macro` lint only works with `std::dbg!`, thus we simulate
+    # it here, see: https://github.com/rust-lang/rust-clippy/issues/11303.
+    { path = "kernel::dbg", reason = "the `dbg!` macro is intended as a debugging tool" },
+]
diff --git a/Makefile b/Makefile
index 43cc17c514dc..d005a439a67c 100644
--- a/Makefile
+++ b/Makefile
@@ -452,7 +452,6 @@ export rust_common_flags := --edition=2021 \
 			    -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 = 2;
-/// # #[allow(clippy::dbg_macro)]
+/// # #[allow(clippy::disallowed_macros)]
 /// let b = dbg!(a * 2) + 1;
 /// //      ^-- prints: [src/main.rs:2] a * 2 = 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 <= 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 @@
 
 struct RustPrint;
 
+#[allow(clippy::disallowed_macros)]
 fn arc_print() -> Result {
     use kernel::sync::*;
 
-- 
2.48.1


  parent reply	other threads:[~2025-03-07 22:51 UTC|newest]

Thread overview: 72+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-07 22:49 [PATCH 6.12.y 00/60] `alloc`, `#[expect]` and "Custom FFI" Miguel Ojeda
2025-03-07 22:49 ` [PATCH 6.12.y 01/60] rust: workqueue: remove unneeded ``#[allow(clippy::new_ret_no_self)]` Miguel Ojeda
2025-03-07 22:49 ` [PATCH 6.12.y 02/60] rust: sort global Rust flags Miguel Ojeda
2025-03-07 22:49 ` [PATCH 6.12.y 03/60] rust: types: avoid repetition in `{As,From}Bytes` impls Miguel Ojeda
2025-03-07 22:49 ` [PATCH 6.12.y 04/60] rust: enable `clippy::undocumented_unsafe_blocks` lint Miguel Ojeda
2025-03-07 22:49 ` [PATCH 6.12.y 05/60] rust: enable `clippy::unnecessary_safety_comment` lint Miguel Ojeda
2025-03-07 22:49 ` [PATCH 6.12.y 06/60] rust: enable `clippy::unnecessary_safety_doc` lint Miguel Ojeda
2025-03-07 22:49 ` [PATCH 6.12.y 07/60] rust: enable `clippy::ignored_unit_patterns` lint Miguel Ojeda
2025-03-07 22:49 ` [PATCH 6.12.y 08/60] rust: enable `rustdoc::unescaped_backticks` lint Miguel Ojeda
2025-03-07 22:49 ` [PATCH 6.12.y 09/60] rust: init: remove unneeded `#[allow(clippy::disallowed_names)]` Miguel Ojeda
2025-03-07 22:49 ` [PATCH 6.12.y 10/60] rust: sync: remove unneeded `#[allow(clippy::non_send_fields_in_send_ty)]` Miguel Ojeda
2025-03-07 22:49 ` [PATCH 6.12.y 11/60] rust: introduce `.clippy.toml` Miguel Ojeda
2025-03-07 22:49 ` Miguel Ojeda [this message]
2025-03-07 22:49 ` [PATCH 6.12.y 13/60] rust: provide proper code documentation titles Miguel Ojeda
2025-03-07 22:49 ` [PATCH 6.12.y 14/60] rust: enable Clippy's `check-private-items` Miguel Ojeda
2025-03-07 22:49 ` [PATCH 6.12.y 15/60] Documentation: rust: add coding guidelines on lints Miguel Ojeda
2025-03-07 22:49 ` [PATCH 6.12.y 16/60] rust: start using the `#[expect(...)]` attribute Miguel Ojeda
2025-03-07 22:49 ` [PATCH 6.12.y 17/60] Documentation: rust: discuss `#[expect(...)]` in the guidelines Miguel Ojeda
2025-03-07 22:49 ` [PATCH 6.12.y 18/60] rust: error: make conversion functions public Miguel Ojeda
2025-03-07 22:49 ` [PATCH 6.12.y 19/60] rust: error: optimize error type to use nonzero Miguel Ojeda
2025-03-07 22:49 ` [PATCH 6.12.y 20/60] rust: alloc: add `Allocator` trait Miguel Ojeda
2025-03-07 22:49 ` [PATCH 6.12.y 21/60] rust: alloc: separate `aligned_size` from `krealloc_aligned` Miguel Ojeda
2025-03-07 22:49 ` [PATCH 6.12.y 22/60] rust: alloc: rename `KernelAllocator` to `Kmalloc` Miguel Ojeda
2025-03-07 22:49 ` [PATCH 6.12.y 23/60] rust: alloc: implement `ReallocFunc` Miguel Ojeda
2025-03-07 22:49 ` [PATCH 6.12.y 24/60] rust: alloc: make `allocator` module public Miguel Ojeda
2025-03-07 22:49 ` [PATCH 6.12.y 25/60] rust: alloc: implement `Allocator` for `Kmalloc` Miguel Ojeda
2025-03-07 22:49 ` [PATCH 6.12.y 26/60] rust: alloc: add module `allocator_test` Miguel Ojeda
2025-03-07 22:49 ` [PATCH 6.12.y 27/60] rust: alloc: implement `Vmalloc` allocator Miguel Ojeda
2025-03-07 22:49 ` [PATCH 6.12.y 28/60] rust: alloc: implement `KVmalloc` allocator Miguel Ojeda
2025-03-07 22:49 ` [PATCH 6.12.y 29/60] rust: alloc: add __GFP_NOWARN to `Flags` Miguel Ojeda
2025-03-07 22:49 ` [PATCH 6.12.y 30/60] rust: alloc: implement kernel `Box` Miguel Ojeda
2025-03-07 22:49 ` [PATCH 6.12.y 31/60] rust: treewide: switch to our kernel `Box` type Miguel Ojeda
2025-03-07 22:49 ` [PATCH 6.12.y 32/60] rust: alloc: remove extension of std's `Box` Miguel Ojeda
2025-03-07 22:49 ` [PATCH 6.12.y 33/60] rust: alloc: add `Box` to prelude Miguel Ojeda
2025-03-07 22:49 ` [PATCH 6.12.y 34/60] rust: alloc: introduce `ArrayLayout` Miguel Ojeda
2025-03-07 22:49 ` [PATCH 6.12.y 35/60] rust: alloc: implement kernel `Vec` type Miguel Ojeda
2025-03-07 22:49 ` [PATCH 6.12.y 36/60] rust: alloc: implement `IntoIterator` for `Vec` Miguel Ojeda
2025-03-07 22:49 ` [PATCH 6.12.y 37/60] rust: alloc: implement `collect` for `IntoIter` Miguel Ojeda
2025-03-07 22:49 ` [PATCH 6.12.y 38/60] rust: treewide: switch to the kernel `Vec` type Miguel Ojeda
2025-03-07 22:49 ` [PATCH 6.12.y 39/60] rust: alloc: remove `VecExt` extension Miguel Ojeda
2025-03-07 22:49 ` [PATCH 6.12.y 40/60] rust: alloc: add `Vec` to prelude Miguel Ojeda
2025-03-07 22:49 ` [PATCH 6.12.y 41/60] rust: error: use `core::alloc::LayoutError` Miguel Ojeda
2025-03-07 22:49 ` [PATCH 6.12.y 42/60] rust: error: check for config `test` in `Error::name` Miguel Ojeda
2025-03-07 22:49 ` [PATCH 6.12.y 43/60] rust: alloc: implement `contains` for `Flags` Miguel Ojeda
2025-03-07 22:49 ` [PATCH 6.12.y 44/60] rust: alloc: implement `Cmalloc` in module allocator_test Miguel Ojeda
2025-03-07 22:49 ` [PATCH 6.12.y 45/60] rust: str: test: replace `alloc::format` Miguel Ojeda
2025-03-07 22:49 ` [PATCH 6.12.y 46/60] rust: alloc: update module comment of alloc.rs Miguel Ojeda
2025-03-07 22:49 ` [PATCH 6.12.y 47/60] kbuild: rust: remove the `alloc` crate and `GlobalAlloc` Miguel Ojeda
2025-03-07 22:49 ` [PATCH 6.12.y 48/60] MAINTAINERS: add entry for the Rust `alloc` module Miguel Ojeda
2025-03-07 22:49 ` [PATCH 6.12.y 49/60] drm/panic: avoid reimplementing Iterator::find Miguel Ojeda
2025-03-07 22:49 ` [PATCH 6.12.y 50/60] drm/panic: remove unnecessary borrow in alignment_pattern Miguel Ojeda
2025-03-07 22:49 ` [PATCH 6.12.y 51/60] drm/panic: prefer eliding lifetimes Miguel Ojeda
2025-03-07 22:49 ` [PATCH 6.12.y 52/60] drm/panic: remove redundant field when assigning value Miguel Ojeda
2025-03-07 22:50 ` [PATCH 6.12.y 53/60] drm/panic: correctly indent continuation of line in list item Miguel Ojeda
2025-03-07 22:50 ` [PATCH 6.12.y 54/60] drm/panic: allow verbose boolean for clarity Miguel Ojeda
2025-03-07 22:50 ` [PATCH 6.12.y 55/60] drm/panic: allow verbose version check Miguel Ojeda
2025-03-07 22:50 ` [PATCH 6.12.y 56/60] rust: kbuild: expand rusttest target for macros Miguel Ojeda
2025-03-07 22:50 ` [PATCH 6.12.y 57/60] rust: fix size_t in bindgen prototypes of C builtins Miguel Ojeda
2025-03-07 22:50 ` [PATCH 6.12.y 58/60] rust: map `__kernel_size_t` and friends also to usize/isize Miguel Ojeda
2025-03-07 22:50 ` [PATCH 6.12.y 59/60] rust: use custom FFI integer types Miguel Ojeda
2025-03-07 22:50 ` [PATCH 6.12.y 60/60] rust: alloc: Fix `ArrayLayout` allocations Miguel Ojeda
2025-03-09  9:47 ` [PATCH 6.12.y 00/60] `alloc`, `#[expect]` and "Custom FFI" Greg Kroah-Hartman
2025-03-09 12:41 ` Ilya K
2025-03-09 14:20   ` Miguel Ojeda
2025-03-09 16:27     ` Greg Kroah-Hartman
2025-03-09 20:42       ` [PATCH 6.12.y 0/2] The two missing ones Miguel Ojeda
2025-03-09 20:42         ` [PATCH 6.12.y 1/2] rust: finish using custom FFI integer types Miguel Ojeda
2025-03-13  9:01           ` Sasha Levin
2025-03-09 20:42         ` [PATCH 6.12.y 2/2] rust: map `long` to `isize` and `char` to `u8` Miguel Ojeda
2025-03-13  9:01           ` Sasha Levin
2025-03-13 10:59             ` Miguel Ojeda
2025-03-09 21:02         ` [PATCH 6.12.y 0/2] The two missing ones Greg Kroah-Hartman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250307225008.779961-13-ojeda@kernel.org \
    --to=ojeda@kernel.org \
    --cc=aliceryhl@google.com \
    --cc=dakr@kernel.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=hi@alyssa.is \
    --cc=noisycoil@disroot.org \
    --cc=patches@lists.linux.dev \
    --cc=sashal@kernel.org \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox