Skip to content

unnested-or-patterns union binding no longer usable #15219

Open
@matthiaskrgr

Description

@matthiaskrgr

Using the following flags

--force-warn clippy::unnested-or-patterns

this code:

#![allow(unreachable_patterns)]

#[derive(Copy, Clone)]
#[allow(dead_code)]
struct Pie {
    slices: u8,
    size: u8,
}

union Foo {
    #[allow(dead_code)]
    bar: i8,
    baz: Pie,
}

fn main() {
    let u = Foo { bar: 5 };
    let (Some(Foo { bar: _ }) | None) = Some(u);
    let u = Foo { bar: 6 };
    let (Some(Foo { bar: _ }) | Some(Foo { bar: _ }) | None) = Some(u);
    unsafe {
        let u = Foo { bar: 7 };
        let (Foo { bar } | Foo { bar }) = u;
        assert_eq!(bar, 7)
    }
}

caused the following diagnostics:

    Checking _a v0.1.0 (/tmp/icemaker_global_tempdir.ATXPagLEKTo8/icemaker_clippyfix_tempdir.8m2Msv4yeMG9/_a)
warning: unnested or-patterns
  --> src/main.rs:20:9
   |
20 |     let (Some(Foo { bar: _ }) | Some(Foo { bar: _ }) | None) = Some(u);
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnested_or_patterns
   = note: requested on the command line with `--force-warn clippy::unnested-or-patterns`
help: nest the patterns
   |
20 -     let (Some(Foo { bar: _ }) | Some(Foo { bar: _ }) | None) = Some(u);
20 +     let (Some(Foo { bar: _ | _ }) | None) = Some(u);
   |

warning: unnested or-patterns
  --> src/main.rs:23:13
   |
23 |         let (Foo { bar } | Foo { bar }) = u;
   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnested_or_patterns
help: nest the patterns
   |
23 -         let (Foo { bar } | Foo { bar }) = u;
23 +         let (Foo { bar | bar }) = u;
   |

warning: `_a` (bin "_a") generated 2 warnings (run `cargo clippy --fix --bin "_a"` to apply 2 suggestions)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.22s

However after applying these diagnostics, the resulting code:

#![allow(unreachable_patterns)]

#[derive(Copy, Clone)]
#[allow(dead_code)]
struct Pie {
    slices: u8,
    size: u8,
}

union Foo {
    #[allow(dead_code)]
    bar: i8,
    baz: Pie,
}

fn main() {
    let u = Foo { bar: 5 };
    let (Some(Foo { bar: _ }) | None) = Some(u);
    let u = Foo { bar: 6 };
    let (Some(Foo { bar: _ | _ }) | None) = Some(u);
    unsafe {
        let u = Foo { bar: 7 };
        let (Foo { bar | bar }) = u;
        assert_eq!(bar, 7)
    }
}

no longer compiled:

    Checking _a v0.1.0 (/tmp/icemaker_global_tempdir.ATXPagLEKTo8/icemaker_clippyfix_tempdir.8m2Msv4yeMG9/_a)
error: expected `,`
  --> src/main.rs:23:24
   |
23 |         let (Foo { bar | bar }) = u;
   |              ---       ^
   |              |
   |              while parsing the fields for this pattern

error[E0425]: cannot find value `bar` in this scope
  --> src/main.rs:24:20
   |
24 |         assert_eq!(bar, 7)
   |                    ^^^ not found in this scope

error: union patterns should have exactly one field
  --> src/main.rs:23:14
   |
23 |         let (Foo { bar | bar }) = u;
   |              ^^^^^^^^^^^^^^^^^

error: `..` cannot be used in union patterns
  --> src/main.rs:23:14
   |
23 |         let (Foo { bar | bar }) = u;
   |              ^^^^^^^^^^^^^^^^^

For more information about this error, try `rustc --explain E0425`.
error: could not compile `_a` (bin "_a" test) due to 4 previous errors
warning: build failed, waiting for other jobs to finish...
error: could not compile `_a` (bin "_a") due to 4 previous errors

Version:

rustc 1.90.0-nightly (c83e217d2 2025-07-06)
binary: rustc
commit-hash: c83e217d268d25960a0c79c6941bcb3917a6a0af
commit-date: 2025-07-06
host: x86_64-unknown-linux-gnu
release: 1.90.0-nightly
LLVM version: 20.1.7

Metadata

Metadata

Assignees

No one assigned

    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