You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The question_mark lint suggests incorrect code when the return type is behind Deref. I first noticed this when using parking_lot::Mutex<Option<_>>::lock() which returns a MutexGuard<'_, RawMutex, Option<_>>. Specifically, it forgets to include the parentheses to dereference the type before applying the question mark operator.
use parking_lot::Mutex;fnmain(){let a = Mutex::new(Some(0));println!("{:?}", format_a(a));}fnformat_a(a:Mutex<Option<u32>>) -> Option<String>{letSome(a) = *a.lock()else{returnNone;};Some(format!("{}", a))}
I expected to see this happen:
warning: this `let...else` may be rewritten with the `?` operator
--> src/main.rs:9:5
|
9 | / let Some(a) = *a.lock() else {
10 | | return None;
11 | | };
| |______^ help: replace it with: `let a = (*a.lock())?;`
Instead, this happened:
warning: this `let...else` may be rewritten with the `?` operator
--> src/main.rs:9:5
|
9 | / let Some(a) = *a.lock() else {
10 | | return None;
11 | | };
| |______^ help: replace it with: `let a = *a.lock()?;`
error[E0277]: the `?` operator can only be applied to values that implement `Try`
--> src/main.rs:9:14
|
9 | let a = *a.lock()?;
| ^^^^^^^^^ the `?` operator cannot be applied to type `parking_lot::lock_api::MutexGuard<'_, parking_lot::RawMutex, Option<u32>>`
|
= help: the trait `Try` is not implemented for `parking_lot::lock_api::MutexGuard<'_, parking_lot::RawMutex, Option<u32>>`
Summary
The question_mark lint suggests incorrect code when the return type is behind Deref. I first noticed this when using
parking_lot::Mutex<Option<_>>::lock()
which returns aMutexGuard<'_, RawMutex, Option<_>>
. Specifically, it forgets to include the parentheses to dereference the type before applying the question mark operator.Here is a minimal reproduction in rust playground:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=1d714214fc7983d0785b99ffd7d3eab4
Thanks!
Reproducer
I tried this code:
I expected to see this happen:
Instead, this happened:
Version
Additional Labels
No response
The text was updated successfully, but these errors were encountered: