os-rust/compiler/rustc_pattern_analysis/src
Matthias Krüger 7fc2b33722
Rollup merge of #132708 - estebank:const-as-binding, r=Nadrieril
Point at `const` definition when used instead of a binding in a `let` statement

Modify `PatKind::InlineConstant` to be `ExpandedConstant` standing in not only for inline `const` blocks but also for `const` items. This allows us to track named `const`s used in patterns when the pattern is a single binding. When we detect that there is a refutable pattern involving a `const` that could have been a binding instead, we point at the `const` item, and suggest renaming. We do this for both `let` bindings and `match` expressions missing a catch-all arm if there's at least one single binding pattern referenced.

After:

```
error[E0005]: refutable pattern in local binding
  --> $DIR/bad-pattern.rs:19:13
   |
LL |     const PAT: u32 = 0;
   |     -------------- missing patterns are not covered because `PAT` is interpreted as a constant pattern, not a new variable
...
LL |         let PAT = v1;
   |             ^^^ pattern `1_u32..=u32::MAX` not covered
   |
   = note: `let` bindings require an "irrefutable pattern", like a `struct` or an `enum` with only one variant
   = note: for more information, visit https://doc.rust-lang.org/book/ch18-02-refutability.html
   = note: the matched value is of type `u32`
help: introduce a variable instead
   |
LL |         let PAT_var = v1;
   |             ~~~~~~~
```

Before:

```
error[E0005]: refutable pattern in local binding
  --> $DIR/bad-pattern.rs:19:13
   |
LL |         let PAT = v1;
   |             ^^^
   |             |
   |             pattern `1_u32..=u32::MAX` not covered
   |             missing patterns are not covered because `PAT` is interpreted as a constant pattern, not a new variable
   |             help: introduce a variable instead: `PAT_var`
   |
   = note: `let` bindings require an "irrefutable pattern", like a `struct` or an `enum` with only one variant
   = note: for more information, visit https://doc.rust-lang.org/book/ch18-02-refutability.html
   = note: the matched value is of type `u32`
```

CC #132582.
2024-11-20 20:10:12 +01:00
..
rustc compiler: Add rustc_abi dependence to the compiler 2024-10-27 21:10:58 -07:00
constructor.rs cleanup: don't clone types that are Copy 2024-09-29 13:31:30 +02:00
errors.rs Encapsulate the printing of WitnessPat 2024-07-29 14:56:50 +10:00
lib.rs Add warn(unreachable_pub) to rustc_pattern_analysis. 2024-08-29 20:18:44 +10:00
lints.rs Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
pat.rs Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
pat_column.rs Rename TypeCx -> PatCx 2024-03-13 13:56:38 +01:00
rustc.rs Rollup merge of #132708 - estebank:const-as-binding, r=Nadrieril 2024-11-20 20:10:12 +01:00
usefulness.rs Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00