rust-lang / rust-lang/rust

Suggest to deref/reborrow to turn scrutinee into the right type for pattern

Open
#131,301 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
use std::cell::RefCell;

struct Foo {
    content: RefCell::<Option<u64>>,
}

fn main() {
    let foo = Foo {
        content: RefCell::new(Some(5)),
    };

    if let Some(_content) = foo.content.borrow_mut() {
    };
}
Current output
error[E0308]: mismatched types
  --> src/main.rs:12:12
   |
12 |     if let Some(_content) = foo.content.borrow_mut() {
   |            ^^^^^^^^^^^^^^   ------------------------ this expression has type `RefMut<'_, Option<u64>>`
   |            |
   |            expected `RefMut<'_, Option<u64>>`, found `Option<_>`
   |
   = note: expected struct `RefMut<'_, Option<u64>, >`
                found enum `Option<_>`

For more information about this error, try `rustc --explain E0308`.
error: could not compile `check_ice` (bin "check_ice") due to 1 previous error
Desired output

It can suggest to deref the RefMut:

if let Some(_content) = *foo.content.borrow_mut()
Rationale and extra context

No response

Other cases

No response

Rust Version
rustc 1.83.0-nightly (7042c269c 2024-09-23)
binary: rustc
commit-hash: 7042c269c166191cd5d8daf0409890903df7af57
commit-date: 2024-09-23
host: x86_64-unknown-linux-gnu
release: 1.83.0-nightly
LLVM version: 19.1.0
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.

Research direction

Start by reproducing the diagnostic from the example in src/main.rs with the reported rustc version and inspect the E0308 diagnostic path. Add a dereference or reborrow suggestion for this RefMut/Option pattern mismatch, then verify that the output includes the suggested *foo.content.borrow_mut() form.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.