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

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

Closed
jyn514 opened this issue Mar 5, 2021 · 3 comments
Closed

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

jyn514 opened this issue Mar 5, 2021 · 3 comments
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

@jyn514
Copy link
Member

jyn514 commented Mar 5, 2021

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
@jyn514 jyn514 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 Mar 5, 2021
@llogiq
Copy link
Contributor

llogiq commented Mar 6, 2021

I think the problem may be that it only looks at the parent item's return type instead of the closure's one.

@Y-Nak
Copy link
Contributor

Y-Nak commented Mar 8, 2021

I think this was fixed in #6801.

#![warn(clippy::manual_map)]

struct Foo {
    x: i32,
}

impl Foo {
    fn add(&self, arg: i32) -> Result<i32, String> {
        Ok(self.x + arg)
    }
}

fn f() -> Result<i32, String> {
    let obj = Foo { x: 1 };

    match Some(0) {
        Some(x) => Some(obj.add(x)?),
        None => None,
    };

    Ok(1)
}

fn main() {}

doesn't trigger the lint in the current version, while it triggers the lint in the version right before #6801 was merged.

@jyn514
Copy link
Member Author

jyn514 commented Mar 8, 2021

Yup, works great on the latest nightly, thanks!

@jyn514 jyn514 closed this as completed Mar 8, 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
Projects
None yet
Development

No branches or pull requests

3 participants