Skip to content

option_map_unwrap_or lint suggestion does not compile #2686

Closed
@idubrov

Description

@idubrov

In the following case

#![deny(option_map_unwrap_or)]
pub fn main() {
  let id: String = "identifier".to_string();
  let prefix: Option<&str> = Some("prefix");
  let id = prefix.map(|p| format!("{}.{}", p, id)).unwrap_or(id);
}

Clippy suggests the following change:

note: lint level defined here
  --> main/src/main.rs:1:9
   |
1  | #![deny(option_map_unwrap_or)]
   |         ^^^^^^^^^^^^^^^^^^^^
   = note: replace `map(|p| format!("{}.{}", p, id)).unwrap_or(id)` with `map_or(id, |p| format!("{}.{}", p, id))`
   = help: for further information visit https://rust-lang-nursery.github.io/rust-clippy/v0.0.186/index.html#option_map_unwrap_or

which would not compile:

error[E0382]: capture of moved value: `id`
  --> main/src/main.rs:10:30
   |
10 |   let id = prefix.map_or(id, |p| format!("{}.{}", p, id));
   |                          --  ^^^ value captured here after move
   |                          |
   |                          value moved here
   |
   = note: move occurs because `id` has type `std::string::String`, which does not implement the `Copy` trait

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when appliedL-suggestionLint: Improving, adding or fixing lint suggestions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions