]> git.lizzy.rs Git - rust.git/commitdiff
in which rightward drift is opposed
authorZack M. Davis <code@zackmdavis.net>
Mon, 8 Oct 2018 02:28:37 +0000 (19:28 -0700)
committerZack M. Davis <code@zackmdavis.net>
Mon, 8 Oct 2018 02:46:19 +0000 (19:46 -0700)
Thanks to reviewers Tyler Mandry (for pointing out that this is
ridiculous and we need a helper function), Niko Matsakis (for pointing
out that the span-calculation code only has a couple free variables),
and Esteban Küber (for pointing out `get_generics`).

src/librustc/middle/resolve_lifetime.rs

index 53bfee4ae24bb9d05a7ab9fe51440196e602ec19..2f3fdb7966f25adde4a27f6ccd3ef2f29173211c 100644 (file)
@@ -1398,6 +1398,30 @@ fn with<F>(&mut self, wrap_scope: Scope<'_>, f: F)
         self.xcrate_object_lifetime_defaults = this.xcrate_object_lifetime_defaults;
     }
 
+    /// helper method to determine the span to remove when suggesting the
+    /// deletion of a lifetime
+    fn lifetime_deletion_span(&self, name: ast::Ident, generics: &hir::Generics) -> Option<Span> {
+        if generics.params.len() == 1 {
+            // if sole lifetime, remove the `<>` brackets
+            Some(generics.span)
+        } else {
+            generics.params.iter().enumerate()
+                .find_map(|(i, param)| {
+                    if param.name.ident() == name {
+                        // We also want to delete a leading or trailing comma
+                        // as appropriate
+                        if i >= generics.params.len() - 1 {
+                            Some(generics.params[i-1].span.shrink_to_hi().to(param.span))
+                        } else {
+                            Some(param.span.to(generics.params[i+1].span.shrink_to_lo()))
+                        }
+                    } else {
+                        None
+                    }
+                })
+        }
+    }
+
     fn check_uses_for_lifetimes_defined_by_scope(&mut self) {
         let defined_by = match self.scope {
             Scope::Binder { lifetimes, .. } => lifetimes,
@@ -1475,50 +1499,15 @@ fn check_uses_for_lifetimes_defined_by_scope(&mut self) {
                             &format!("lifetime parameter `{}` never used", name)
                         );
                         if let Some(parent_def_id) = self.tcx.parent(def_id) {
-                            if let Some(node_id) = self.tcx.hir.as_local_node_id(parent_def_id) {
-                                if let Some(Node::Item(hir_item)) = self.tcx.hir.find(node_id) {
-                                    match hir_item.node {
-                                        hir::ItemKind::Fn(_, _, ref generics, _) |
-                                        hir::ItemKind::Impl(_, _, _, ref generics, _, _, _) => {
-                                            let unused_lt_span = if generics.params.len() == 1 {
-                                                // if sole lifetime, remove the `<>` brackets
-                                                Some(generics.span)
-                                            } else {
-                                                generics.params.iter().enumerate()
-                                                    .find_map(|(i, param)| {
-                                                        if param.name.ident() == name {
-                                                            // We also want to delete a leading or
-                                                            // trailing comma as appropriate
-                                                            if i >= generics.params.len() - 1 {
-                                                                Some(
-                                                                    generics.params[i-1]
-                                                                        .span.shrink_to_hi()
-                                                                        .to(param.span)
-                                                                )
-                                                            } else {
-                                                                Some(
-                                                                    param.span.to(
-                                                                        generics.params[i+1]
-                                                                            .span.shrink_to_lo()
-                                                                    )
-                                                                )
-                                                            }
-                                                        } else {
-                                                            None
-                                                        }
-                                                    })
-                                            };
-                                            if let Some(span) = unused_lt_span {
-                                                err.span_suggestion_with_applicability(
-                                                    span,
-                                                    "remove it",
-                                                    String::new(),
-                                                    Applicability::MachineApplicable
-                                                );
-                                            }
-                                        },
-                                        _ => {}
-                                    }
+                            if let Some(generics) = self.tcx.hir.get_generics(parent_def_id) {
+                                let unused_lt_span = self.lifetime_deletion_span(name, generics);
+                                if let Some(span) = unused_lt_span {
+                                    err.span_suggestion_with_applicability(
+                                        span,
+                                        "remove it",
+                                        String::new(),
+                                        Applicability::MachineApplicable
+                                    );
                                 }
                             }
                         }