about summary refs log tree commit diff
diff options
context:
space:
mode:
authorTaylor Yu <tlyu@mit.edu>2021-06-01 20:15:12 -0500
committerTaylor Yu <tlyu@mit.edu>2021-06-18 17:24:35 -0500
commit69f0dc69a45889f58c5e13fabdd8c8eabfd604a1 (patch)
tree1e3d8570652333e80fb66d83e909dc9a2fbbf81a
parent437b2026e1bfcb1f28d838f3a2a24b23f0401b53 (diff)
downloadrust-69f0dc69a45889f58c5e13fabdd8c8eabfd604a1.tar.gz
rust-69f0dc69a45889f58c5e13fabdd8c8eabfd604a1.zip
deindent unsized suggestions
Move stuff out of loops. Use early returns.
-rw-r--r--compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs126
1 files changed, 63 insertions, 63 deletions
diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs
index 4d0e8c50229..1ebdb8ac26c 100644
--- a/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs
+++ b/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs
@@ -1803,51 +1803,51 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> {
             Some(generics) => generics,
             None => return,
         };
-        debug!("suggest_unsized_bound_if_applicable: generics.params={:?}", generics.params);
-        debug!(
-            "suggest_unsized_bound_if_applicable: generics.where_clause={:?}",
-            generics.where_clause
-        );
-        for param in generics.params {
-            if param.span != span
-                || param.bounds.iter().any(|bound| {
-                    bound.trait_ref().and_then(|trait_ref| trait_ref.trait_def_id())
-                        == self.tcx.lang_items().sized_trait()
-                })
-            {
-                continue;
-            }
-            debug!("maybe_suggest_unsized_generics: param={:?}", param);
-            match node {
-                hir::Node::Item(
-                    item
-                    @
-                    hir::Item {
-                        kind:
-                            hir::ItemKind::Enum(..)
-                            | hir::ItemKind::Struct(..)
-                            | hir::ItemKind::Union(..),
-                        ..
-                    },
-                ) => {
-                    if self.maybe_indirection_for_unsized(err, item, param) {
-                        return;
-                    }
+        let sized_trait = self.tcx.lang_items().sized_trait();
+        debug!("maybe_suggest_unsized_generics: generics.params={:?}", generics.params);
+        debug!("maybe_suggest_unsized_generics: generics.where_clause={:?}", generics.where_clause);
+        let param = generics
+            .params
+            .iter()
+            .filter(|param| param.span == span)
+            .filter(|param| {
+                param
+                    .bounds
+                    .iter()
+                    .all(|bound| bound.trait_ref().and_then(|tr| tr.trait_def_id()) != sized_trait)
+            })
+            .next();
+        let param = match param {
+            Some(param) => param,
+            _ => return,
+        };
+        debug!("maybe_suggest_unsized_generics: param={:?}", param);
+        match node {
+            hir::Node::Item(
+                item
+                @
+                hir::Item {
+                    kind:
+                        hir::ItemKind::Enum(..) | hir::ItemKind::Struct(..) | hir::ItemKind::Union(..),
+                    ..
+                },
+            ) => {
+                if self.maybe_indirection_for_unsized(err, item, param) {
+                    return;
                 }
-                _ => {}
             }
-            let (span, separator) = match param.bounds {
-                [] => (span.shrink_to_hi(), ":"),
-                [.., bound] => (bound.span().shrink_to_hi(), " +"),
-            };
-            err.span_suggestion_verbose(
-                span,
-                "consider relaxing the implicit `Sized` restriction",
-                format!("{} ?Sized", separator),
-                Applicability::MachineApplicable,
-            );
-            return;
-        }
+            _ => {}
+        };
+        let (span, separator) = match param.bounds {
+            [] => (span.shrink_to_hi(), ":"),
+            [.., bound] => (bound.span().shrink_to_hi(), " +"),
+        };
+        err.span_suggestion_verbose(
+            span,
+            "consider relaxing the implicit `Sized` restriction",
+            format!("{} ?Sized", separator),
+            Applicability::MachineApplicable,
+        );
     }
 
     fn maybe_indirection_for_unsized(
@@ -1862,29 +1862,29 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> {
         let mut visitor =
             FindTypeParam { param: param.name.ident().name, invalid_spans: vec![], nested: false };
         visitor.visit_item(item);
-        if !visitor.invalid_spans.is_empty() {
-            let mut multispan: MultiSpan = param.span.into();
+        if visitor.invalid_spans.is_empty() {
+            return false;
+        }
+        let mut multispan: MultiSpan = param.span.into();
+        multispan.push_span_label(
+            param.span,
+            format!("this could be changed to `{}: ?Sized`...", param.name.ident()),
+        );
+        for sp in visitor.invalid_spans {
             multispan.push_span_label(
-                param.span,
-                format!("this could be changed to `{}: ?Sized`...", param.name.ident()),
+                sp,
+                format!("...if indirection were used here: `Box<{}>`", param.name.ident()),
             );
-            for sp in visitor.invalid_spans {
-                multispan.push_span_label(
-                    sp,
-                    format!("...if indirection were used here: `Box<{}>`", param.name.ident()),
-                );
-            }
-            err.span_help(
-                multispan,
-                &format!(
-                    "you could relax the implicit `Sized` bound on `{T}` if it were \
-                    used through indirection like `&{T}` or `Box<{T}>`",
-                    T = param.name.ident(),
-                ),
-            );
-            return true;
         }
-        false
+        err.span_help(
+            multispan,
+            &format!(
+                "you could relax the implicit `Sized` bound on `{T}` if it were \
+                used through indirection like `&{T}` or `Box<{T}>`",
+                T = param.name.ident(),
+            ),
+        );
+        true
     }
 
     fn is_recursive_obligation(