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

False positive in option_if_let_else inside const fn #7567

Closed
dtolnay opened this issue Aug 14, 2021 · 0 comments · Fixed by #7573
Closed

False positive in option_if_let_else inside const fn #7567

dtolnay opened this issue Aug 14, 2021 · 0 comments · Fixed by #7573
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

@dtolnay
Copy link
Member

dtolnay commented Aug 14, 2021

#![deny(clippy::option_if_let_else)]

pub const fn f() -> Option<i8> {
    None
}

pub const fn g() -> i8 {
    if let Some(x) = f() {
        1 + x
    } else {
        0
    }
}
$ cargo clippy

error: use Option::map_or instead of an if let/else
  --> src/main.rs:8:5
   |
8  | /     if let Some(x) = f() {
9  | |         1 + x
10 | |     } else {
11 | |         0
12 | |     }
   | |_____^ help: try: `f().map_or(0, |x| 1 + x)`

However, Clippy's suggested replacement code does not compile.

error[E0015]: calls in constant functions are limited to constant functions, tuple structs and tuple variants
 --> src/main.rs:8:5
  |
8 |     f().map_or(0, |x| 1 + x)
  |     ^^^^^^^^^^^^^^^^^^^^^^^^

Originally reported by @lopopolo in #6137 (comment).

Meta

  • cargo clippy -V: clippy 0.1.56 (0fa3190 2021-08-12)
  • rustc -Vv:
    rustc 1.56.0-nightly (0fa319039 2021-08-12)
    binary: rustc
    commit-hash: 0fa3190394475a84360b34e074e719d519bc40f1
    commit-date: 2021-08-12
    host: x86_64-unknown-linux-gnu
    release: 1.56.0-nightly
    LLVM version: 12.0.1
    
@dtolnay dtolnay 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 Aug 14, 2021
bors added a commit that referenced this issue Aug 15, 2021
Downgrade option_if_let_else to nursery

I believe that this lint's loose understanding of ownership (#5822, #6737) makes it unsuitable to be enabled by default in its current state, even as a pedantic lint.

Additionally the lint has known problems with type inference (#6137), though I may be willing to consider this a non-blocker in isolation if it weren't for the ownership false positives.

A fourth false positive involving const fn: #7567.

But on top of these, for me the biggest issue is I basically fully agree with #6137 (comment). In my experience this lint universally makes code worse even when the resulting code does compile.

---

changelog: remove [`option_if_let_else`] from default set of enabled lints
@bors bors closed this as completed in fd30241 Aug 30, 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

Successfully merging a pull request may close this issue.

1 participant