Skip to contentSkip to conversation

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

PATCH Applied Ashutosh Desai 4 participants 5 messages May 2 – Jun 4

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v2 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.

---
Changes in v2:
- Remove explanatory comment above #[must_use] on try_lock (Alice Ryhl)

Link to v1: https://lore.kernel.org/rust-for-linux/[email protected]/

 rust/kernel/sync/lock/global.rs | 2 ++
 1 file changed, 2 insertions(+)
rust/kernel/sync/lock/global.rs +2 -0
index aecbdc34738f..ec2dd84316fc 100644
@@ -85,6 +85,7 @@ pub fn lock(&'static self) -> GlobalGuard<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 @@ 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 v2 1/1
#1 lore raw
Quoted 10 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.

Quoted 8 lines

---
Changes in v2:
- Remove explanatory comment above #[must_use] on try_lock (Alice Ryhl)

Link to v1: https://lore.kernel.org/rust-for-linux/[email protected]/

 rust/kernel/sync/lock/global.rs | 2 ++
 1 file changed, 2 insertions(+)
#2 lore raw
In reply to Ashutosh Desai
Quoted 11 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.

I could pick this one -- do you want me to, Boqun?

Cheers,
Miguel

maintainer
#3 lore raw
Quoted 7 lines · Miguel Ojeda
Quoted 5 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.

Quoted 9 lines
Quoted 6 lines

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.

I could pick this one -- do you want me to, Boqun?

Yes, please! Thank you!

Regards,
Boqun

Quoted 2 lines

Cheers,
Miguel

Applied
#4 lore raw
In reply to Ashutosh Desai
Quoted 11 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.

Applied to rust-next -- thanks everyone!

Cheers,
Miguel