public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Alice Ryhl <aliceryhl@google.com>
To: Ashutosh Desai <ashutoshdesai993@gmail.com>
Cc: peterz@infradead.org, mingo@redhat.com, will@kernel.org,
	boqun@kernel.org,  ojeda@kernel.org, longman@redhat.com,
	gary@garyguo.net,  bjorn3_gh@protonmail.com, lossin@kernel.org,
	a.hindborg@kernel.org,  tmgross@umich.edu, dakr@kernel.org,
	linux-kernel@vger.kernel.org,  rust-for-linux@vger.kernel.org
Subject: Re: [PATCH] rust: sync: add #[must_use] to GlobalGuard and GlobalLock::try_lock
Date: Mon, 27 Apr 2026 07:29:48 +0000	[thread overview]
Message-ID: <ae8QbGQojU7CydGp@google.com> (raw)
In-Reply-To: <20260419001141.211935-1-ashutoshdesai993@gmail.com>

On Sun, Apr 19, 2026 at 12:11:41AM +0000, Ashutosh Desai wrote:
> Guard is marked #[must_use] since dropping it releases the lock. GlobalGuard
> wraps Guard with identical semantics but was missing the annotation, so
> discarding it would silently compile without warning.
> 
> Similarly, GlobalLock::try_lock was missing #[must_use]. Option<T> does not
> propagate #[must_use] from T, so the attribute needs to be on the function
> directly - same reason Lock::try_lock has it. Add the explanatory comment
> there too.
> 
> Signed-off-by: Ashutosh Desai <ashutoshdesai993@gmail.com>
> ---
>  rust/kernel/sync/lock/global.rs | 3 +++
>  1 file changed, 3 insertions(+)
> 
> diff --git a/rust/kernel/sync/lock/global.rs b/rust/kernel/sync/lock/global.rs
> index aecbdc34738f..7067bdcca9c2 100644
> --- a/rust/kernel/sync/lock/global.rs
> +++ b/rust/kernel/sync/lock/global.rs
> @@ -85,6 +85,8 @@ pub fn lock(&'static self) -> GlobalGuard<B> {
>      }
>  
>      /// Try to lock this global lock.
> +    // `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"]

I don't think there's any need for this comment.

Otherwise LGTM.
Reviewed-by: Alice Ryhl <aliceryhl@google.com>

>      #[inline]
>      pub fn try_lock(&'static self) -> Option<GlobalGuard<B>> {
>          Some(GlobalGuard {
> @@ -96,6 +98,7 @@ pub fn try_lock(&'static self) -> Option<GlobalGuard<B>> {
>  /// A guard for a [`GlobalLock`].
>  ///
>  /// See [`global_lock!`] for examples.
> +#[must_use = "the lock unlocks immediately when the guard is unused"]
>  pub struct GlobalGuard<B: GlobalLockBackend> {
>      inner: Guard<'static, B::Item, B::Backend>,
>  }
> -- 
> 2.34.1
> 

      reply	other threads:[~2026-04-27  7:29 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-19  0:11 [PATCH] rust: sync: add #[must_use] to GlobalGuard and GlobalLock::try_lock Ashutosh Desai
2026-04-27  7:29 ` Alice Ryhl [this message]

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=ae8QbGQojU7CydGp@google.com \
    --to=aliceryhl@google.com \
    --cc=a.hindborg@kernel.org \
    --cc=ashutoshdesai993@gmail.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun@kernel.org \
    --cc=dakr@kernel.org \
    --cc=gary@garyguo.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=longman@redhat.com \
    --cc=lossin@kernel.org \
    --cc=mingo@redhat.com \
    --cc=ojeda@kernel.org \
    --cc=peterz@infradead.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tmgross@umich.edu \
    --cc=will@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