mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-28 04:45:05 +00:00
Deduplicate unsafe method call into a single function
This commit is contained in:
parent
d5f11e530d
commit
aca3d6c57e
1 changed files with 31 additions and 41 deletions
|
@ -743,6 +743,26 @@ fn is_child_of_impl(element: &SyntaxElement) -> bool {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn is_method_call_unsafe(
|
||||||
|
sema: &Semantics<RootDatabase>,
|
||||||
|
method_call_expr: ast::MethodCallExpr,
|
||||||
|
) -> Option<()> {
|
||||||
|
let expr = method_call_expr.expr()?;
|
||||||
|
let field_expr =
|
||||||
|
if let ast::Expr::FieldExpr(field_expr) = expr { field_expr } else { return None };
|
||||||
|
let ty = sema.type_of_expr(&field_expr.expr()?)?;
|
||||||
|
if !ty.is_packed(sema.db) {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
let func = sema.resolve_method_call(&method_call_expr)?;
|
||||||
|
if func.self_param(sema.db)?.is_ref {
|
||||||
|
Some(())
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn highlight_name(
|
fn highlight_name(
|
||||||
sema: &Semantics<RootDatabase>,
|
sema: &Semantics<RootDatabase>,
|
||||||
db: &RootDatabase,
|
db: &RootDatabase,
|
||||||
|
@ -769,28 +789,13 @@ fn highlight_name(
|
||||||
if func.is_unsafe(db) {
|
if func.is_unsafe(db) {
|
||||||
h |= HighlightModifier::Unsafe;
|
h |= HighlightModifier::Unsafe;
|
||||||
} else {
|
} else {
|
||||||
(|| {
|
let is_unsafe = name_ref
|
||||||
let method_call_expr =
|
.and_then(|name_ref| name_ref.syntax().parent())
|
||||||
name_ref?.syntax().parent().and_then(ast::MethodCallExpr::cast)?;
|
.and_then(ast::MethodCallExpr::cast)
|
||||||
let expr = method_call_expr.expr()?;
|
.and_then(|method_call_expr| is_method_call_unsafe(sema, method_call_expr));
|
||||||
let field_expr = if let ast::Expr::FieldExpr(field_expr) = expr {
|
if is_unsafe.is_some() {
|
||||||
Some(field_expr)
|
h |= HighlightModifier::Unsafe;
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}?;
|
|
||||||
let ty = sema.type_of_expr(&field_expr.expr()?)?;
|
|
||||||
if !ty.is_packed(db) {
|
|
||||||
return None;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let func = sema.resolve_method_call(&method_call_expr)?;
|
|
||||||
if func.self_param(db)?.is_ref {
|
|
||||||
Some(HighlightModifier::Unsafe)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
})()
|
|
||||||
.map(|modifier| h |= modifier);
|
|
||||||
}
|
}
|
||||||
return h;
|
return h;
|
||||||
}
|
}
|
||||||
|
@ -865,26 +870,11 @@ fn highlight_name_ref_by_syntax(name: ast::NameRef, sema: &Semantics<RootDatabas
|
||||||
match parent.kind() {
|
match parent.kind() {
|
||||||
METHOD_CALL_EXPR => {
|
METHOD_CALL_EXPR => {
|
||||||
let mut h = Highlight::new(HighlightTag::Function);
|
let mut h = Highlight::new(HighlightTag::Function);
|
||||||
let modifier: Option<HighlightModifier> = (|| {
|
let is_unsafe = ast::MethodCallExpr::cast(parent)
|
||||||
let method_call_expr = ast::MethodCallExpr::cast(parent)?;
|
.and_then(|method_call_expr| is_method_call_unsafe(sema, method_call_expr));
|
||||||
let expr = method_call_expr.expr()?;
|
|
||||||
let field_expr = if let ast::Expr::FieldExpr(field_expr) = expr {
|
|
||||||
field_expr
|
|
||||||
} else {
|
|
||||||
return None;
|
|
||||||
};
|
|
||||||
|
|
||||||
let expr = field_expr.expr()?;
|
if is_unsafe.is_some() {
|
||||||
let ty = sema.type_of_expr(&expr)?;
|
h |= HighlightModifier::Unsafe;
|
||||||
if ty.is_packed(sema.db) {
|
|
||||||
Some(HighlightModifier::Unsafe)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
})();
|
|
||||||
|
|
||||||
if let Some(modifier) = modifier {
|
|
||||||
h |= modifier;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
h
|
h
|
||||||
|
|
Loading…
Reference in a new issue