rust-lang / rust-lang/rust

Option variable is not checked correctly

Open
#153,347 1 comment 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
fn main() -> Result<(), Box<dyn Error>> {
    
    let mut array = vec![String::from("jaja"), String::from("haha"), String::from("fafa")];

    let mut data_ptr: Option<&mut String> = None;

    let mut count = 100;
    while count > 0 {
         if data_ptr.is_none(){
            data_ptr = None;
            for item in array.iter_mut(){      
                if item .starts_with("f"){
                    data_ptr = Some(item);
                    break;
                }
            }
         
        }
        // .....
        if data_ptr.is_some(){
            println!("find {:?}", data_ptr);
            data_ptr = None;         
        }

        count -= 1;
    }
    Ok(())
}
Current output
if data_ptr.is_none(){
   |             -------- first borrow used here, in later iteration of loop
17 |             // data_ptr = None;
18 |             for item in array.iter_mut(){
   |                         ^^^^^ `array` was mutably borrowed here in the previous iteration of the loop
Desired output
it is None already, it has been checked by "data_ptr.is_none()". we have to bind it to None again to tell rustc, it is None.
Rationale and extra context

No response

Other cases
// data_ptr = None; 

mask this line, evertything is ok
Rust Version
rustc 1.93.0 (254b59607 2026-01-19)
binary: rustc
commit-hash: 254b59607d4417e9dffbc307138ae5c86280fe4c
commit-date: 2026-01-19
host: x86_64-pc-windows-msvc
release: 1.93.0
LLVM version: 21.1.8
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

Reproduce the supplied example with rustc 1.93.0 and compare the diagnostic with the reported current and desired output. Investigate the compiler's borrow-checking and diagnostic behavior for the mutable reference stored in Option; done means the issue is either explained with a precise limitation or the reported case receives an appropriate diagnostic change.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.