Skip to content

manual_map recommends noncompilable code involving ? #6795

@dtolnay

Description

@dtolnay
#![deny(clippy::manual_map)]

fn f(_x: &str) -> Result<bool, bool> {
    Ok(false)
}

pub fn g(x: Option<String>) -> Result<(), bool> {
    let _x = match x {
        Some(ref x) => Some(f(x)?),
        None => None,
    };
    Ok(())
}
$ cargo clippy

error: manual implementation of `Option::map`
  --> src/main.rs:8:14
   |
8  |       let _x = match x {
   |  ______________^
9  | |         Some(ref x) => Some(f(x)?),
10 | |         None => None,
11 | |     };
   | |_____^ help: try this: `x.as_ref().map(|x| f(x)?)`

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

error[E0277]: the `?` operator can only be used in a closure that returns `Result` or `Option` (or another type that implements `Try`)
 --> src/main.rs:8:33
  |
8 |     let _x = x.as_ref().map(|x| f(x)?);
  |                             ----^^^^^
  |                             |   |
  |                             |   cannot use the `?` operator in a closure that returns `bool`
  |                             this function should return `Result` or `Option` to accept `?`
  |
  = help: the trait `Try` is not implemented for `bool`
  = note: required by `from_error`

@Jarcho @llogiq

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingE-mediumCall for participation: Medium difficulty level problem and requires some initial experience.I-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