public inbox for rust-for-linux@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] rust: sync: add #[must_use] to Lock::try_lock
@ 2024-12-08 18:48 Jason Devers
  2024-12-08 19:11 ` Boqun Feng
  2024-12-09  7:11 ` Alice Ryhl
  0 siblings, 2 replies; 23+ messages in thread
From: Jason Devers @ 2024-12-08 18:48 UTC (permalink / raw)
  To: Miguel Ojeda, Alex Gaynor
  Cc: Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
	Andreas Hindborg, Alice Ryhl, Trevor Gross, rust-for-linux,
	Jason Devers, Alice Ryhl

The `Lock::try_lock` function returns an `Option<Guard<...>>`, but it
currently does not issue a warning if the return value is unused. This
could result in the lock being unlocked immediately, which is unsafe
and unintended. This patch adds a `#[must_use]` annotation to
`Lock::try_lock` to prevent this.

Suggested-by: Alice Ryhl <alice@ryhl.io>
Link: https://github.com/Rust-for-Linux/linux/issues/1133
Signed-off-by: Jason Devers <dev.json2@gmail.com>
---
 rust/kernel/sync/lock.rs | 1 +
 1 file changed, 1 insertion(+)

diff --git a/rust/kernel/sync/lock.rs b/rust/kernel/sync/lock.rs
index 41dcddac69e2..f6a5e83ff685 100644
--- a/rust/kernel/sync/lock.rs
+++ b/rust/kernel/sync/lock.rs
@@ -147,6 +147,7 @@ pub fn lock(&self) -> Guard<'_, T, B> {
     /// Tries to acquire the lock.
     ///
     /// Returns a guard that can be used to access the data protected by the lock if successful.
+    #[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.
-- 
2.44.2


^ permalink raw reply related	[flat|nested] 23+ messages in thread

end of thread, other threads:[~2025-01-15 18:12 UTC | newest]

Thread overview: 23+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-12-08 18:48 [PATCH] rust: sync: add #[must_use] to Lock::try_lock Jason Devers
2024-12-08 19:11 ` Boqun Feng
     [not found]   ` <CAOJvc-bi6ym6ZMAbDSh4H_L6LQXoTs8oNY07SZ1crzXxY9BodQ@mail.gmail.com>
2024-12-08 19:43     ` jos
2024-12-08 20:02     ` Boqun Feng
2024-12-08 20:50       ` Jason Devers
2024-12-08 21:57         ` Tamir Duberstein
2024-12-08 22:09           ` jos
2024-12-08 22:26             ` Jason Devers
2024-12-09 11:57               ` Miguel Ojeda
2024-12-09 17:09                 ` jos
2024-12-11  2:05             ` [PATCH v4] " Jason Devers
2024-12-11  9:17               ` Alice Ryhl
2024-12-11  9:45               ` Miguel Ojeda
2024-12-11 17:05                 ` jos
2024-12-11 18:57                   ` Boqun Feng
2024-12-12  3:11             ` [PATCH v5] " Jason Devers
2024-12-12 11:25               ` Miguel Ojeda
2024-12-12 15:47             ` [PATCH v6] " Jason Devers
2024-12-15 20:05               ` Boqun Feng
2025-01-09 13:05               ` Alice Ryhl
2025-01-15 18:12                 ` Boqun Feng
2024-12-08 21:28     ` [PATCH] " Miguel Ojeda
2024-12-09  7:11 ` Alice Ryhl

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox