Return bool from is_unsafe_method_call and cleanup usages

This commit is contained in:
Paul Daniel Faria 2020-07-23 18:31:28 -04:00
parent a6af0272f7
commit 39fdd41df4
2 changed files with 25 additions and 24 deletions

View file

@ -281,26 +281,26 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
self.imp.assert_contains_node(node)
}
pub fn is_unsafe_method_call(&self, 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 = self.type_of_expr(&field_expr.expr()?)?;
if !ty.is_packed(self.db) {
return None;
}
pub fn is_unsafe_method_call(&self, method_call_expr: ast::MethodCallExpr) -> bool {
method_call_expr
.expr()
.and_then(|expr| {
let field_expr = if let ast::Expr::FieldExpr(field_expr) = expr {
field_expr
} else {
return None;
};
let ty = self.type_of_expr(&field_expr.expr()?)?;
if !ty.is_packed(self.db) {
return None;
}
let func = self.resolve_method_call(&method_call_expr)?;
if func.has_self_param(self.db) {
let params = func.params(self.db);
if matches!(params.into_iter().next(), Some(TypeRef::Reference(..))) {
Some(())
} else {
None
}
} else {
None
}
let func = self.resolve_method_call(&method_call_expr)?;
let is_unsafe = func.has_self_param(self.db)
&& matches!(func.params(self.db).first(), Some(TypeRef::Reference(..)));
Some(is_unsafe)
})
.unwrap_or(false)
}
pub fn is_unsafe_ref_expr(&self, ref_expr: &ast::RefExpr) -> bool {

View file

@ -730,8 +730,9 @@ fn highlight_name(
let is_unsafe = name_ref
.and_then(|name_ref| name_ref.syntax().parent())
.and_then(ast::MethodCallExpr::cast)
.and_then(|method_call_expr| sema.is_unsafe_method_call(method_call_expr));
if is_unsafe.is_some() {
.map(|method_call_expr| sema.is_unsafe_method_call(method_call_expr))
.unwrap_or(false);
if is_unsafe {
h |= HighlightModifier::Unsafe;
}
}
@ -809,9 +810,9 @@ fn highlight_name_ref_by_syntax(name: ast::NameRef, sema: &Semantics<RootDatabas
METHOD_CALL_EXPR => {
let mut h = Highlight::new(HighlightTag::Function);
let is_unsafe = ast::MethodCallExpr::cast(parent)
.and_then(|method_call_expr| sema.is_unsafe_method_call(method_call_expr));
if is_unsafe.is_some() {
.map(|method_call_expr| sema.is_unsafe_method_call(method_call_expr))
.unwrap_or(false);
if is_unsafe {
h |= HighlightModifier::Unsafe;
}