diff --git a/compiler/rustc_hir/src/hir.rs b/compiler/rustc_hir/src/hir.rs index 751c379b21a..b91fb41cb4d 100644 --- a/compiler/rustc_hir/src/hir.rs +++ b/compiler/rustc_hir/src/hir.rs @@ -139,19 +139,6 @@ impl fmt::Display for Lifetime { } } -pub enum LifetimeSuggestionPosition { - /// The user wrote `'a` or `'_`. - Normal, - /// The user wrote `&type` or `&mut type`. - Ampersand, - /// The user wrote `Path` and omitted the `<'_>`. - ElidedPath, - /// The user wrote `Path`, and omitted the `'_,`. - ElidedPathArgument, - /// The user wrote `dyn Trait` and omitted the `+ '_`. - ObjectDefault, -} - impl Lifetime { pub fn is_elided(&self) -> bool { self.res.is_elided() @@ -161,33 +148,27 @@ impl Lifetime { self.ident.name == kw::Empty || self.ident.name == kw::UnderscoreLifetime } - pub fn suggestion_position(&self) -> (LifetimeSuggestionPosition, Span) { - if self.ident.name == kw::Empty { - if self.ident.span.is_empty() { - (LifetimeSuggestionPosition::ElidedPathArgument, self.ident.span) - } else { - (LifetimeSuggestionPosition::ElidedPath, self.ident.span.shrink_to_hi()) - } - } else if self.res == LifetimeName::ImplicitObjectLifetimeDefault { - (LifetimeSuggestionPosition::ObjectDefault, self.ident.span) - } else if self.ident.span.is_empty() { - (LifetimeSuggestionPosition::Ampersand, self.ident.span) - } else { - (LifetimeSuggestionPosition::Normal, self.ident.span) - } - } - pub fn suggestion(&self, new_lifetime: &str) -> (Span, String) { debug_assert!(new_lifetime.starts_with('\'')); - let (pos, span) = self.suggestion_position(); - let code = match pos { - LifetimeSuggestionPosition::Normal => format!("{new_lifetime}"), - LifetimeSuggestionPosition::Ampersand => format!("{new_lifetime} "), - LifetimeSuggestionPosition::ElidedPath => format!("<{new_lifetime}>"), - LifetimeSuggestionPosition::ElidedPathArgument => format!("{new_lifetime}, "), - LifetimeSuggestionPosition::ObjectDefault => format!("+ {new_lifetime}"), - }; - (span, code) + + if self.ident.name == kw::Empty { + if self.ident.span.is_empty() { + // The user wrote `Path`, and omitted the `'_,`. + (self.ident.span, format!("{new_lifetime}, ")) + } else { + // The user wrote `Path` and omitted the `<'_>`. + (self.ident.span.shrink_to_hi(), format!("<{new_lifetime}>")) + } + } else if self.res == LifetimeName::ImplicitObjectLifetimeDefault { + // The user wrote `dyn Trait` and omitted the `+ '_`. + (self.ident.span, format!("+ {new_lifetime}")) + } else if self.ident.span.is_empty() { + // The user wrote `&type` or `&mut type`. + (self.ident.span, format!("{new_lifetime} ")) + } else { + // The user wrote `'a` or `'_`. + (self.ident.span, format!("{new_lifetime}")) + } } }