mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-17 01:13:11 +00:00
review comment: use newtype to deduplicate logic
This commit is contained in:
parent
12a776b41d
commit
9349046ed5
@ -2073,48 +2073,86 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
let span_unnamed_borrow = |span: Span| {
|
|
||||||
let lo = span.lo() + BytePos(1);
|
struct Lifetime(Span, String);
|
||||||
span.with_lo(lo).with_hi(lo)
|
impl Lifetime {
|
||||||
};
|
fn is_unnamed(&self) -> bool {
|
||||||
let span_underscore_borrow = |span: Span| {
|
self.1.starts_with('&') && !self.1.starts_with("&'")
|
||||||
let lo = span.lo() + BytePos(1);
|
}
|
||||||
|
fn is_underscore(&self) -> bool {
|
||||||
|
self.1.starts_with("&'_ ")
|
||||||
|
}
|
||||||
|
fn is_named(&self) -> bool {
|
||||||
|
self.1.starts_with("&'")
|
||||||
|
}
|
||||||
|
fn suggestion(&self, sugg: String) -> Option<(Span, String)> {
|
||||||
|
Some(
|
||||||
|
match (
|
||||||
|
self.is_unnamed(),
|
||||||
|
self.is_underscore(),
|
||||||
|
self.is_named(),
|
||||||
|
sugg.starts_with("&"),
|
||||||
|
) {
|
||||||
|
(true, _, _, false) => (self.span_unnamed_borrow(), sugg),
|
||||||
|
(true, _, _, true) => {
|
||||||
|
(self.span_unnamed_borrow(), sugg[1..].to_string())
|
||||||
|
}
|
||||||
|
(_, true, _, false) => {
|
||||||
|
(self.span_underscore_borrow(), sugg.trim().to_string())
|
||||||
|
}
|
||||||
|
(_, true, _, true) => {
|
||||||
|
(self.span_underscore_borrow(), sugg[1..].trim().to_string())
|
||||||
|
}
|
||||||
|
(_, _, true, false) => {
|
||||||
|
(self.span_named_borrow(), sugg.trim().to_string())
|
||||||
|
}
|
||||||
|
(_, _, true, true) => {
|
||||||
|
(self.span_named_borrow(), sugg[1..].trim().to_string())
|
||||||
|
}
|
||||||
|
_ => return None,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
fn span_unnamed_borrow(&self) -> Span {
|
||||||
|
let lo = self.0.lo() + BytePos(1);
|
||||||
|
self.0.with_lo(lo).with_hi(lo)
|
||||||
|
}
|
||||||
|
fn span_named_borrow(&self) -> Span {
|
||||||
|
let lo = self.0.lo() + BytePos(1);
|
||||||
|
self.0.with_lo(lo)
|
||||||
|
}
|
||||||
|
fn span_underscore_borrow(&self) -> Span {
|
||||||
|
let lo = self.0.lo() + BytePos(1);
|
||||||
let hi = lo + BytePos(2);
|
let hi = lo + BytePos(2);
|
||||||
span.with_lo(lo).with_hi(hi)
|
self.0.with_lo(lo).with_hi(hi)
|
||||||
};
|
}
|
||||||
let unnamed_borrow =
|
}
|
||||||
|snippet: &str| snippet.starts_with('&') && !snippet.starts_with("&'");
|
|
||||||
for param in params {
|
for param in params {
|
||||||
if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(param.span) {
|
if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(param.span) {
|
||||||
if unnamed_borrow(&snippet) {
|
if let Some((span, sugg)) =
|
||||||
let span = span_unnamed_borrow(param.span);
|
Lifetime(param.span, snippet).suggestion("'a ".to_string())
|
||||||
introduce_suggestion.push((span, "'a ".to_string()));
|
{
|
||||||
} else if snippet.starts_with("&'_ ") {
|
introduce_suggestion.push((span, sugg));
|
||||||
let span = span_underscore_borrow(param.span);
|
|
||||||
introduce_suggestion.push((span, "'a".to_string()));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (span, sugg) in spans_with_counts.iter().copied().zip(suggs.iter()).filter_map(
|
for (span, sugg) in spans_with_counts.iter().copied().zip(suggs.iter()).filter_map(
|
||||||
|((span, _), sugg)| match sugg {
|
|((span, _), sugg)| match &sugg {
|
||||||
Some(sugg) => Some((span, sugg)),
|
Some(sugg) => Some((span, sugg.to_string())),
|
||||||
_ => None,
|
_ => None,
|
||||||
},
|
},
|
||||||
) {
|
) {
|
||||||
match self.tcx.sess.source_map().span_to_snippet(span) {
|
let (span, sugg) = self
|
||||||
Ok(snippet) if unnamed_borrow(&snippet) && sugg.starts_with("&") => {
|
.tcx
|
||||||
let span = span_unnamed_borrow(span);
|
.sess
|
||||||
introduce_suggestion.push((span, sugg[1..].to_string()));
|
.source_map()
|
||||||
}
|
.span_to_snippet(span)
|
||||||
Ok(snippet) if snippet.starts_with("&'_ ") && sugg.starts_with("&") => {
|
.ok()
|
||||||
let span = span_underscore_borrow(span);
|
.and_then(|snippet| Lifetime(span, snippet).suggestion(sugg.clone()))
|
||||||
introduce_suggestion.push((span, sugg[1..].to_string()));
|
.unwrap_or((span, sugg));
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
introduce_suggestion.push((span, sugg.to_string()));
|
introduce_suggestion.push((span, sugg.to_string()));
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
err.multipart_suggestion_with_style(
|
err.multipart_suggestion_with_style(
|
||||||
&msg,
|
&msg,
|
||||||
introduce_suggestion,
|
introduce_suggestion,
|
||||||
|
Loading…
Reference in New Issue
Block a user