|
1 |
| -use clippy_utils::diagnostics::span_lint_and_help; |
2 |
| -use rustc_ast::ast::{Pat, PatFieldsRest, PatKind}; |
3 |
| -use rustc_lint::{EarlyContext, EarlyLintPass}; |
| 1 | +use crate::rustc_lint::LintContext as _; |
| 2 | +use clippy_utils::diagnostics::span_lint_and_then; |
| 3 | +use itertools::Itertools; |
| 4 | +use rustc_lint::LateLintPass; |
| 5 | +use rustc_middle::ty; |
4 | 6 | use rustc_session::declare_lint_pass;
|
5 | 7 |
|
6 | 8 | declare_clippy_lint! {
|
@@ -41,17 +43,57 @@ declare_clippy_lint! {
|
41 | 43 | }
|
42 | 44 | declare_lint_pass!(RestWhenDestructuringStruct => [REST_WHEN_DESTRUCTURING_STRUCT]);
|
43 | 45 |
|
44 |
| -impl EarlyLintPass for RestWhenDestructuringStruct { |
45 |
| - fn check_pat(&mut self, cx: &EarlyContext<'_>, pat: &Pat) { |
46 |
| - if let PatKind::Struct(_, _, _, PatFieldsRest::Rest) = pat.kind { |
47 |
| - span_lint_and_help( |
48 |
| - cx, |
49 |
| - REST_WHEN_DESTRUCTURING_STRUCT, |
50 |
| - pat.span, |
51 |
| - "struct destructuring with rest (..)", |
52 |
| - None, |
53 |
| - "consider explicitly ignoring remaining fields with wildcard patterns (x: _)", |
54 |
| - ); |
| 46 | +impl<'tcx> LateLintPass<'tcx> for RestWhenDestructuringStruct { |
| 47 | + fn check_pat(&mut self, cx: &rustc_lint::LateContext<'tcx>, pat: &'tcx rustc_hir::Pat<'tcx>) { |
| 48 | + if pat.span.in_external_macro(cx.tcx.sess.source_map()) { |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + if let rustc_hir::PatKind::Struct(path, fields, true) = pat.kind { |
| 53 | + let qty = cx.typeck_results().qpath_res(&path, pat.hir_id); |
| 54 | + let ty = cx.typeck_results().pat_ty(pat); |
| 55 | + if let ty::Adt(a, _) = ty.kind() { |
| 56 | + let vid = a.variant_index_with_id(qty.def_id()); |
| 57 | + let mut rest_fields = a.variants()[vid] |
| 58 | + .fields |
| 59 | + .iter() |
| 60 | + .map(|field| field.ident(cx.tcx)) |
| 61 | + .filter(|pf| !fields.iter().any(|x| x.ident == *pf)) |
| 62 | + .map(|x| format!("{x}: _")); |
| 63 | + let fmt_fields = rest_fields.join(", "); |
| 64 | + |
| 65 | + let sm = cx.sess().source_map(); |
| 66 | + |
| 67 | + // It is not possible to get the span of the et cetera symbol at HIR level |
| 68 | + // so we have to get it in a bit of a roundabout way: |
| 69 | + |
| 70 | + // Find the end of the last field if any. |
| 71 | + let last_field = fields.iter().last().map(|x| x.span.shrink_to_hi()); |
| 72 | + // If no last field take the whole pattern. |
| 73 | + let last_field = last_field.unwrap_or(pat.span.shrink_to_lo()); |
| 74 | + // Create a new span starting and ending just before the first . |
| 75 | + let before_dot = sm.span_extend_to_next_char(last_field, '.', true).shrink_to_hi(); |
| 76 | + // Extend the span to the end of the line |
| 77 | + let rest_of_line = sm.span_extend_to_next_char(before_dot, '\n', true); |
| 78 | + // Shrink the span so it only contains dots |
| 79 | + let dotdot = sm.span_take_while(rest_of_line, |x| *x == '.'); |
| 80 | + |
| 81 | + span_lint_and_then( |
| 82 | + cx, |
| 83 | + REST_WHEN_DESTRUCTURING_STRUCT, |
| 84 | + pat.span, |
| 85 | + "struct destructuring with rest (..)", |
| 86 | + |diag| { |
| 87 | + // println!("{:?}", pat); |
| 88 | + diag.span_suggestion_verbose( |
| 89 | + dotdot, |
| 90 | + "consider explicitly ignoring remaining fields with wildcard patterns (x: _)", |
| 91 | + fmt_fields, |
| 92 | + rustc_errors::Applicability::MachineApplicable, |
| 93 | + ); |
| 94 | + }, |
| 95 | + ); |
| 96 | + } |
55 | 97 | }
|
56 | 98 | }
|
57 | 99 | }
|
0 commit comments