Skip to content

Commit 12a776b

Browse files
committed
review comment: reduce duplication
1 parent 14add46 commit 12a776b

File tree

1 file changed

+27
-24
lines changed

1 file changed

+27
-24
lines changed

compiler/rustc_resolve/src/late/diagnostics.rs

+27-24
Original file line numberDiff line numberDiff line change
@@ -2073,43 +2073,46 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
20732073
continue;
20742074
}
20752075
});
2076+
let span_unnamed_borrow = |span: Span| {
2077+
let lo = span.lo() + BytePos(1);
2078+
span.with_lo(lo).with_hi(lo)
2079+
};
2080+
let span_underscore_borrow = |span: Span| {
2081+
let lo = span.lo() + BytePos(1);
2082+
let hi = lo + BytePos(2);
2083+
span.with_lo(lo).with_hi(hi)
2084+
};
2085+
let unnamed_borrow =
2086+
|snippet: &str| snippet.starts_with('&') && !snippet.starts_with("&'");
20762087
for param in params {
20772088
if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(param.span) {
2078-
if snippet.starts_with('&') && !snippet.starts_with("&'") {
2079-
let lo = param.span.lo() + BytePos(1);
2080-
let span = param.span.with_lo(lo).with_hi(lo);
2089+
if unnamed_borrow(&snippet) {
2090+
let span = span_unnamed_borrow(param.span);
20812091
introduce_suggestion.push((span, "'a ".to_string()));
2082-
} else if let Some(_) = snippet.strip_prefix("&'_ ") {
2083-
let lo = param.span.lo() + BytePos(1);
2084-
let hi = lo + BytePos(2);
2085-
let span = param.span.with_lo(lo).with_hi(hi);
2092+
} else if snippet.starts_with("&'_ ") {
2093+
let span = span_underscore_borrow(param.span);
20862094
introduce_suggestion.push((span, "'a".to_string()));
20872095
}
20882096
}
20892097
}
2090-
for ((span, _), sugg) in spans_with_counts.iter().copied().zip(suggs.iter()) {
2091-
match (sugg, self.tcx.sess.source_map().span_to_snippet(span)) {
2092-
(Some(sugg), Ok(snippet))
2093-
if snippet.starts_with('&')
2094-
&& !snippet.starts_with("&'")
2095-
&& sugg.starts_with("&") =>
2096-
{
2097-
let lo = span.lo() + BytePos(1);
2098-
let span = span.with_lo(lo).with_hi(lo);
2098+
for (span, sugg) in spans_with_counts.iter().copied().zip(suggs.iter()).filter_map(
2099+
|((span, _), sugg)| match sugg {
2100+
Some(sugg) => Some((span, sugg)),
2101+
_ => None,
2102+
},
2103+
) {
2104+
match self.tcx.sess.source_map().span_to_snippet(span) {
2105+
Ok(snippet) if unnamed_borrow(&snippet) && sugg.starts_with("&") => {
2106+
let span = span_unnamed_borrow(span);
20992107
introduce_suggestion.push((span, sugg[1..].to_string()));
21002108
}
2101-
(Some(sugg), Ok(snippet))
2102-
if snippet.starts_with("&'_ ") && sugg.starts_with("&") =>
2103-
{
2104-
let lo = span.lo() + BytePos(1);
2105-
let hi = lo + BytePos(2);
2106-
let span = span.with_lo(lo).with_hi(hi);
2109+
Ok(snippet) if snippet.starts_with("&'_ ") && sugg.starts_with("&") => {
2110+
let span = span_underscore_borrow(span);
21072111
introduce_suggestion.push((span, sugg[1..].to_string()));
21082112
}
2109-
(Some(sugg), _) => {
2113+
_ => {
21102114
introduce_suggestion.push((span, sugg.to_string()));
21112115
}
2112-
_ => {}
21132116
}
21142117
}
21152118
err.multipart_suggestion_with_style(

0 commit comments

Comments
 (0)