Skip to content

manual_let_else produces a wrong suggestion with or-patterns #9938

Closed
@Serial-ATA

Description

@Serial-ATA

Summary

When using an or-pattern with two different variants, manual_let_else will not wrap them in parentheses, which are needed for the pattern to be valid in that context.

Reproducer

I tried this code:

#![warn(clippy::manual_let_else)]

enum Foo {
    Bar(i32),
    Baz(i32),
    Qux(i32),
}

fn main() {
    let foo = Foo::Bar(1);

    let _value = match foo {
        Foo::Bar(_) | Foo::Baz(_) => (),
        _ => return,
    };
}

I expected to see this happen:

#![warn(clippy::manual_let_else)]

enum Foo {
    Bar(i32),
    Baz(i32),
    Qux(i32),
}

fn main() {
    let foo = Foo::Bar(1);

    let (Foo::Bar(_) | Foo::Baz(_)) = foo else { return };
}

This is valid, producing no errors.

Instead, this happened:

#![warn(clippy::manual_let_else)]

enum Foo {
    Bar(i32),
    Baz(i32),
    Qux(i32),
}

fn main() {
    let foo = Foo::Bar(1);

	// Notice the missing parens, which is not valid when using `let else`
    let Foo::Bar(_) | Foo::Baz(_) = foo else { return };
}

Which produces the error:

error: top-level or-patterns are not allowed in `let` bindings
  --> src/main.rs:23:9
   |
23 |     let Foo::Bar(_) | Foo::Baz(_) = foo else { return };
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^ help: wrap the pattern in parentheses: `(Foo::Bar(_) | Foo::Baz(_))`

Version

rustc 1.67.0-nightly (70f8737b2 2022-11-23)
binary: rustc
commit-hash: 70f8737b2f5d3bf7d6b784fad00b663b7ff9feda
commit-date: 2022-11-23
host: x86_64-unknown-linux-gnu
release: 1.67.0-nightly
LLVM version: 15.0.4

Additional Labels

@rustbot label +I-suggestion-causes-error

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions