Skip to content

False positive in option_if_let_else inside const fn #7567

@dtolnay

Description

@dtolnay
#![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
    

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