Skip to content

redundant_pattern_matching: if let Err(_) = ... else and if ....is_err() are not the same #14113

@KizzyCode

Description

@KizzyCode

Summary

The clippy lint is suggesting to replace if let pattern = blurp else with if blurp.is_err(), which are not synonymous. The problem with the drop order is already warned about; however there's another point that is specific to let ... else:

let ... else forces you to handle the else-case by diverging, whereas a normal if-clause does not. Bugs like goto fail (different language, same underlying problem) show that this is a problem in practice. let ... else has different, significantly stronger semantics regarding error handling and bug resilience; and clippy's suggestion to just go if is unfortunate in my opinion.

If people use let ... else, it is not unlikely that they use it intentionally to get those stronger semantics, and I personally consider this warning (and the suggested replacement) a false-positive for let ... else-cases (at least on the default warning level).

Lint Name

redundant_pattern_matching

Reproducer

I tried this code:

if let Err(_) = thread_join_handle.join() {
    // Fail with error if the task panicked
    return Err(format!("Task panicked"));
}

I saw this happen:

redundant pattern matching, consider using `is_err()`
this will change drop order of the result, as well as all temporaries
add `#[allow(clippy::redundant_pattern_matching)]` if this is important
for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_pattern_matching
`#[warn(clippy::redundant_pattern_matching)]` on by default

I expected to see this happen:

<nothing>

Version

rustc 1.84.0 (9fc6b4312 2025-01-07)
binary: rustc
commit-hash: 9fc6b43126469e3858e2fe86cafb4f0fd5068869
commit-date: 2025-01-07
host: aarch64-apple-darwin
release: 1.84.0
LLVM version: 19.1.5

Additional Labels

No response

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