rust-lang / rust-lang/rust-clippy

`option_if_let_else` suggested fix is invalidated by borrow checker

Open
#14,587 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-suggestion-causes-error
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Summary

No response

Reproducer

I tried this code:

    let metoame = tauste
        .iter()
        .filter(|(metoa, _, _)| {
            !metoa.is_empty() && !toadua.lines().any(|toa| toa == metoa.join(""))
        })
        .map(|(metoa, lujvo, def)| (metoa.join(""), lujvo, def))
        .map(|(metoa, lujvo, def)| (if let Some(t) = metoa.strip_prefix('\'') {t.to_string()}else{metoa}, lujvo, def))
        .collect_vec();

I expected to see this happen: Unknown

Instead, this happened:

warning: use Option::map_or instead of an if let/else
   --> src\main.rs:103:37
    |
103 |         .map(|(metoa, lujvo, def)| (if let Some(t) = metoa.strip_prefix('\'') {t.to_string()}else{metoa}, lujvo, def))
    |                                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `metoa.strip_prefix('\'').map_or(metoa, |t| t.to_string())`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#option_if_let_else
    = note: `-W clippy::option-if-let-else` implied by `-W clippy::nursery`
    = help: to override `-W clippy::nursery` add `#[allow(clippy::option_if_let_else)]`

Accepting this suggestion yields an [E0505]:

error[E0505]: cannot move out of `metoa` because it is borrowed
   --> src\main.rs:103:69
    |
103 |         .map(|(metoa, lujvo, def)| (metoa.strip_prefix('\'').map_or(metoa, |t| t.to_string()), lujvo, def))
    |                -----                -----                    ------ ^^^^^ move out of `metoa` occurs here
    |                |                    |                        |
    |                |                    |                        borrow later used by call
    |                |                    borrow of `metoa` occurs here
    |                binding `metoa` declared here
Version
rustc 1.88.0-nightly (00095b3da 2025-04-03)
binary: rustc
commit-hash: 00095b3da4f23d9b3e7a809ac6a4e2b2530df84c
commit-date: 2025-04-03
host: x86_64-pc-windows-msvc
release: 1.88.0-nightly
LLVM version: 20.1.2
Additional Labels

@rustbot label +I-suggestion-causes-error

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 at the option_if_let_else lint entry point and reproduce the reported metoa example with the shown Rust version. Check that the suggested Option::map_or replacement compiles without E0505, and validate the corrected suggestion against this reproducer.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 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.