Skip to content

manual_let_else produces a wrong suggestion with or-patterns #9938

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

Closed
Serial-ATA opened this issue Nov 24, 2022 · 1 comment · Fixed by #9966
Closed

manual_let_else produces a wrong suggestion with or-patterns #9938

Serial-ATA opened this issue Nov 24, 2022 · 1 comment · Fixed by #9966
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@Serial-ATA
Copy link
Contributor

Serial-ATA commented Nov 24, 2022

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

@Serial-ATA Serial-ATA added the C-bug Category: Clippy is not doing the correct thing label Nov 24, 2022
@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Nov 24, 2022
@alex-semenyuk
Copy link
Member

@rustbot claim

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-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants