Repository: rust
Issue: Matching with multiple patterns and match guards can have unintuitive results
Labels: A-diagnostics,T-compiler

<!--
Thank you for filing a bug report! 🐛 Please provide a short summary of the bug,
along with any information you feel relevant to replicating the bug.

If you cannot produce a minimal reproduction case (something that would work in
isolation), please provide the steps or even link to a repository that causes
the problematic output to occur.
-->

When matching with multiple patterns AND a match guard, it is potentially confusing that the match guard applies to ALL patterns, not just the rightmost pattern.

In writing an encoder where I wanted to ensure that I don't encode a value larger than 127, I wrote the "wrong" variant below, until I caught the issue in a test. There was no warning, and clippy also did not give any warning.

This is probably a pretty hard thing to write a reliable lint for (either in the compiler or in clippy) that wouldn't have false positives, so I understand if this gets closed, but I stumbled over it and wanted to report it, in case y'all have better ideas on how to catch this than I do.

Given the following code: [playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=db1f1214c0204590bafb10412a4fc17a)

```rust
fn main() {
    for outer in 0..13 {
        for inner in 0..12 {
            // wrong
            let x = match outer {
                0..=9 | 10 if inner <= 7 => (outer * 12) + inner,
                _ => 0x00,
            };
            
            // right
            let y = match outer {
                0..=9 => (outer * 12) + inner,
                10 if inner <= 7 => (outer * 12) + inner,
                _ => 0x00,
            };
            
            let res = if x == y { "RIGHT" } else { "WRONG" };
            
            println!("| {outer:02} | {inner:02} | {x:03} | {y:03} | {res} |");
            assert!(x <= 127);
        }
    }
}
```

The current output is:

