Skip to content

manual_map recommends noncompilable code involving moved values #6797

@dtolnay

Description

@dtolnay
fn main() {
    let tuple = (Some(String::new()), String::new());

    let x = match tuple.0 {
        Some(x) => Some((x, tuple.1)),
        None => None,
    };

    println!("{:?}", x);
}
$  cargo clippy

warning: manual implementation of `Option::map`
 --> src/main.rs:4:13
  |
4 |       let x = match tuple.0 {
  |  _____________^
5 | |         Some(x) => Some((x, tuple.1)),
6 | |         None => None,
7 | |     };
  | |_____^ help: try this: `tuple.0.map(|x| (x, tuple.1))`
  |
  = 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

Applying the suggestion made by clippy makes the code not compile:

error[E0382]: use of partially moved value: `tuple`
   --> src/main.rs:4:25
    |
4   |     let x = tuple.0.map(|x| (x, tuple.1));
    |                     ----^^^--------------
    |                     |   |       |
    |                     |   |       use occurs due to use in closure
    |                     |   value used here after partial move
    |                     `tuple.0` partially moved due to this method call
    |
note: this function takes ownership of the receiver `self`, which moves `tuple.0`
   --> /home/david/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/option.rs:485:38
    |
485 |     pub fn map<U, F: FnOnce(T) -> U>(self, f: F) -> Option<U> {
    |                                      ^^^^
    = note: partial move occurs because `tuple.0` has type `Option<String>`, which does not implement the `Copy` trait

@Jarcho @llogiq

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions