Skip to content

Commit 99214ef

Browse files
hz2fbq
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 <aliceryhl@google.com> Link: Rust-for-Linux/linux#1133 Signed-off-by: Jason Devers <dev.json2@gmail.com> Reviewed-by: Alice Ryhl <aliceryhl@google.com> Signed-off-by: Boqun Feng <boqun.feng@gmail.com> Link: https://lore.kernel.org/r/20241212154753.139563-1-dev.json2@gmail.com
1 parent daec29d commit 99214ef

File tree

1 file changed

+2
-0
lines changed

1 file changed

+2
-0
lines changed

rust/kernel/sync/lock.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ impl<T: ?Sized, B: Backend> Lock<T, B> {
175175
/// Tries to acquire the lock.
176176
///
177177
/// Returns a guard that can be used to access the data protected by the lock if successful.
178+
// `Option<T>` is not `#[must_use]` even if `T` is, thus the attribute is needed here.
179+
#[must_use = "if unused, the lock will be immediately unlocked"]
178180
pub fn try_lock(&self) -> Option<Guard<'_, T, B>> {
179181
// SAFETY: The constructor of the type calls `init`, so the existence of the object proves
180182
// that `init` was called.

0 commit comments

Comments
 (0)