Skip to content

manual_let_else suggests invalid replacement #10241

Closed
@poliorcetics

Description

@poliorcetics

Summary

manual_let_else can propose replacements that change the behaviour of the code

Permalink to the reproducer: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=b6719a7919c90a17b495f991566e423c, tested 2023-01-26

Lint Name

manual_let_else

Reproducer

I tried this code:

#![warn(clippy::manual_let_else)]

fn main() {
    let data = [1_u8, 2, 3, 4, 0, 0, 0, 0];
    
    // Rewriting as let-else would break this match because [data @ ..] (last pattern) matches everything.
    let data = match data.as_slice() {
        [] | [0, 0] => return,
        [data @ .., 0, 0, 0, 0] | [data @ .., 0, 0] | [data @ ..] => data,
    };
    
    dbg!(data);
}

I saw this happen:

#![warn(clippy::manual_let_else)]

fn main() {
    let data = [1_u8, 2, 3, 4, 0, 0, 0, 0];
    
    // Rewriting as let-else would break this match because [data @ ..] (last pattern) matches everything.
    let ([data @ .., 0, 0, 0, 0] | [data @ .., 0, 0] | [data @ ..]) = data.as_slice() else {
        return;
    };
    
    dbg!(data);
}

I expected to see this happen:

No replacement is proposed

Version

- rust-clippy 0.1.68 (2023-01-26 d7948c8) (playground)

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