From: "tip-bot2 for Jason Devers" <tip-bot2@linutronix.de>
To: linux-tip-commits@vger.kernel.org
Cc: Alice Ryhl <aliceryhl@google.com>,
Jason Devers <dev.json2@gmail.com>,
Boqun Feng <boqun.feng@gmail.com>,
x86@kernel.org, linux-kernel@vger.kernel.org
Subject: [tip: locking/core] rust: sync: Add #[must_use] to Lock::try_lock()
Date: Sat, 19 Jul 2025 17:40:14 -0000 [thread overview]
Message-ID: <175294681414.1420.16229564078269746655.tip-bot2@tip-bot2> (raw)
In-Reply-To: <20241212154753.139563-1-dev.json2@gmail.com>
The following commit has been merged into the locking/core branch of tip:
Commit-ID: 99214efedea521f1b79fa2a28ff142e933fc3eba
Gitweb: https://git.kernel.org/tip/99214efedea521f1b79fa2a28ff142e933fc3eba
Author: Jason Devers <dev.json2@gmail.com>
AuthorDate: Thu, 12 Dec 2024 10:47:53 -05:00
Committer: Boqun Feng <boqun.feng@gmail.com>
CommitterDate: Fri, 11 Jul 2025 15:11:54 -07:00
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: https://github.com/rust-lang/rust/issues/71368.
Suggested-by: Alice Ryhl <aliceryhl@google.com>
Link: https://github.com/Rust-for-Linux/linux/issues/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
---
rust/kernel/sync/lock.rs | 2 ++
1 file changed, 2 insertions(+)
diff --git a/rust/kernel/sync/lock.rs b/rust/kernel/sync/lock.rs
index e82fa5b..27202be 100644
--- a/rust/kernel/sync/lock.rs
+++ b/rust/kernel/sync/lock.rs
@@ -175,6 +175,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.
+ // `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"]
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.
parent reply other threads:[~2025-07-19 17:40 UTC|newest]
Thread overview: expand[flat|nested] mbox.gz Atom feed
[parent not found: <20241212154753.139563-1-dev.json2@gmail.com>]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=175294681414.1420.16229564078269746655.tip-bot2@tip-bot2 \
--to=tip-bot2@linutronix.de \
--cc=aliceryhl@google.com \
--cc=boqun.feng@gmail.com \
--cc=dev.json2@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-tip-commits@vger.kernel.org \
--cc=x86@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®