review comment: reduce duplication

This commit is contained in:
Esteban Kuber 2021-08-18 11:14:33 +00:00
parent 14add46e94
commit 12a776b41d

View file

@ -2073,43 +2073,46 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
continue; continue;
} }
}); });
let span_unnamed_borrow = |span: Span| {
let lo = span.lo() + BytePos(1);
span.with_lo(lo).with_hi(lo)
};
let span_underscore_borrow = |span: Span| {
let lo = span.lo() + BytePos(1);
let hi = lo + BytePos(2);
span.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 snippet.starts_with('&') && !snippet.starts_with("&'") { if unnamed_borrow(&snippet) {
let lo = param.span.lo() + BytePos(1); let span = span_unnamed_borrow(param.span);
let span = param.span.with_lo(lo).with_hi(lo);
introduce_suggestion.push((span, "'a ".to_string())); introduce_suggestion.push((span, "'a ".to_string()));
} else if let Some(_) = snippet.strip_prefix("&'_ ") { } else if snippet.starts_with("&'_ ") {
let lo = param.span.lo() + BytePos(1); let span = span_underscore_borrow(param.span);
let hi = lo + BytePos(2);
let span = param.span.with_lo(lo).with_hi(hi);
introduce_suggestion.push((span, "'a".to_string())); introduce_suggestion.push((span, "'a".to_string()));
} }
} }
} }
for ((span, _), sugg) in spans_with_counts.iter().copied().zip(suggs.iter()) { for (span, sugg) in spans_with_counts.iter().copied().zip(suggs.iter()).filter_map(
match (sugg, self.tcx.sess.source_map().span_to_snippet(span)) { |((span, _), sugg)| match sugg {
(Some(sugg), Ok(snippet)) Some(sugg) => Some((span, sugg)),
if snippet.starts_with('&') _ => None,
&& !snippet.starts_with("&'") },
&& sugg.starts_with("&") => ) {
{ match self.tcx.sess.source_map().span_to_snippet(span) {
let lo = span.lo() + BytePos(1); Ok(snippet) if unnamed_borrow(&snippet) && sugg.starts_with("&") => {
let span = span.with_lo(lo).with_hi(lo); let span = span_unnamed_borrow(span);
introduce_suggestion.push((span, sugg[1..].to_string())); introduce_suggestion.push((span, sugg[1..].to_string()));
} }
(Some(sugg), Ok(snippet)) Ok(snippet) if snippet.starts_with("&'_ ") && sugg.starts_with("&") => {
if snippet.starts_with("&'_ ") && sugg.starts_with("&") => let span = span_underscore_borrow(span);
{
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())); introduce_suggestion.push((span, sugg[1..].to_string()));
} }
(Some(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(