Skip to content

FP return_and_then #14927

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

Open
matthiaskrgr opened this issue May 30, 2025 · 2 comments · May be fixed by #14950
Open

FP return_and_then #14927

matthiaskrgr opened this issue May 30, 2025 · 2 comments · May be fixed by #14950
Assignees
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

@matthiaskrgr
Copy link
Member

matthiaskrgr commented May 30, 2025

Summary

.

Reproducer

I tried this code:

#![warn(clippy::return_and_then)]
#![allow(unused)]
use std::path::Path;
struct A {
    pub func: fn(check: bool, a: &Path, b: Option<&Path>),
}
const MY_A: A = A {
    func: |check, a, b| {
        if check {
            let _ = ();
        } else if let Some(parent) = b.and_then(|p| p.parent()) {
            let _ = ();
        }
    },
};

pub fn main() {}

suggestion:

warning: use the `?` operator instead of an `and_then` call
  --> src/main.rs:11:38
   |
11 |         } else if let Some(parent) = b.and_then(|p| p.parent()) {
   |                                      ^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#return_and_then
note: the lint level is defined here
  --> src/main.rs:1:9
   |
1  | #![warn(clippy::return_and_then)]
   |         ^^^^^^^^^^^^^^^^^^^^^^^
help: try
   |
11 ~         } else if let Some(parent) = {
12 +             let p = b?;
13 +             p.parent()
14 ~         } {
   |

the suggested code:

#![warn(clippy::return_and_then)]
#![allow(unused)]
use std::path::Path;
struct A {
    pub func: fn(check: bool, a: &Path, b: Option<&Path>),
}
const MY_A: A = A {
    func: |check, a, b| {
        if check {
            let _ = ();
        } else if let Some(parent) = {
            let p = b?;
            p.parent()
        } {
            let _ = ();
        }
    },
};

pub fn main() {}

does not compile:

error[E0277]: the `?` operator can only be used in a closure that returns `Result` or `Option` (or another type that implements `FromResidual`)
  --> src/main.rs:12:22
   |
8  |     func: |check, a, b| {
   |           ------------- this function should return `Result` or `Option` to accept `?`
...
12 |             let p = b?;
   |                      ^ cannot use the `?` operator in a closure that returns `()`

For more information about this error, try `rustc --explain E0277`.

Version


Additional Labels

No response

@matthiaskrgr matthiaskrgr 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 May 30, 2025
@matthiaskrgr
Copy link
Member Author

rustc 1.89.0-nightly (1bbd62e54 2025-05-29)
binary: rustc
commit-hash: 1bbd62e547ba5cc08ccb44c27def3d33195d2dd5
commit-date: 2025-05-29
host: x86_64-unknown-linux-gnu
release: 1.89.0-nightly
LLVM version: 20.1.5

@profetia
Copy link
Contributor

@rustbot claim

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

Successfully merging a pull request may close this issue.

2 participants