Skip to content

manual_map thinks rust has non-local control flow in closures #6857

Closed
@jyn514

Description

@jyn514

Lint name: manual_map

I tried this code:

        let github_repo = match self.github_stats {
            Some(stats) => Some(stats.create(&mut self.db.conn())?),
            None => None,
        };

I expected to see this happen: No warning, because this is non-trivial to replicate with combinators.

Instead, this happened: Clippy warned that this can use map:

warning: manual implementation of `Option::map`
   --> src/test/fakes.rs:319:27
    |
319 |           let github_repo = match self.github_stats {
    |  ___________________________^
320 | |             Some(stats) => Some(stats.create(&mut self.db.conn())?),
321 | |             None => None,
322 | |         };
    | |_________^ help: try this: `self.github_stats.map(|stats| stats.create(&mut self.db.conn())?)`
    |
    = 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

and then fails to apply its own suggestion:

The following errors were reported:
error[E0277]: the `?` operator can only be used in a closure that returns `Result` or `Option` (or another type that implements `std::ops::Try`)
   --> src/test/fakes.rs:319:57
    |
319 |         let github_repo = self.github_stats.map(|stats| stats.create(&mut self.db.conn())?);
    |                                                 --------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |                                                 |       |
    |                                                 |       cannot use the `?` operator in a closure that returns `std::string::String`
    |                                                 this function should return `Result` or `Option` to accept `?`
    |
    = help: the trait `std::ops::Try` is not implemented for `std::string::String`
    = note: required by `std::ops::Try::from_error`

error: aborting due to previous error

Meta

  • cargo clippy -V: clippy 0.1.52 (45b3c28 2021-03-04)
  • rustc -Vv:
rustc 1.52.0-nightly (45b3c2851 2021-03-04)
binary: rustc
commit-hash: 45b3c28518e4c45dfd12bc2c4400c0d0e9639927
commit-date: 2021-03-04
host: x86_64-unknown-linux-gnu
release: 1.52.0-nightly
LLVM version: 12.0.0

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