Skip to content
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

In some cases, clippy's manual_map lint suggests changes that don't compile #7413

Closed
dcormier opened this issue Jun 30, 2021 · 0 comments · Fixed by #7531
Closed

In some cases, clippy's manual_map lint suggests changes that don't compile #7413

dcormier opened this issue Jun 30, 2021 · 0 comments · Fixed by #7531
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 I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@dcormier
Copy link

I tried this code:

use std::collections::HashMap;

fn main() {
    #[derive(Debug)]
    struct Thing {
        name: Option<&'static str>,
    }

    let map: HashMap<_, _> = vec![
        Thing { name: Some("a") },
        Thing { name: None },
        Thing { name: Some("b") },
    ]
    .into_iter()
    // Clippy suggests changing this manual map
    // https://rust-lang.github.io/rust-clippy/master/index.html#manual_map
    .filter_map(|thing| match &thing.name {
        Some(name) => Some((name.to_string(), thing)),
        None => None,
    })
    // Clippy's recommendation doesn't compile:
    // .filter_map(|thing| thing.name.as_ref().map(|name| (name.clone(), thing)))
    .collect();

    println!("{:?}", map);
}

Playground.

I expected to see this happen: Clippy would have no complaints.

Instead, this happened: Clippy complained about the manual map:

warning: manual implementation of `Option::map`
  --> src/main.rs:17:25
   |
17 |       .filter_map(|thing| match &thing.name {
   |  _________________________^
18 | |         Some(name) => Some((name.to_string(), thing)),
19 | |         None => None,
20 | |     })
   | |_____^ help: try this: `thing.name.as_ref().map(|name| (name.to_string(), thing))`
   |
   = 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

But the suggested change doesn't compile.

error[E0505]: cannot move out of `thing` because it is borrowed
  --> src/main.rs:22:49
   |
22 |     .filter_map(|thing| thing.name.as_ref().map(|name| (name.clone(), thing)))
   |                         ----------          --- ^^^^^^                ----- move occurs due to use in closure
   |                         |                   |   |
   |                         |                   |   move out of `thing` occurs here
   |                         |                   borrow later used by call
   |                         borrow of `thing.name` occurs here

Meta

  • cargo clippy -V: clippy 0.1.53 (53cb7b09 2021-06-17)
  • rustc -Vv:
rustc 1.53.0 (53cb7b09b 2021-06-17)
binary: rustc
commit-hash: 53cb7b09b00cbea8754ffb78e7e3cb521cb8af4b
commit-date: 2021-06-17
host: x86_64-apple-darwin
release: 1.53.0
LLVM version: 12.0.1
@dcormier dcormier added the C-bug Category: Clippy is not doing the correct thing label Jun 30, 2021
@giraffate giraffate added I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied labels Jul 1, 2021
@Jarcho Jarcho mentioned this issue Aug 4, 2021
@bors bors closed this as completed in 3f0c977 Aug 16, 2021
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 I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants