-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
Don't show suggestion if slice pattern is not top-level #121236
Don't show suggestion if slice pattern is not top-level #121236
Conversation
r? @Nadrieril rustbot has assigned @Nadrieril. Use r? to explicitly pick a reviewer |
This comment has been minimized.
This comment has been minimized.
Hi! Thanks for your contribution. Let me know with @rustbot author |
b1a6410
to
9e5ade0
Compare
My first question is: the example you show gives a correct suggestion: to match on Could you add two tests: one with the example you mention, and one with the example in #120605 so we can see how your code handles them? |
The reason why I use # self.a without [..]
-> % rustc +stage1 test.rs
error[E0529]: expected an array or slice, found `Vec<Struct>`
--> test.rs:8:16
|
8 | if let [Struct { a: [] }] = &self.a {
| ^^^^^^^^^^^^^^^^^^ ------- help: consider slicing here: `&self.a[..]`
| |
| pattern cannot match with input type `Vec<Struct>`
error[E0529]: expected an array or slice, found `Vec<Struct>`
--> test.rs:8:29
|
8 | if let [Struct { a: [] }] = &self.a {
| ^^ pattern cannot match with input type `Vec<Struct>`
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0529`. # self.a with [..]
-> % rustc +stage1 test.rs
error[E0529]: expected an array or slice, found `Vec<Struct>`
--> test.rs:8:29
|
8 | if let [Struct { a: [] }] = &self.a[..] {
| ^^ pattern cannot match with input type `Vec<Struct>`
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0529`.
Yes, I will add them today. |
9e5ade0
to
300ca5c
Compare
I added a test for the issue. Could you review? @rustbot ready |
This comment has been minimized.
This comment has been minimized.
@rustbot author |
a74c295
to
fe143a2
Compare
Thank you! @bors r+ rollup |
Oops wait, could you squash all commits into one please? @bors r- |
04c966b
to
78e94cb
Compare
I squashed all commits. Thank you for reviewing. |
👍 @bors r+ rollup |
…iaskrgr Rollup of 3 pull requests Successful merges: - rust-lang#121236 (Don't show suggestion if slice pattern is not top-level) - rust-lang#121787 (run change tracker even when config parse fails) - rust-lang#122633 (avoid unnecessary collect()) r? `@ghost` `@rustbot` modify labels: rollup
Rollup merge of rust-lang#121236 - long-long-float:rust-fix-consider-slicing, r=Nadrieril Don't show suggestion if slice pattern is not top-level Close rust-lang#120605 Don't show suggestion to add slicing (`[..]`) if the slice pattern is enclosed by struct like `Struct { a: [] }`. For example, current rustc makes a suggestion as a comment. However, the pattern `a: []` is wrong, not scrutinee `&self.a`. In this case, the structure type `a: Vec<Struct>` and the pattern `a: []` are different so I think the pattern should be fixed, not the scrutinee. If the parent of the pattern that was the target of the error is a structure, I made the compiler not show a suggestion. ```rs pub struct Struct { a: Vec<Struct>, } impl Struct { pub fn test(&self) { if let [Struct { a: [] }] = &self.a { // ^^^^^^^^^^^^^^^^^^ ------- help: consider slicing here: `&self.a[..]` println!("matches!") } } } ``` Note: * ~~I created `PatInfo.history` to store parent-child relationships for patterns, but this may be inefficient.~~ * I use two fields `parent_kind` and `current_kind` instead of vec. It may not performance issue. * Currently only looking at direct parents, but may need to look at deeper ancestry.
Close #120605
Don't show suggestion to add slicing (
[..]
) if the slice pattern is enclosed by struct likeStruct { a: [] }
.For example, current rustc makes a suggestion as a comment. However, the pattern
a: []
is wrong, not scrutinee&self.a
.In this case, the structure type
a: Vec<Struct>
and the patterna: []
are different so I think the pattern should be fixed, not the scrutinee.If the parent of the pattern that was the target of the error is a structure, I made the compiler not show a suggestion.
Note:
I createdPatInfo.history
to store parent-child relationships for patterns, but this may be inefficient.parent_kind
andcurrent_kind
instead of vec. It may not performance issue.