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

if_then_some_else_none invalid suggestion #13407

Closed
allan2 opened this issue Sep 17, 2024 · 0 comments · Fixed by #13409
Closed

if_then_some_else_none invalid suggestion #13407

allan2 opened this issue Sep 17, 2024 · 0 comments · Fixed by #13409
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

@allan2
Copy link

allan2 commented Sep 17, 2024

Summary

if_then_some_else_none is giving an invalid suggestion for the case below.

fn opt_bool_s(s: &str) -> Option<bool> {
    if s == "1" {
        Some(true)
    } else {
        None
    }
}
warning: this could be simplified with `bool::then`
   --> foo/src/lib.rs:629:2
    |
629 | /     if s == "1" {
630 | |         Some(true)
631 | |     } else {
632 | |         None
633 | |     }
    | |_____^ help: try: `(s == "1").then(true)`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#if_then_some_else_none

The suggestion is invalid code.

expected a `FnOnce()` closure, found `bool`
the trait `FnOnce()` is not implemented for `bool`
wrap the `bool` in a closure with no arguments: `|| { /* code */ }`rustc[Click for full compiler diagnostic](rust-analyzer-diagnostics-view:/diagnostic%20message%20%5B0%5D?0#file%3A%2F%2F%2Fhome%2Fal%2Fdesktop%2Faigu%2Fcrates%2Fproxy%2Fsrc%2Flib.rs)
lib.rs(629, 16): required by a bound introduced by this call
bool.rs(59, 23): required by a bound in `core::bool::<impl bool>::then`

Expected behaviour

While (s == "1").then(|| true) works, the suggestion should be (s == "1").then_some(true).

See the emitted warning for the following function:

fn opt_bool(cond: bool) -> Option<bool> {
    if cond {
        Some(true)
    } else {
        None
    }
}
warning: this could be simplified with `bool::then_some`
   --> crates/proxy/src/lib.rs:641:5
    |
641 | /     if cond {
642 | |         Some(true)
643 | |     } else {
644 | |         None
645 | |     }
    | |_____^ help: try: `cond.then_some(true)`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#if_then_some_else_none

Version

rustc 1.83.0-nightly (c52c23b6f 2024-09-16)
binary: rustc
commit-hash: c52c23b6f44cd19718721a5e3b2eeb169e9c96ff
commit-date: 2024-09-16
host: x86_64-unknown-linux-gnu
release: 1.83.0-nightly
LLVM version: 19.1.0
@allan2 allan2 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 Sep 17, 2024
@allan2 allan2 changed the title if_then_some_else_none false positive if_then_some_else_none invalid suggestion Sep 17, 2024
bors added a commit that referenced this issue Sep 22, 2024
Fix `if_then_some_else_none` sugg missing closure intro

Fixes #13407

#13407 works in current stable. The suggestion-generating code got trampled over in 0532104 :-)

changelog: [`if_then_some_else_none`]: Fix missing closure in suggestion
@bors bors closed this as completed in c3d58cd Sep 22, 2024
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.

1 participant