From 39fdd41df4052cef5da4876067ae28615012476b Mon Sep 17 00:00:00 2001 From: Paul Daniel Faria Date: Thu, 23 Jul 2020 18:31:28 -0400 Subject: [PATCH] Return bool from is_unsafe_method_call and cleanup usages --- crates/ra_hir/src/semantics.rs | 38 ++++++++++++------------ crates/ra_ide/src/syntax_highlighting.rs | 11 +++---- 2 files changed, 25 insertions(+), 24 deletions(-) diff --git a/crates/ra_hir/src/semantics.rs b/crates/ra_hir/src/semantics.rs index f706a186e7..9697c7082b 100644 --- a/crates/ra_hir/src/semantics.rs +++ b/crates/ra_hir/src/semantics.rs @@ -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 { diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs index e29f65a784..4527885e93 100644 --- a/crates/ra_ide/src/syntax_highlighting.rs +++ b/crates/ra_ide/src/syntax_highlighting.rs @@ -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 { 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; }