Skip to content

manual_map recommends noncompilable code involving moved values #6797

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
dtolnay opened this issue Feb 26, 2021 · 4 comments · Fixed by #6801
Closed

manual_map recommends noncompilable code involving moved values #6797

dtolnay opened this issue Feb 26, 2021 · 4 comments · Fixed by #6801
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@dtolnay
Copy link
Member

dtolnay commented Feb 26, 2021

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

@dtolnay dtolnay added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Feb 26, 2021
dtolnay added a commit to serde-rs/serde that referenced this issue Feb 26, 2021
rust-lang/rust-clippy#6797

    error[E0382]: use of partially moved value: `self`
       --> serde_derive/src/internals/attr.rs:71:24
        |
    71  |         self.value.map(|value| (self.tokens, value))
        |                    ----^^^^^^^----------------------
        |                    |   |        |
        |                    |   |        use occurs due to use in closure
        |                    |   value used here after partial move
        |                    `self.value` partially moved due to this method call
        |
    note: this function takes ownership of the receiver `self`, which moves `self.value`
       --> /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 `self.value` has type `std::option::Option<T>`, which does not implement the `Copy` trait
@matthiaskrgr
Copy link
Member

@dtolnay you are frequently reporting new false positives, do you have a set of crates that you run clippy on on a regular basis?

I can add them to our lintcheck sources list so that we can notice new false positives earlier (optimally even before a new lint gets approved) :)

@dtolnay
Copy link
Member Author

dtolnay commented Feb 26, 2021

@llogiq
Copy link
Contributor

llogiq commented Feb 28, 2021

So in that case, we should not lint? Or is there an alternative suggestion we could make?

@matthiaskrgr
Copy link
Member

I've added anyhow, async-trait, cxx, ryu, serde_yaml and thiserror to the lintcheck sources so that we can easily check if something causes false positives in these crates.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants