-
Notifications
You must be signed in to change notification settings - Fork 13.3k
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
base: master
Are you sure you want to change the base?
Conversation
Some changes occurred in match checking cc @Nadrieril |
tests/ui/pattern/usefulness/empty-match.exhaustive_patterns.stderr
Outdated
Show resolved
Hide resolved
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Some changes occurred in src/tools/clippy cc @rust-lang/clippy |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
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| { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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)
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done (amended last commit).
r=me when CI is green |
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 |
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) |
There was a problem hiding this comment.
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
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 |
There was a problem hiding this comment.
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 :-/
@bors r=compiler-errors |
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 r- |
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.
LL - ! => || { | ||
LL - | ||
LL - | ||
LL - use std::ops::Add; | ||
LL - 0.add(1) | ||
LL - }, | ||
LL + !, |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
help: remove the `|` | ||
| | ||
LL - ! | | ||
LL + ! | ||
| |
There was a problem hiding this comment.
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.
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. |
@bors r=compiler-errors |
🌲 The tree is currently closed for pull requests below priority 102. This pull request will be tested once the tree is reopened. |
@bors r- needs tracking issue |
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:
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.