]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
rust: sync: add #[must_use] to GlobalGuard and GlobalLock::try_lock
authorAshutosh Desai <ashutoshdesai993@gmail.com>
Sat, 2 May 2026 16:00:57 +0000 (16:00 +0000)
committerMiguel Ojeda <ojeda@kernel.org>
Thu, 4 Jun 2026 20:22:33 +0000 (22:22 +0200)
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.

Reviewed-by: Alice Ryhl <aliceryhl@google.com>
Signed-off-by: Ashutosh Desai <ashutoshdesai993@gmail.com>
Reviewed-by: Gary Guo <gary@garyguo.net>
Link: https://patch.msgid.link/20260502160057.3402896-1-ashutoshdesai993@gmail.com
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
rust/kernel/sync/lock/global.rs

index aecbdc34738fbd746de79eb0f1abe995478ad785..ec2dd84316fc596e95584b39c7878d3ea4c8e7d0 100644 (file)
@@ -85,6 +85,7 @@ impl<B: GlobalLockBackend> GlobalLock<B> {
     }
 
     /// Try to lock this global lock.
+    #[must_use = "if unused, the lock will be immediately unlocked"]
     #[inline]
     pub fn try_lock(&'static self) -> Option<GlobalGuard<B>> {
         Some(GlobalGuard {
@@ -96,6 +97,7 @@ impl<B: GlobalLockBackend> GlobalLock<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>,
 }