Skip to contentSkip to conversation

rust: allow `clippy::unwrap_or_default` globally

PATCH Applied Alexandre Courbot 2 participants 3 messages Jul 8 – Jul 9

Subsystems (2)
lore
Share & tools
lore

Alexandre Courbot

author reviewer
#0 lore raw

PATCH v2 1/1 rust: allow `clippy::unwrap_or_default` globally

Starting with rustc 1.88, the clippy::unwrap_or_default lint triggers on rust/kernel/soc.rs if CONFIG_CC_OPTIMIZE_FOR_SIZE=y:

    warning: use of `unwrap_or` to construct default value
      --> ../rust/kernel/soc.rs:66:10
      |
   66 |         .unwrap_or(core::ptr::null())
      |          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_default()`

This is a clippy bug [1]: the lint decides whether an expression is equivalent to Default::default() by inspecting the optimized MIR of <*const T as Default>::default exported by core, so its outcome depends on the optimization level core was built with. Moreover, its suggestion ignores our MSRV of 1.85 (Default for *const T is only stable since Rust 1.88), so we could not apply it anyway.

Disable the lint globally rather than working around this single occurrence; it can be re-enabled conditionally using rustc-min-version once clippy is fixed.

---
Changes in v2:
- Disable the clippy::unwrap_or_default lint globally instead of
allowing it on the site triggering the issue.
- Link to v1: https://patch.msgid.link/[email protected]

To: Nathan Chancellor <[email protected]>
To: Nicolas Schier <[email protected]>
To: Miguel Ojeda <[email protected]>
To: Boqun Feng <[email protected]>
To: Gary Guo <[email protected]>
To: Björn Roy Baron <[email protected]>
To: Benno Lossin <[email protected]>
To: Andreas Hindborg <[email protected]>
To: Alice Ryhl <[email protected]>
To: Trevor Gross <[email protected]>
To: Danilo Krummrich <[email protected]>
To: Daniel Almeida <[email protected]>
To: Tamir Duberstein <[email protected]>
To: Alexandre Courbot <[email protected]>
To: Onur Özkan <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]

---
 Makefile | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
Makefile +6 -1
index b9c5792c79e0..22be861c5e7d 100644
@@ -863,8 +863,13 @@ export WARN_ON_UNUSED_TRACEPOINTS
 # include bitmasking and shift operations. However, because it generated
 # many hits, in Rust 1.86.0 it was split into a new `precedence_bits`
 # lint which is not enabled by default.
+#
+# `-Aclippy::unwrap_or_default`: the lint is buggy [1] and ignores our
+# MSRV. It can trigger depending on the optimization level.
+# [1] https://github.com/rust-lang/rust-clippy/issues/17379
 rust_common_flags_per_version := \
-    $(if $(call rustc-min-version,108600),,-Aclippy::precedence)
+    $(if $(call rustc-min-version,108600),,-Aclippy::precedence) \
+    -Aclippy::unwrap_or_default
 
 rust_common_flags += $(rust_common_flags_per_version)
 KBUILD_HOSTRUSTFLAGS += $(rust_common_flags_per_version) $(HOSTRUSTFLAGS)

---
base-commit: dc59e4fea9d83f03bad6bddf3fa2e52491777482
change-id: 20260707-soc_unwrap_or-ed37e674a759

Best regards,

Signature · 1 line
Alexandre Courbot <[email protected]>
#1 lore raw

Makefile export WARN_ON_UNUSED_TRACEPOINTS · line 872

Quoted 8 lines · Alexandre Courbot
Makefile +5 -1
+# `-Aclippy::unwrap_or_default`: the lint is buggy [1] and ignores our
+# MSRV. It can trigger depending on the optimization level.
+# [1] https://github.com/rust-lang/rust-clippy/issues/17379
 rust_common_flags_per_version := \
-    $(if $(call rustc-min-version,108600),,-Aclippy::precedence)
+    $(if $(call rustc-min-version,108600),,-Aclippy::precedence) \
+    -Aclippy::unwrap_or_default

I guess it may be per-version later on (i.e. when they fix it), but for now, it can go into the main list above -- I can move it when I apply it.

Thanks for the patch!

Cheers,
Miguel

Applied
#2 lore raw
In reply to Alexandre Courbot
Quoted 17 lines · Alexandre Courbot

Starting with rustc 1.88, the clippy::unwrap_or_default lint triggers on rust/kernel/soc.rs if CONFIG_CC_OPTIMIZE_FOR_SIZE=y:

    warning: use of `unwrap_or` to construct default value
      --> ../rust/kernel/soc.rs:66:10
      |
   66 |         .unwrap_or(core::ptr::null())
      |          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_default()`

This is a clippy bug [1]: the lint decides whether an expression is equivalent to Default::default() by inspecting the optimized MIR of <*const T as Default>::default exported by core, so its outcome depends on the optimization level core was built with. Moreover, its suggestion ignores our MSRV of 1.85 (Default for *const T is only stable since Rust 1.88), so we could not apply it anyway.

Quoted 7 lines

Disable the lint globally rather than working around this single occurrence; it can be re-enabled conditionally using rustc-min-version once clippy is fixed.

Applied to rust-fixes -- thanks!

Cc: [email protected] # Needed in 6.12.y and later (Rust is
pinned in older LTSs).

[ Moved to non-versioned group. - Miguel ]

Cheers,
Miguel