Remove LifetimeSuggestionPosition and Lifetime::suggestion_position.

They both are only used in `Lifetime::suggestion`. This commit inlines
and removes them.
This commit is contained in:
Nicholas Nethercote 2025-03-25 08:56:33 +11:00
parent 4924e0a067
commit c6d8d65496

View file

@ -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<T>`, 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<T>`, 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}"))
}
}
}