From b7e25ba854a5ca0f1dee7082c113170876358632 Mon Sep 17 00:00:00 2001 From: Paul Daniel Faria Date: Sat, 27 Jun 2020 11:55:54 -0400 Subject: Improve perf of finding unsafe exprs --- crates/ra_hir_ty/src/unsafe_validation.rs | 88 ++++++++++++++----------------- 1 file changed, 39 insertions(+), 49 deletions(-) (limited to 'crates') diff --git a/crates/ra_hir_ty/src/unsafe_validation.rs b/crates/ra_hir_ty/src/unsafe_validation.rs index e2353404b..aad13d99c 100644 --- a/crates/ra_hir_ty/src/unsafe_validation.rs +++ b/crates/ra_hir_ty/src/unsafe_validation.rs @@ -60,12 +60,6 @@ pub struct UnsafeExpr { pub inside_unsafe_block: bool, } -impl UnsafeExpr { - fn new(expr: ExprId) -> Self { - Self { expr, inside_unsafe_block: false } - } -} - pub fn unsafe_expressions( db: &dyn HirDatabase, infer: &InferenceResult, @@ -73,59 +67,55 @@ pub fn unsafe_expressions( ) -> Vec { let mut unsafe_exprs = vec![]; let body = db.body(def); - for (id, expr) in body.exprs.iter() { - match expr { - Expr::Call { callee, .. } => { - let ty = &infer[*callee]; - if let &Ty::Apply(ApplicationTy { - ctor: TypeCtor::FnDef(CallableDef::FunctionId(func)), - .. - }) = ty - { - if db.function_data(func).is_unsafe { - unsafe_exprs.push(UnsafeExpr::new(id)); - } + walk_unsafe(&mut unsafe_exprs, db, infer, &body, body.body_expr, false); + + unsafe_exprs +} + +fn walk_unsafe( + unsafe_exprs: &mut Vec, + db: &dyn HirDatabase, + infer: &InferenceResult, + body: &Body, + current: ExprId, + inside_unsafe_block: bool, +) { + let expr = &body.exprs[current]; + match expr { + Expr::Call { callee, .. } => { + let ty = &infer[*callee]; + if let &Ty::Apply(ApplicationTy { + ctor: TypeCtor::FnDef(CallableDef::FunctionId(func)), + .. + }) = ty + { + if db.function_data(func).is_unsafe { + unsafe_exprs.push(UnsafeExpr { expr: current, inside_unsafe_block }); } } - Expr::MethodCall { .. } => { - if infer - .method_resolution(id) - .map(|func| db.function_data(func).is_unsafe) - .unwrap_or(false) - { - unsafe_exprs.push(UnsafeExpr::new(id)); - } + } + Expr::MethodCall { .. } => { + if infer + .method_resolution(current) + .map(|func| db.function_data(func).is_unsafe) + .unwrap_or(false) + { + unsafe_exprs.push(UnsafeExpr { expr: current, inside_unsafe_block }); } - Expr::UnaryOp { expr, op: UnaryOp::Deref } => { - if let Ty::Apply(ApplicationTy { ctor: TypeCtor::RawPtr(..), .. }) = &infer[*expr] { - unsafe_exprs.push(UnsafeExpr::new(id)); - } + } + Expr::UnaryOp { expr, op: UnaryOp::Deref } => { + if let Ty::Apply(ApplicationTy { ctor: TypeCtor::RawPtr(..), .. }) = &infer[*expr] { + unsafe_exprs.push(UnsafeExpr { expr: current, inside_unsafe_block }); } - _ => {} } + _ => {} } - for unsafe_expr in &mut unsafe_exprs { - unsafe_expr.inside_unsafe_block = - is_in_unsafe(&body, body.body_expr, unsafe_expr.expr, false); - } - - unsafe_exprs -} - -fn is_in_unsafe(body: &Body, current: ExprId, needle: ExprId, within_unsafe: bool) -> bool { - if current == needle { - return within_unsafe; - } - - let expr = &body.exprs[current]; if let &Expr::Unsafe { body: child } = expr { - return is_in_unsafe(body, child, needle, true); + return walk_unsafe(unsafe_exprs, db, infer, body, child, true); } - let mut found = false; expr.walk_child_exprs(|child| { - found = found || is_in_unsafe(body, child, needle, within_unsafe); + walk_unsafe(unsafe_exprs, db, infer, body, child, inside_unsafe_block); }); - found } -- cgit v1.2.3