```
| 00 | 00 | 000 | 000 | RIGHT |
| 00 | 01 | 001 | 001 | RIGHT |
| 00 | 02 | 002 | 002 | RIGHT |
| 00 | 03 | 003 | 003 | RIGHT |
| 00 | 04 | 004 | 004 | RIGHT |
| 00 | 05 | 005 | 005 | RIGHT |
| 00 | 06 | 006 | 006 | RIGHT |
| 00 | 07 | 007 | 007 | RIGHT |
| 00 | 08 | 000 | 008 | WRONG |
| 00 | 09 | 000 | 009 | WRONG |
| 00 | 10 | 000 | 010 | WRONG |
| 00 | 11 | 000 | 011 | WRONG |
| 01 | 00 | 012 | 012 | RIGHT |
| 01 | 01 | 013 | 013 | RIGHT |
| 01 | 02 | 014 | 014 | RIGHT |
| 01 | 03 | 015 | 015 | RIGHT |
| 01 | 04 | 016 | 016 | RIGHT |
| 01 | 05 | 017 | 017 | RIGHT |
| 01 | 06 | 018 | 018 | RIGHT |
| 01 | 07 | 019 | 019 | RIGHT |
| 01 | 08 | 000 | 020 | WRONG |
| 01 | 09 | 000 | 021 | WRONG |
| 01 | 10 | 000 | 022 | WRONG |
| 01 | 11 | 000 | 023 | WRONG |
| 02 | 00 | 024 | 024 | RIGHT |
| 02 | 01 | 025 | 025 | RIGHT |
| 02 | 02 | 026 | 026 | RIGHT |
| 02 | 03 | 027 | 027 | RIGHT |
| 02 | 04 | 028 | 028 | RIGHT |
| 02 | 05 | 029 | 029 | RIGHT |
| 02 | 06 | 030 | 030 | RIGHT |
| 02 | 07 | 031 | 031 | RIGHT |
| 02 | 08 | 000 | 032 | WRONG |
| 02 | 09 | 000 | 033 | WRONG |
| 02 | 10 | 000 | 034 | WRONG |
| 02 | 11 | 000 | 035 | WRONG |
| 03 | 00 | 036 | 036 | RIGHT |
| 03 | 01 | 037 | 037 | RIGHT |
| 03 | 02 | 038 | 038 | RIGHT |
| 03 | 03 | 039 | 039 | RIGHT |
| 03 | 04 | 040 | 040 | RIGHT |
| 03 | 05 | 041 | 041 | RIGHT |
| 03 | 06 | 042 | 042 | RIGHT |
| 03 | 07 | 043 | 043 | RIGHT |
| 03 | 08 | 000 | 044 | WRONG |
| 03 | 09 | 000 | 045 | WRONG |
| 03 | 10 | 000 | 046 | WRONG |
| 03 | 11 | 000 | 047 | WRONG |
| 04 | 00 | 048 | 048 | RIGHT |
| 04 | 01 | 049 | 049 | RIGHT |
| 04 | 02 | 050 | 050 | RIGHT |
| 04 | 03 | 051 | 051 | RIGHT |
| 04 | 04 | 052 | 052 | RIGHT |
| 04 | 05 | 053 | 053 | RIGHT |
| 04 | 06 | 054 | 054 | RIGHT |
| 04 | 07 | 055 | 055 | RIGHT |
| 04 | 08 | 000 | 056 | WRONG |
| 04 | 09 | 000 | 057 | WRONG |
| 04 | 10 | 000 | 058 | WRONG |
| 04 | 11 | 000 | 059 | WRONG |
| 05 | 00 | 060 | 060 | RIGHT |
| 05 | 01 | 061 | 061 | RIGHT |
| 05 | 02 | 062 | 062 | RIGHT |
| 05 | 03 | 063 | 063 | RIGHT |
| 05 | 04 | 064 | 064 | RIGHT |
| 05 | 05 | 065 | 065 | RIGHT |
| 05 | 06 | 066 | 066 | RIGHT |
| 05 | 07 | 067 | 067 | RIGHT |
| 05 | 08 | 000 | 068 | WRONG |
| 05 | 09 | 000 | 069 | WRONG |
| 05 | 10 | 000 | 070 | WRONG |
| 05 | 11 | 000 | 071 | WRONG |
| 06 | 00 | 072 | 072 | RIGHT |
| 06 | 01 | 073 | 073 | RIGHT |
| 06 | 02 | 074 | 074 | RIGHT |
| 06 | 03 | 075 | 075 | RIGHT |
| 06 | 04 | 076 | 076 | RIGHT |
| 06 | 05 | 077 | 077 | RIGHT |
| 06 | 06 | 078 | 078 | RIGHT |
| 06 | 07 | 079 | 079 | RIGHT |
| 06 | 08 | 000 | 080 | WRONG |
| 06 | 09 | 000 | 081 | WRONG |
| 06 | 10 | 000 | 082 | WRONG |
| 06 | 11 | 000 | 083 | WRONG |
| 07 | 00 | 084 | 084 | RIGHT |
| 07 | 01 | 085 | 085 | RIGHT |
| 07 | 02 | 086 | 086 | RIGHT |
| 07 | 03 | 087 | 087 | RIGHT |
| 07 | 04 | 088 | 088 | RIGHT |
| 07 | 05 | 089 | 089 | RIGHT |
| 07 | 06 | 090 | 090 | RIGHT |
| 07 | 07 | 091 | 091 | RIGHT |
| 07 | 08 | 000 | 092 | WRONG |
| 07 | 09 | 000 | 093 | WRONG |
| 07 | 10 | 000 | 094 | WRONG |
| 07 | 11 | 000 | 095 | WRONG |
| 08 | 00 | 096 | 096 | RIGHT |
| 08 | 01 | 097 | 097 | RIGHT |
| 08 | 02 | 098 | 098 | RIGHT |
| 08 | 03 | 099 | 099 | RIGHT |
| 08 | 04 | 100 | 100 | RIGHT |
| 08 | 05 | 101 | 101 | RIGHT |
| 08 | 06 | 102 | 102 | RIGHT |
| 08 | 07 | 103 | 103 | RIGHT |
| 08 | 08 | 000 | 104 | WRONG |
| 08 | 09 | 000 | 105 | WRONG |
| 08 | 10 | 000 | 106 | WRONG |
| 08 | 11 | 000 | 107 | WRONG |
| 09 | 00 | 108 | 108 | RIGHT |
| 09 | 01 | 109 | 109 | RIGHT |
| 09 | 02 | 110 | 110 | RIGHT |
| 09 | 03 | 111 | 111 | RIGHT |
| 09 | 04 | 112 | 112 | RIGHT |
| 09 | 05 | 113 | 113 | RIGHT |
| 09 | 06 | 114 | 114 | RIGHT |
| 09 | 07 | 115 | 115 | RIGHT |
| 09 | 08 | 000 | 116 | WRONG |
| 09 | 09 | 000 | 117 | WRONG |
| 09 | 10 | 000 | 118 | WRONG |
| 09 | 11 | 000 | 119 | WRONG |
| 10 | 00 | 120 | 120 | RIGHT |
| 10 | 01 | 121 | 121 | RIGHT |
| 10 | 02 | 122 | 122 | RIGHT |
| 10 | 03 | 123 | 123 | RIGHT |
| 10 | 04 | 124 | 124 | RIGHT |
| 10 | 05 | 125 | 125 | RIGHT |
| 10 | 06 | 126 | 126 | RIGHT |
| 10 | 07 | 127 | 127 | RIGHT |
| 10 | 08 | 000 | 000 | RIGHT |
| 10 | 09 | 000 | 000 | RIGHT |
| 10 | 10 | 000 | 000 | RIGHT |
| 10 | 11 | 000 | 000 | RIGHT |
| 11 | 00 | 000 | 000 | RIGHT |
| 11 | 01 | 000 | 000 | RIGHT |
| 11 | 02 | 000 | 000 | RIGHT |
| 11 | 03 | 000 | 000 | RIGHT |
| 11 | 04 | 000 | 000 | RIGHT |
| 11 | 05 | 000 | 000 | RIGHT |
| 11 | 06 | 000 | 000 | RIGHT |
| 11 | 07 | 000 | 000 | RIGHT |
| 11 | 08 | 000 | 000 | RIGHT |
| 11 | 09 | 000 | 000 | RIGHT |
| 11 | 10 | 000 | 000 | RIGHT |
| 11 | 11 | 000 | 000 | RIGHT |
| 12 | 00 | 000 | 000 | RIGHT |
| 12 | 01 | 000 | 000 | RIGHT |
| 12 | 02 | 000 | 000 | RIGHT |
| 12 | 03 | 000 | 000 | RIGHT |
| 12 | 04 | 000 | 000 | RIGHT |
| 12 | 05 | 000 | 000 | RIGHT |
| 12 | 06 | 000 | 000 | RIGHT |
| 12 | 07 | 000 | 000 | RIGHT |
| 12 | 08 | 000 | 000 | RIGHT |
| 12 | 09 | 000 | 000 | RIGHT |
| 12 | 10 | 000 | 000 | RIGHT |
| 12 | 11 | 000 | 000 | RIGHT |
```