|
1 | 1 | mod builtin_type_shadow; |
2 | 2 | mod double_neg; |
3 | 3 | mod redundant_pattern; |
| 4 | +mod unneeded_field_pattern; |
4 | 5 | mod unneeded_wildcard_pattern; |
5 | 6 |
|
6 | | -use clippy_utils::diagnostics::{span_lint, span_lint_and_help, span_lint_and_sugg, span_lint_and_then}; |
| 7 | +use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg, span_lint_and_then}; |
7 | 8 | use clippy_utils::source::snippet_opt; |
8 | 9 | use rustc_ast::ast::{Expr, Generics, Lit, LitFloatType, LitIntType, LitKind, NodeId, Pat, PatKind}; |
9 | 10 | use rustc_ast::visit::FnKind; |
@@ -271,71 +272,7 @@ impl EarlyLintPass for MiscEarlyLints { |
271 | 272 | } |
272 | 273 |
|
273 | 274 | fn check_pat(&mut self, cx: &EarlyContext<'_>, pat: &Pat) { |
274 | | - if let PatKind::Struct(ref npat, ref pfields, _) = pat.kind { |
275 | | - let mut wilds = 0; |
276 | | - let type_name = npat |
277 | | - .segments |
278 | | - .last() |
279 | | - .expect("A path must have at least one segment") |
280 | | - .ident |
281 | | - .name; |
282 | | - |
283 | | - for field in pfields { |
284 | | - if let PatKind::Wild = field.pat.kind { |
285 | | - wilds += 1; |
286 | | - } |
287 | | - } |
288 | | - if !pfields.is_empty() && wilds == pfields.len() { |
289 | | - span_lint_and_help( |
290 | | - cx, |
291 | | - UNNEEDED_FIELD_PATTERN, |
292 | | - pat.span, |
293 | | - "all the struct fields are matched to a wildcard pattern, consider using `..`", |
294 | | - None, |
295 | | - &format!("try with `{} {{ .. }}` instead", type_name), |
296 | | - ); |
297 | | - return; |
298 | | - } |
299 | | - if wilds > 0 { |
300 | | - for field in pfields { |
301 | | - if let PatKind::Wild = field.pat.kind { |
302 | | - wilds -= 1; |
303 | | - if wilds > 0 { |
304 | | - span_lint( |
305 | | - cx, |
306 | | - UNNEEDED_FIELD_PATTERN, |
307 | | - field.span, |
308 | | - "you matched a field with a wildcard pattern, consider using `..` instead", |
309 | | - ); |
310 | | - } else { |
311 | | - let mut normal = vec![]; |
312 | | - |
313 | | - for field in pfields { |
314 | | - match field.pat.kind { |
315 | | - PatKind::Wild => {}, |
316 | | - _ => { |
317 | | - if let Ok(n) = cx.sess().source_map().span_to_snippet(field.span) { |
318 | | - normal.push(n); |
319 | | - } |
320 | | - }, |
321 | | - } |
322 | | - } |
323 | | - |
324 | | - span_lint_and_help( |
325 | | - cx, |
326 | | - UNNEEDED_FIELD_PATTERN, |
327 | | - field.span, |
328 | | - "you matched a field with a wildcard pattern, consider using `..` \ |
329 | | - instead", |
330 | | - None, |
331 | | - &format!("try with `{} {{ {}, .. }}`", type_name, normal[..].join(", ")), |
332 | | - ); |
333 | | - } |
334 | | - } |
335 | | - } |
336 | | - } |
337 | | - } |
338 | | - |
| 275 | + unneeded_field_pattern::check(cx, pat); |
339 | 276 | redundant_pattern::check(cx, pat); |
340 | 277 | unneeded_wildcard_pattern::check(cx, pat); |
341 | 278 | } |
|
0 commit comments