Skip to content

Commit 3f2880c

Browse files
hz2intel-lab-lkp
authored andcommitted
rust: sync: add #[must_use] to Lock::try_lock
The `Lock::try_lock` function returns an `Option<Guard<...>>`, but it currently does not issue a warning if the return value is unused. To avoid potential bugs, the `#[must_use]` annotation is added to ensure proper usage. Note that `T` is `#[must_use]` but `Option<T>` is not. For more context, see: rust-lang/rust#71368. Suggested-by: Alice Ryhl <[email protected]> Link: Rust-for-Linux#1133 Signed-off-by: Jason Devers <[email protected]>
1 parent fac04ef commit 3f2880c

File tree

1 file changed

+2
-0
lines changed

1 file changed

+2
-0
lines changed

rust/kernel/sync/lock.rs

+2
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ impl<T: ?Sized, B: Backend> Lock<T, B> {
147147
/// Tries to acquire the lock.
148148
///
149149
/// Returns a guard that can be used to access the data protected by the lock if successful.
150+
// Note that `T` is `#[must_use]` but `Option<T>` is not.
151+
#[must_use = "if unused, the lock will be immediately unlocked"]
150152
pub fn try_lock(&self) -> Option<Guard<'_, T, B>> {
151153
// SAFETY: The constructor of the type calls `init`, so the existence of the object proves
152154
// that `init` was called.

0 commit comments

Comments
 (0)