From 200470692ff1023024f0d4a6c35deb43d3045f10 Mon Sep 17 00:00:00 2001 From: Phil Ellison Date: Mon, 12 Aug 2019 20:39:11 +0100 Subject: Cast SyntaxNodePtr to AstPtr directly --- crates/ra_hir/src/expr/validation.rs | 17 +++-------------- crates/ra_syntax/src/ptr.rs | 7 +++++++ 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/crates/ra_hir/src/expr/validation.rs b/crates/ra_hir/src/expr/validation.rs index fce1c2f14..e35990d2e 100644 --- a/crates/ra_hir/src/expr/validation.rs +++ b/crates/ra_hir/src/expr/validation.rs @@ -145,21 +145,10 @@ impl<'a, 'b> ExprValidator<'a, 'b> { if params.len() == 2 && ¶ms[0] == &mismatch.actual { let source_map = self.func.body_source_map(db); let file_id = self.func.source(db).file_id; - let parse = db.parse(file_id.original_file(db)); - let source_file = parse.tree(); - let expr_syntax = source_map.expr_syntax(id); - if expr_syntax.is_none() { - return; - } - let expr_syntax = expr_syntax.unwrap(); - let node = expr_syntax.to_node(source_file.syntax()); - let ast = ast::Expr::cast(node); - if ast.is_none() { - return; - } - let ast = ast.unwrap(); - self.sink.push(MissingOkInTailExpr { file: file_id, expr: AstPtr::new(&ast) }); + if let Some(expr) = source_map.expr_syntax(id).and_then(|n| n.cast::()) { + self.sink.push(MissingOkInTailExpr { file: file_id, expr }); + } } } } diff --git a/crates/ra_syntax/src/ptr.rs b/crates/ra_syntax/src/ptr.rs index d24660ac3..992034ef0 100644 --- a/crates/ra_syntax/src/ptr.rs +++ b/crates/ra_syntax/src/ptr.rs @@ -31,6 +31,13 @@ impl SyntaxNodePtr { pub fn kind(self) -> SyntaxKind { self.kind } + + pub fn cast(self) -> Option> { + if !N::can_cast(self.kind()) { + return None; + } + Some(AstPtr { raw: self, _ty: PhantomData }) + } } /// Like `SyntaxNodePtr`, but remembers the type of node -- cgit v1.2.3