- /// Given a `NodeId`, return the `FnDecl` of the method it is enclosed by and whether a
- /// suggestion can be made, `None` otherwise.
- pub fn get_fn_decl(&self, blk_id: ast::NodeId) -> Option<(hir::FnDecl, bool)> {
- // Get enclosing Fn, if it is a function or a trait method, unless there's a `loop` or
- // `while` before reaching it, as block tail returns are not available in them.
- if let Some(fn_id) = self.tcx.hir().get_return_block(blk_id) {
- let parent = self.tcx.hir().get(fn_id);
+ fn parent_item_span(&self, id: ast::NodeId) -> Option<Span> {
+ let node = self.tcx.hir().get(self.tcx.hir().get_parent(id));
+ match node {
+ Node::Item(&hir::Item {
+ node: hir::ItemKind::Fn(_, _, _, body_id), ..
+ }) |
+ Node::ImplItem(&hir::ImplItem {
+ node: hir::ImplItemKind::Method(_, body_id), ..
+ }) => {
+ let body = self.tcx.hir().body(body_id);
+ if let ExprKind::Block(block, _) = &body.value.node {
+ return Some(block.span);
+ }
+ }
+ _ => {}
+ }
+ None
+ }
+
+ /// Given a function block's `NodeId`, return its `FnDecl` if it exists, or `None` otherwise.
+ fn get_parent_fn_decl(&self, blk_id: ast::NodeId) -> Option<(hir::FnDecl, ast::Ident)> {
+ let parent = self.tcx.hir().get(self.tcx.hir().get_parent(blk_id));
+ self.get_node_fn_decl(parent).map(|(fn_decl, ident, _)| (fn_decl, ident))
+ }