rustc recommends wrapping in Some instead of removing extra reference
Open
@xizheyin is already working on this.
Since Aug 20, 2025.
A-diagnostics
D-incorrect
D-invalid-suggestion
T-compiler
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
Code
fn main() {
let foo = Some(&(1, 2));
assert!(matches!(foo, &Some((1, 2))))
}
Current output
--> src/main.rs:3:27
|
3 | assert!(matches!(foo, &Some((1, 2))))
| --- ^^^^^^^^^^^^^ expected `Option<&({integer}, {integer})>`, found `&_`
| |
| this expression has type `Option<&({integer}, {integer})>`
|
= note: expected enum `Option<&({integer}, {integer})>`
found reference `&_`
help: try wrapping the pattern in `Some`
|
3 | assert!(matches!(foo, Some(&Some((1, 2)))))
| +++++ +
For more information about this error, try `rustc --explain E0308`.
Desired output
--> src/main.rs:3:27
|
3 | assert!(matches!(foo, &Some((1, 2))))
| --- ^^^^^^^^^^^^^ expected `Option<&({integer}, {integer})>`, found `&_`
| |
| this expression has type `Option<&({integer}, {integer})>`
|
= note: expected enum `Option<&({integer}, {integer})>`
found reference `&Option<_>`
help: try moving the borrow
|
3 | assert!(matches!(foo, &Some(&(1, 2)))))
| - +
For more information about this error, try `rustc --explain E0308`.
Rationale and extra context
No response
Other cases
Rust Version
1.91.0-nightly (2025-08-18 9eb4a2652031ed5ba97c)
Anything else?
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.