public inbox for rust-for-linux@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] rust: sync: add #[must_use] to GlobalGuard and GlobalLock::try_lock
@ 2026-04-19  0:11 Ashutosh Desai
  0 siblings, 0 replies; only message in thread
From: Ashutosh Desai @ 2026-04-19  0:11 UTC (permalink / raw)
  To: peterz, mingo, will, boqun, ojeda
  Cc: longman, gary, bjorn3_gh, lossin, a.hindborg, aliceryhl, tmgross,
	dakr, linux-kernel, rust-for-linux, Ashutosh Desai

Guard is marked #[must_use] since dropping it releases the lock. GlobalGuard
wraps Guard with identical semantics but was missing the annotation, so
discarding it would silently compile without warning.

Similarly, GlobalLock::try_lock was missing #[must_use]. Option<T> does not
propagate #[must_use] from T, so the attribute needs to be on the function
directly - same reason Lock::try_lock has it. Add the explanatory comment
there too.

Signed-off-by: Ashutosh Desai <ashutoshdesai993@gmail.com>
---
 rust/kernel/sync/lock/global.rs | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/rust/kernel/sync/lock/global.rs b/rust/kernel/sync/lock/global.rs
index aecbdc34738f..7067bdcca9c2 100644
--- a/rust/kernel/sync/lock/global.rs
+++ b/rust/kernel/sync/lock/global.rs
@@ -85,6 +85,8 @@ pub fn lock(&'static self) -> GlobalGuard<B> {
     }
 
     /// Try to lock this global lock.
+    // `Option<T>` is not `#[must_use]` even if `T` is, thus the attribute is needed here.
+    #[must_use = "if unused, the lock will be immediately unlocked"]
     #[inline]
     pub fn try_lock(&'static self) -> Option<GlobalGuard<B>> {
         Some(GlobalGuard {
@@ -96,6 +98,7 @@ pub fn try_lock(&'static self) -> Option<GlobalGuard<B>> {
 /// A guard for a [`GlobalLock`].
 ///
 /// See [`global_lock!`] for examples.
+#[must_use = "the lock unlocks immediately when the guard is unused"]
 pub struct GlobalGuard<B: GlobalLockBackend> {
     inner: Guard<'static, B::Item, B::Backend>,
 }
-- 
2.34.1


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-04-19  0:11 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-19  0:11 [PATCH] rust: sync: add #[must_use] to GlobalGuard and GlobalLock::try_lock Ashutosh Desai

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox