Skip to content

Commit 7b95f6d

Browse files
committed
rust: replace clippy::dbg_macro with disallowed_macros
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: rust-lang/rust-clippy#11303 [1] Link: rust-lang/rust-clippy#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>
1 parent d60a639 commit 7b95f6d

File tree

4 files changed

+12
-6
lines changed

4 files changed

+12
-6
lines changed

.clippy.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
11
# SPDX-License-Identifier: GPL-2.0
2+
3+
disallowed-macros = [
4+
# The `clippy::dbg_macro` lint only works with `std::dbg!`, thus we simulate
5+
# it here, see: https://github.com/rust-lang/rust-clippy/issues/11303.
6+
{ path = "kernel::dbg", reason = "the `dbg!` macro is intended as a debugging tool" },
7+
]

Makefile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,6 @@ export rust_common_flags := --edition=2021 \
452452
-Wrust_2018_idioms \
453453
-Wunreachable_pub \
454454
-Wclippy::all \
455-
-Wclippy::dbg_macro \
456455
-Wclippy::ignored_unit_patterns \
457456
-Wclippy::mut_mut \
458457
-Wclippy::needless_bitwise_bool \

rust/kernel/std_vendor.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
///
1515
/// ```rust
1616
/// let a = 2;
17-
/// # #[allow(clippy::dbg_macro)]
17+
/// # #[allow(clippy::disallowed_macros)]
1818
/// let b = dbg!(a * 2) + 1;
1919
/// // ^-- prints: [src/main.rs:2] a * 2 = 4
2020
/// assert_eq!(b, 5);
@@ -52,7 +52,7 @@
5252
/// With a method call:
5353
///
5454
/// ```rust
55-
/// # #[allow(clippy::dbg_macro)]
55+
/// # #[allow(clippy::disallowed_macros)]
5656
/// fn foo(n: usize) {
5757
/// if dbg!(n.checked_sub(4)).is_some() {
5858
/// // ...
@@ -71,7 +71,7 @@
7171
/// Naive factorial implementation:
7272
///
7373
/// ```rust
74-
/// # #[allow(clippy::dbg_macro)]
74+
/// # #[allow(clippy::disallowed_macros)]
7575
/// # {
7676
/// fn factorial(n: u32) -> u32 {
7777
/// if dbg!(n <= 1) {
@@ -118,7 +118,7 @@
118118
/// a tuple (and return it, too):
119119
///
120120
/// ```
121-
/// # #[allow(clippy::dbg_macro)]
121+
/// # #![allow(clippy::disallowed_macros)]
122122
/// assert_eq!(dbg!(1usize, 2u32), (1, 2));
123123
/// ```
124124
///
@@ -127,7 +127,7 @@
127127
/// invocations. You can use a 1-tuple directly if you need one:
128128
///
129129
/// ```
130-
/// # #[allow(clippy::dbg_macro)]
130+
/// # #[allow(clippy::disallowed_macros)]
131131
/// # {
132132
/// assert_eq!(1, dbg!(1u32,)); // trailing comma ignored
133133
/// assert_eq!((1,), dbg!((1u32,))); // 1-tuple

samples/rust/rust_print.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ module! {
1515

1616
struct RustPrint;
1717

18+
#[allow(clippy::disallowed_macros)]
1819
fn arc_print() -> Result {
1920
use kernel::sync::*;
2021

0 commit comments

Comments
 (0)