Skip to content
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

Tweak comma handling of "missing match arm" suggestion and fix "remove this arm" suggestion, and make suggestion verbose #137409

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

estebank
Copy link
Contributor

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 #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.

@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
@rustbot
Copy link
Collaborator

rustbot commented Feb 22, 2025

Some changes occurred in match checking

cc @Nadrieril

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rustbot
Copy link
Collaborator

rustbot commented Feb 22, 2025

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@@ -676,6 +676,23 @@ impl<'hir> LoweringContext<'_, 'hir> {
{
self.lower_expr(body)
} else {
let removal_span = |mut removal_span: Span| {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this necessary? Can you use find_ancestor_in_same_ctxt instead of cooking it yourself?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should be able to do like... pat_span.find_ancestor_in_same_ctxt(arm_span).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done (amended last commit).

@compiler-errors
Copy link
Member

r=me when CI is green

@compiler-errors
Copy link
Member

Also @estebank please for this PR and all others in the future, can you make sure to note that you're making these suggestions verbose. Seems like a major thing to leave out when that's presumably your underlying motivation here. Thx

@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
Comment on lines +537 to +561
error[E0004]: non-exhaustive patterns: `[]` not covered
--> $DIR/empty-match.rs:81:15
|
LL ~ _ if false => {},
LL + [] => todo!()
LL | indirect!(arrayN_of_empty);
| ^^^^^^^^^^^^^^^ pattern `[]` not covered
|
= note: the matched value is of type `[!; N]`
note: within macro `match_guarded_arm`, this `match` expression doesn't expand to cover all patterns
--> $DIR/empty-match.rs:16:13
|
LL | / macro_rules! match_guarded_arm {
LL | | ($e:expr) => {
LL | |/ match $e {
LL | || _ if false => {}
LL | || }
| ||_____________^
LL | | };
LL | | }
| |______-
...
LL | indirect!(arrayN_of_empty);
| -------------------------- in this macro invocation
= note: match arms with guards don't count towards exhaustivity
= help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern
= note: this error originates in the macro `indirect` (in Nightly builds, run with -Z macro-backtrace for more info)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Despite the verbosity of this output, I am happy with how much context it provides in a really tricky case. With macro-backtrace, the output is outright great (IMO):

error[E0004]: non-exhaustive patterns: `[]` not covered
  --> tests/ui/pattern/usefulness/empty-match.rs:81:15
   |
81 |     indirect!(arrayN_of_empty); //~ ERROR `[]` not covered
   |               ^^^^^^^^^^^^^^^ pattern `[]` not covered
   |
   = note: the matched value is of type `[!; N]`
note: within macro `match_guarded_arm`, this `match` expression doesn't expand to cover all patterns
  --> tests/ui/pattern/usefulness/empty-match.rs:16:13
   |
14 | /      macro_rules! match_guarded_arm {
15 | |          ($e:expr) => {
16 | |/             match $e {
17 | ||                 _ if false => {}
18 | ||             }
   | ||_____________^
19 | |          };
20 | |      }
   | |______-
21 |
22 |  /     macro_rules! indirect {
23 |  |         ($e:expr) => {
24 |  |             match_guarded_arm!($e)
25 |  |         };
26 |  |     }
   |  |_____- in this expansion of `indirect!`
...
81 |        indirect!(arrayN_of_empty); //~ ERROR `[]` not covered
   |        -------------------------- in this macro invocation
   = note: match arms with guards don't count towards exhaustivity
   = help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern

Screenshot_20250228_091633

Comment on lines 12 to +15
LL | let ref mut a @ (ref b @ ref mut c) = u(); // sub-in-sub
| ^^^^^^^^^ ----- --------- value is mutably borrowed by `c` here
| | |
| | value is borrowed by `b` here
| ^^^^^^^^^ ------ --------- value is mutably borrowed by `c` here
| | |
| | value is borrowed by `b` here
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is technically a regression that I haven't figured out how to get rid of without breaking anything else :-/

@estebank
Copy link
Contributor Author

@bors r=compiler-errors

@bors
Copy link
Collaborator

bors commented Feb 28, 2025

📌 Commit 88398eb has been approved by compiler-errors

It is now in the queue for this repository.

@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.
@matthiaskrgr
Copy link
Member

@bors r-
needs rebase for tests/ui/never_type/unused_trait_in_never_pattern_body.rs from what I see

@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
estebank added 7 commits March 1, 2025 17:30
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).
```
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.
Comment on lines 25 to 31
LL - ! => || {
LL -
LL -
LL - use std::ops::Add;
LL - 0.add(1)
LL - },
LL + !,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a lot of lines, to the point that it's a bit hard to even see (without coloration) that this is even suggesting to leave the plain ! and remove everything else. we should probably make this suggestion hidden?

| help: remove this expression
| ^^ this will never be executed
|
= help: remove the match arm expression
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This probably be "remove the match arm guard and expression" now.

Comment on lines +9 to +13
help: remove the `|`
|
LL - ! |
LL + !
|
Copy link
Contributor Author

@estebank estebank Mar 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix for the left-over whitespace coming soon. Edit: #137872.

@compiler-errors
Copy link
Member

Do you plan on tweaking this further? If not, please open a tracking issue (https://rust-lang.zulipchat.com/#narrow/channel/147480-t-compiler.2Fwg-diagnostics/topic/Why.20are.20we.20making.20suggestions.20verbose.3F/near/502691049) and it would be nice to cc that on this PR and any further PRs having to do with verbose-ifying diagnostics. r=me afterwards.

@estebank
Copy link
Contributor Author

estebank commented Mar 3, 2025

@bors r=compiler-errors

@bors
Copy link
Collaborator

bors commented Mar 3, 2025

📌 Commit 7e1f8f2 has been approved by compiler-errors

It is now in the queue for this repository.

@bors
Copy link
Collaborator

bors commented Mar 3, 2025

🌲 The tree is currently closed for pull requests below priority 102. This pull request will be tested once the tree is reopened.

@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
@compiler-errors
Copy link
Member

@bors r-

needs tracking issue

@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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants