Skip to content

Commit

Permalink
rust: replace clippy::dbg_macro with disallowed_macros
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-by: Gary Guo <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Miguel Ojeda <[email protected]>
  • Loading branch information
ojeda committed Oct 7, 2024
1 parent d60a639 commit 7b95f6d
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 6 deletions.
6 changes: 6 additions & 0 deletions .clippy.toml
Original file line number Diff line number Diff line change
@@ -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" },
]
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
10 changes: 5 additions & 5 deletions rust/kernel/std_vendor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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() {
/// // ...
Expand All @@ -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) {
Expand Down Expand Up @@ -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));
/// ```
///
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions samples/rust/rust_print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module! {

struct RustPrint;

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

Expand Down

0 comments on commit 7b95f6d

Please sign in to comment.