rust-lang / rust-lang/rust-clippy

`manual_map` does not consider the borrowing relationship

Open
#9,407 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Summary

In the example below, the suggestion given by clippy is wrong. Because of the conflicting borrows in the Scrutinee and Arm of the match expression.

Lint Name

manual_map

Reproducer

I tried this code (playground):

struct A(i32);

impl A {
    fn val(&self) -> Option<&i32> {
        Some(&self.0)
    }
    
    fn do_something(&mut self) -> &'static str {
        "result"
    }
}

let mut a = A(1);
let _ = match a.val() {
    None => None,
    Some(_) => Some(a.do_something()),
};

I saw this happen:

warning: manual implementation of `Option::map`
  --> src/main.rs:15:13
   |
15 |       let _ = match a.val() {
   |  _____________^
16 | |         None => None,
17 | |         Some(_) => Some(a.do_something()),
18 | |     };
   | |_____^ help: try this: `a.val().map(|_| a.do_something())`
   |
   = note: `#[warn(clippy::manual_map)]` on by default
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_map
Version

No response

Additional Labels

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 with the manual_map lint and the Rust reproducer in this issue, then run Clippy against it. The fix is done when Clippy no longer suggests the invalid Option::map rewrite and the valid manual-map cases remain covered.

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
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.