From a9e13fa5533df7fc004aeab00d45822eacb8461e Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Thu, 10 Mar 2022 23:12:35 +0100 Subject: Lint elided lifetimes in path on the AST. --- compiler/rustc_errors/src/lib.rs | 38 ++++++++++---------------------------- 1 file changed, 10 insertions(+), 28 deletions(-) (limited to 'compiler/rustc_errors/src') diff --git a/compiler/rustc_errors/src/lib.rs b/compiler/rustc_errors/src/lib.rs index 416bc4e2e3b..d7c5df7d8e2 100644 --- a/compiler/rustc_errors/src/lib.rs +++ b/compiler/rustc_errors/src/lib.rs @@ -1511,35 +1511,17 @@ pub fn add_elided_lifetime_in_path_suggestion( path_span: Span, incl_angl_brckt: bool, insertion_span: Span, - anon_lts: String, ) { - let (replace_span, suggestion) = if incl_angl_brckt { - (insertion_span, anon_lts) - } else { - // When possible, prefer a suggestion that replaces the whole - // `Path` expression with `Path<'_, T>`, rather than inserting `'_, ` - // at a point (which makes for an ugly/confusing label) - if let Ok(snippet) = source_map.span_to_snippet(path_span) { - // But our spans can get out of whack due to macros; if the place we think - // we want to insert `'_` isn't even within the path expression's span, we - // should bail out of making any suggestion rather than panicking on a - // subtract-with-overflow or string-slice-out-out-bounds (!) - // FIXME: can we do better? - if insertion_span.lo().0 < path_span.lo().0 { - return; - } - let insertion_index = (insertion_span.lo().0 - path_span.lo().0) as usize; - if insertion_index > snippet.len() { - return; - } - let (before, after) = snippet.split_at(insertion_index); - (path_span, format!("{}{}{}", before, anon_lts, after)) - } else { - (insertion_span, anon_lts) - } - }; - diag.span_suggestion( - replace_span, + diag.span_label(path_span, format!("expected lifetime parameter{}", pluralize!(n))); + if source_map.span_to_snippet(insertion_span).is_err() { + // Do not try to suggest anything if generated by a proc-macro. + return; + } + let anon_lts = vec!["'_"; n].join(", "); + let suggestion = + if incl_angl_brckt { format!("<{}>", anon_lts) } else { format!("{}, ", anon_lts) }; + diag.span_suggestion_verbose( + insertion_span.shrink_to_hi(), &format!("indicate the anonymous lifetime{}", pluralize!(n)), suggestion, Applicability::MachineApplicable, -- cgit 1.4.1-3-g733a5