Improve perf of finding unsafe exprs
This commit is contained in:
parent
b1992b469c
commit
b7e25ba854
@ -60,12 +60,6 @@ pub struct UnsafeExpr {
|
|||||||
pub inside_unsafe_block: bool,
|
pub inside_unsafe_block: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl UnsafeExpr {
|
|
||||||
fn new(expr: ExprId) -> Self {
|
|
||||||
Self { expr, inside_unsafe_block: false }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn unsafe_expressions(
|
pub fn unsafe_expressions(
|
||||||
db: &dyn HirDatabase,
|
db: &dyn HirDatabase,
|
||||||
infer: &InferenceResult,
|
infer: &InferenceResult,
|
||||||
@ -73,59 +67,55 @@ pub fn unsafe_expressions(
|
|||||||
) -> Vec<UnsafeExpr> {
|
) -> Vec<UnsafeExpr> {
|
||||||
let mut unsafe_exprs = vec![];
|
let mut unsafe_exprs = vec![];
|
||||||
let body = db.body(def);
|
let body = db.body(def);
|
||||||
for (id, expr) in body.exprs.iter() {
|
walk_unsafe(&mut unsafe_exprs, db, infer, &body, body.body_expr, false);
|
||||||
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));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
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::UnaryOp { expr, op: UnaryOp::Deref } => {
|
|
||||||
if let Ty::Apply(ApplicationTy { ctor: TypeCtor::RawPtr(..), .. }) = &infer[*expr] {
|
|
||||||
unsafe_exprs.push(UnsafeExpr::new(id));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
unsafe_exprs
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_in_unsafe(body: &Body, current: ExprId, needle: ExprId, within_unsafe: bool) -> bool {
|
fn walk_unsafe(
|
||||||
if current == needle {
|
unsafe_exprs: &mut Vec<UnsafeExpr>,
|
||||||
return within_unsafe;
|
db: &dyn HirDatabase,
|
||||||
}
|
infer: &InferenceResult,
|
||||||
|
body: &Body,
|
||||||
|
current: ExprId,
|
||||||
|
inside_unsafe_block: bool,
|
||||||
|
) {
|
||||||
let expr = &body.exprs[current];
|
let expr = &body.exprs[current];
|
||||||
if let &Expr::Unsafe { body: child } = expr {
|
match expr {
|
||||||
return is_in_unsafe(body, child, needle, true);
|
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(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 { expr: current, inside_unsafe_block });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
|
||||||
|
if let &Expr::Unsafe { body: child } = expr {
|
||||||
|
return walk_unsafe(unsafe_exprs, db, infer, body, child, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut found = false;
|
|
||||||
expr.walk_child_exprs(|child| {
|
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
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user