Tweak comma handling of "missing match arm" suggestion and fix "remove this arm" suggestion, and make suggestion verbose by estebank · Pull Request #137409 · rust-lang/rust

@rustbot rustbot added S-waiting-on-review

Status: Awaiting review from the assignee but also interested parties.

T-compiler

Relevant to the compiler team, which will review and decide on the PR/issue.

labels

Feb 22, 2025

estebank

estebank

compiler-errors

@compiler-errors compiler-errors changed the title Tweak comma handling of "missing match arm" suggestion and fix "remove this arm" suggestion Tweak comma handling of "missing match arm" suggestion and fix "remove this arm" suggestion, and make suggestion verbose

Feb 28, 2025

estebank

estebank

@bors bors added S-waiting-on-bors

Status: Waiting on bors to run and complete tests. Bors will change the label on completion.

and removed S-waiting-on-review

Status: Awaiting review from the assignee but also interested parties.

labels

Feb 28, 2025

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request

Mar 1, 2025
Tweak comma handling of "missing match arm" suggestion and fix "remove this arm" suggestion, and make suggestion verbose

Better track trailing commas in match arms. Do not suggest adding trailing comma to match arm with block body. Better heuristic for "is this match in one line".

When encountering an unreachable match arm, (correctly) suggest removing the entire arm:

```
error: a never pattern is always unreachable
  --> $DIR/ICE-130779-never-arm-no-oatherwise-block.rs:10:20
   |
LL |         ! if true => {}
   |                      ^^ this will never be executed
   |
help: remove the unreachable match arm
   |
LL -         ! if true => {}
   |
```

Noticed in rust-lang#137343 (comment).

r? `@compiler-errors`

The first commit is independent of the second, but to make the second one produce accurate suggestions the span needs to include the trailing comma, hence the grouping of both changes in this PR.

@bors bors removed the S-waiting-on-bors

Status: Waiting on bors to run and complete tests. Bors will change the label on completion.

label

Mar 1, 2025

@bors bors added the S-waiting-on-author

Status: This is awaiting some action (such as code changes or more information) from the author.

label

Mar 1, 2025
Better track trailing commas in match arms. Do not suggest adding trailing comma to match arm with block body. Better heuristic for "is this match in one line".
When encountering an unreachable match arm, (correctly) suggest removing the entire arm:

```
error: a never pattern is always unreachable
  --> $DIR/ICE-130779-never-arm-no-oatherwise-block.rs:10:20
   |
LL |         Some(!) if true => {}
   |                            ^^ this will never be executed
   |
help: remove the unreachable match arm
   |
LL -         Some(!) if true => {}
LL +         Some(!),
   |
```

Noticed in rust-lang#137343 (comment).

@estebank

```
error[E0004]: non-exhaustive patterns: `u8::MAX` not covered
  --> $DIR/exhaustiveness.rs:47:8
   |
LL |     m!(0u8, 0..255);
   |        ^^^ pattern `u8::MAX` not covered
   |
   = note: the matched value is of type `u8`
note: within macro `m`, this `match` expression doesn't expand to cover all patterns
  --> $DIR/exhaustiveness.rs:7:9
   |
LL | / macro_rules! m {
LL | |     ($s:expr, $($t:tt)+) => {
LL | |         match $s { $($t)+ => {} }
   | |         ^^^^^^^^^^^^^^^^^^^^^^^^^
LL | |     }
LL | | }
   | |_-
   = help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern
```
Parentheses surrounding patterns are not ketp in the HIR (and are warned against in the AST). In order to avoid having some suggestions break, we keep the outer span (including parentheses) when lowering the patterns.

```
error: a never pattern is always unreachable
  --> $DIR/ICE-133117-duplicate-never-arm.rs:9:26
   |
LL |         (!|!) if true => {}
   |                          ^^ this will never be executed
   |
help: remove the match arm expression
   |
LL -         (!|!) if true => {}
LL +         (!|!),
   |
```
```
error[E0308]: mismatched types
  --> $DIR/well-typed-edition-2024.rs:135:15
   |
LL |     let [&mut &(mut x)] = &mut [&0];
   |               ^^^^^^^^    --------- this expression has type `&mut [&{integer}; 1]`
   |               |
   |               expected integer, found `&_`
   |
   = note:   expected type `{integer}`
           found reference `&_`
help: consider removing `&` from the pattern
   |
LL -     let [&mut &(mut x)] = &mut [&0];
LL +     let [&mut (mut x)] = &mut [&0];
   |
```
```
error: a never pattern is always unreachable
  --> $DIR/pattern-behind-macro.rs:13:21
   |
LL |         never!() => {}
   |                     ^^ this will never be executed
   |
help: remove the match arm expression
   |
LL -         never!() => {}
LL +         never!(),
   |
```

Look up the macro backtrace call sites to see if we find where the macro was used as a pattern, to properly suggest removing match arm guard and body.

@estebank

compiler-errors

@estebank

estebank

estebank

@bors bors added S-waiting-on-bors

Status: Waiting on bors to run and complete tests. Bors will change the label on completion.

and removed S-waiting-on-author

Status: This is awaiting some action (such as code changes or more information) from the author.

labels

Mar 3, 2025

@bors bors added S-waiting-on-author

Status: This is awaiting some action (such as code changes or more information) from the author.

and removed S-waiting-on-bors

Status: Waiting on bors to run and complete tests. Bors will change the label on completion.

labels

Mar 3, 2025

Kobzol added a commit to Kobzol/rust that referenced this pull request

Aug 14, 2025
…rors

Include whitespace in "remove |" suggestion and make it hidden

Tweak error rendering of patterns with an extra `|` on either end.

Built on rust-lang#137409. Only last commit is relevant.

? `@compiler-errors`

Kobzol added a commit to Kobzol/rust that referenced this pull request

Aug 14, 2025
…rors

Include whitespace in "remove |" suggestion and make it hidden

Tweak error rendering of patterns with an extra `|` on either end.

Built on rust-lang#137409. Only last commit is relevant.

? ``@compiler-errors``

rust-timer added a commit that referenced this pull request

Aug 15, 2025
Rollup merge of #137872 - estebank:extra-vert, r=compiler-errors

Include whitespace in "remove |" suggestion and make it hidden

Tweak error rendering of patterns with an extra `|` on either end.

Built on #137409. Only last commit is relevant.

? ``@compiler-errors``