Skip to content

Commit

Permalink
rust: sync: add #[must_use] to Lock::try_lock
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
hz2 authored and intel-lab-lkp committed Dec 11, 2024
1 parent fac04ef commit ff42186
Showing 1 changed file with 2 additions and 0 deletions.
2 changes: 2 additions & 0 deletions rust/kernel/sync/lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ impl<T: ?Sized, B: Backend> Lock<T, B> {
/// Tries to acquire the lock.
///
/// Returns a guard that can be used to access the data protected by the lock if successful.
/// Note that `T` is `#[must_use]` but `Option<T>` is not.
#[must_use = "if unused, leads to an empty critical section"]
pub fn try_lock(&self) -> Option<Guard<'_, T, B>> {
// SAFETY: The constructor of the type calls `init`, so the existence of the object proves
// that `init` was called.
Expand Down

0 comments on commit ff42186

Please sign in to comment.