about summary refs log tree commit diff
path: root/compiler/rustc_resolve/src
diff options
context:
space:
mode:
authorEsteban Kuber <esteban@kuber.com.ar>2021-08-18 11:14:33 +0000
committerEsteban Kuber <esteban@kuber.com.ar>2021-08-18 11:29:29 +0000
commit12a776b41d4ed9bd645cfd7bfb4eb3f95ffabd97 (patch)
tree9b619f2d8df214aa88100d18ffdf9051778ff317 /compiler/rustc_resolve/src
parent14add46e94aa57d6f659ccfb5401e00ffacddc0b (diff)
downloadrust-12a776b41d4ed9bd645cfd7bfb4eb3f95ffabd97.tar.gz
rust-12a776b41d4ed9bd645cfd7bfb4eb3f95ffabd97.zip
review comment: reduce duplication
Diffstat (limited to 'compiler/rustc_resolve/src')
-rw-r--r--compiler/rustc_resolve/src/late/diagnostics.rs51
1 files changed, 27 insertions, 24 deletions
diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs
index ac77ec1be78..10f74dc1a04 100644
--- a/compiler/rustc_resolve/src/late/diagnostics.rs
+++ b/compiler/rustc_resolve/src/late/diagnostics.rs
@@ -2073,43 +2073,46 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
                         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 {
                     if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(param.span) {
-                        if snippet.starts_with('&') && !snippet.starts_with("&'") {
-                            let lo = param.span.lo() + BytePos(1);
-                            let span = param.span.with_lo(lo).with_hi(lo);
+                        if unnamed_borrow(&snippet) {
+                            let span = span_unnamed_borrow(param.span);
                             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);
+                        } else if snippet.starts_with("&'_ ") {
+                            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()) {
-                    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);
+                for (span, sugg) in spans_with_counts.iter().copied().zip(suggs.iter()).filter_map(
+                    |((span, _), sugg)| match sugg {
+                        Some(sugg) => Some((span, sugg)),
+                        _ => None,
+                    },
+                ) {
+                    match self.tcx.sess.source_map().span_to_snippet(span) {
+                        Ok(snippet) if unnamed_borrow(&snippet) && sugg.starts_with("&") => {
+                            let span = span_unnamed_borrow(span);
                             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);
+                        Ok(snippet) if snippet.starts_with("&'_ ") && sugg.starts_with("&") => {
+                            let span = span_underscore_borrow(span);
                             introduce_suggestion.push((span, sugg[1..].to_string()));
                         }
-                        (Some(sugg), _) => {
+                        _ => {
                             introduce_suggestion.push((span, sugg.to_string()));
                         }
-                        _ => {}
                     }
                 }
                 err.multipart_suggestion_with_style(