rust-lang / rust-lang/rust

rustc recommends wrapping in Some instead of removing extra reference

Open
#145,634 0 comments 0 reactions 1 assignee View on GitHub

@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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.