1
Fork 0

Use more accurate spans when proposing adding lifetime to item

This commit is contained in:
Esteban Kuber 2021-08-12 18:54:29 +00:00
parent 679dea4cc3
commit 14add46e94
17 changed files with 62 additions and 37 deletions

View file

@ -2076,16 +2076,40 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
for param in params {
if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(param.span) {
if snippet.starts_with('&') && !snippet.starts_with("&'") {
introduce_suggestion
.push((param.span, format!("&'a {}", &snippet[1..])));
} else if let Some(stripped) = snippet.strip_prefix("&'_ ") {
introduce_suggestion.push((param.span, format!("&'a {}", &stripped)));
let lo = param.span.lo() + BytePos(1);
let span = param.span.with_lo(lo).with_hi(lo);
introduce_suggestion.push((span, "'a ".to_string()));
} else if let Some(_) = snippet.strip_prefix("&'_ ") {
let lo = param.span.lo() + BytePos(1);
let hi = lo + BytePos(2);
let span = param.span.with_lo(lo).with_hi(hi);
introduce_suggestion.push((span, "'a".to_string()));
}
}
}
for ((span, _), sugg) in spans_with_counts.iter().copied().zip(suggs.iter()) {
if let Some(sugg) = sugg {
introduce_suggestion.push((span, sugg.to_string()));
match (sugg, self.tcx.sess.source_map().span_to_snippet(span)) {
(Some(sugg), Ok(snippet))
if snippet.starts_with('&')
&& !snippet.starts_with("&'")
&& sugg.starts_with("&") =>
{
let lo = span.lo() + BytePos(1);
let span = span.with_lo(lo).with_hi(lo);
introduce_suggestion.push((span, sugg[1..].to_string()));
}
(Some(sugg), Ok(snippet))
if snippet.starts_with("&'_ ") && sugg.starts_with("&") =>
{
let lo = span.lo() + BytePos(1);
let hi = lo + BytePos(2);
let span = span.with_lo(lo).with_hi(hi);
introduce_suggestion.push((span, sugg[1..].to_string()));
}
(Some(sugg), _) => {
introduce_suggestion.push((span, sugg.to_string()));
}
_ => {}
}
}
err.multipart_suggestion_with_style(
@ -2159,7 +2183,8 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
for ((span, _), snippet) in spans_with_counts.iter().copied().zip(snippets.iter()) {
match snippet.as_deref() {
Some("") => spans_suggs.push((span, "'lifetime, ".to_string())),
Some("&") => spans_suggs.push((span, "&'lifetime ".to_string())),
Some("&") => spans_suggs
.push((span.with_lo(span.lo() + BytePos(1)), "'lifetime ".to_string())),
_ => {}
}
}