Commit 3ec2b7b
authored
Rollup merge of #121236 - long-long-float:rust-fix-consider-slicing, r=Nadrieril
Don't show suggestion if slice pattern is not top-level
Close #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.File tree
3 files changed
+70
-13
lines changed- compiler/rustc_hir_typeck/src
- tests/ui/suggestions
3 files changed
+70
-13
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
83 | 83 | | |
84 | 84 | | |
85 | 85 | | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
86 | 89 | | |
87 | 90 | | |
88 | 91 | | |
| |||
152 | 155 | | |
153 | 156 | | |
154 | 157 | | |
155 | | - | |
| 158 | + | |
| 159 | + | |
156 | 160 | | |
157 | 161 | | |
158 | 162 | | |
| |||
163 | 167 | | |
164 | 168 | | |
165 | 169 | | |
166 | | - | |
| 170 | + | |
| 171 | + | |
167 | 172 | | |
168 | 173 | | |
169 | 174 | | |
| |||
172 | 177 | | |
173 | 178 | | |
174 | 179 | | |
175 | | - | |
176 | | - | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
177 | 186 | | |
178 | 187 | | |
179 | 188 | | |
| |||
1046 | 1055 | | |
1047 | 1056 | | |
1048 | 1057 | | |
1049 | | - | |
| 1058 | + | |
1050 | 1059 | | |
1051 | 1060 | | |
1052 | 1061 | | |
1053 | 1062 | | |
1054 | 1063 | | |
1055 | 1064 | | |
1056 | | - | |
| 1065 | + | |
1057 | 1066 | | |
1058 | 1067 | | |
1059 | 1068 | | |
| |||
1120 | 1129 | | |
1121 | 1130 | | |
1122 | 1131 | | |
1123 | | - | |
| 1132 | + | |
1124 | 1133 | | |
1125 | 1134 | | |
1126 | 1135 | | |
| |||
2134 | 2143 | | |
2135 | 2144 | | |
2136 | 2145 | | |
2137 | | - | |
| 2146 | + | |
2138 | 2147 | | |
2139 | 2148 | | |
2140 | 2149 | | |
| |||
2273 | 2282 | | |
2274 | 2283 | | |
2275 | 2284 | | |
2276 | | - | |
| 2285 | + | |
2277 | 2286 | | |
| 2287 | + | |
| 2288 | + | |
2278 | 2289 | | |
2279 | 2290 | | |
2280 | 2291 | | |
| |||
2292 | 2303 | | |
2293 | 2304 | | |
2294 | 2305 | | |
2295 | | - | |
2296 | | - | |
2297 | | - | |
| 2306 | + | |
| 2307 | + | |
| 2308 | + | |
| 2309 | + | |
2298 | 2310 | | |
2299 | 2311 | | |
2300 | 2312 | | |
| |||
2309 | 2321 | | |
2310 | 2322 | | |
2311 | 2323 | | |
2312 | | - | |
| 2324 | + | |
| 2325 | + | |
| 2326 | + | |
2313 | 2327 | | |
2314 | 2328 | | |
2315 | 2329 | | |
| |||
Lines changed: 20 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
Lines changed: 23 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
0 commit comments