Skip to contentSkip to conversation

rust: sync: add #[must_use] to GlobalGuard and GlobalLock::try_lock

PATCH Superseded Ashutosh Desai 2 participants 2 messages Apr 19 – Apr 27

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rust: sync: add #[must_use] to GlobalGuard and GlobalLock::try_lock

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.

---
 rust/kernel/sync/lock/global.rs | 3 +++
 1 file changed, 3 insertions(+)
rust/kernel/sync/lock/global.rs +3 -0
index aecbdc34738f..7067bdcca9c2 100644
@@ -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>,
 }
Signature · 1 line
2.34.1
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 19 lines · 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.

---
 rust/kernel/sync/lock/global.rs | 3 +++
 1 file changed, 3 insertions(+)
rust/kernel/sync/lock/global.rs +0 -0
index aecbdc34738f..7067bdcca9c2 100644

rust/kernel/sync/lock/global.rs pub fn lock(&'static self) -> GlobalGuard<B> { · line 89

Quoted 5 lines
rust/kernel/sync/lock/global.rs +2 -0
@@ -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"]

I don't think there's any need for this comment.

Otherwise LGTM.

Quoted 14 lines
rust/kernel/sync/lock/global.rs +1 -0
     #[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>,
 }
Signature · 1 line
2.34.1