Skip to content

rustc recommends wrapping in Some instead of removing extra reference #145634

@lolbinarycat

Description

@lolbinarycat

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

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsD-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions