aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorPaul Daniel Faria <[email protected]>2020-06-27 22:28:07 +0100
committerPaul Daniel Faria <[email protected]>2020-08-10 13:44:54 +0100
commitaca3d6c57ec2c668cdb51eca34d6f7bc8fa7412b (patch)
treeaece5efecb857d71c0cd9af2b78db8b8e314e905 /crates
parentd5f11e530dbf6edbdd0ca32d6cd5fafe634c8c4a (diff)
Deduplicate unsafe method call into a single function
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_ide/src/syntax_highlighting.rs72
1 files changed, 31 insertions, 41 deletions
diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs
index 454fef39c..02b16b13c 100644
--- a/crates/ra_ide/src/syntax_highlighting.rs
+++ b/crates/ra_ide/src/syntax_highlighting.rs
@@ -743,6 +743,26 @@ fn is_child_of_impl(element: &SyntaxElement) -> bool {
743 } 743 }
744} 744}
745 745
746fn is_method_call_unsafe(
747 sema: &Semantics<RootDatabase>,
748 method_call_expr: ast::MethodCallExpr,
749) -> Option<()> {
750 let expr = method_call_expr.expr()?;
751 let field_expr =
752 if let ast::Expr::FieldExpr(field_expr) = expr { field_expr } else { return None };
753 let ty = sema.type_of_expr(&field_expr.expr()?)?;
754 if !ty.is_packed(sema.db) {
755 return None;
756 }
757
758 let func = sema.resolve_method_call(&method_call_expr)?;
759 if func.self_param(sema.db)?.is_ref {
760 Some(())
761 } else {
762 None
763 }
764}
765
746fn highlight_name( 766fn highlight_name(
747 sema: &Semantics<RootDatabase>, 767 sema: &Semantics<RootDatabase>,
748 db: &RootDatabase, 768 db: &RootDatabase,
@@ -769,28 +789,13 @@ fn highlight_name(
769 if func.is_unsafe(db) { 789 if func.is_unsafe(db) {
770 h |= HighlightModifier::Unsafe; 790 h |= HighlightModifier::Unsafe;
771 } else { 791 } else {
772 (|| { 792 let is_unsafe = name_ref
773 let method_call_expr = 793 .and_then(|name_ref| name_ref.syntax().parent())
774 name_ref?.syntax().parent().and_then(ast::MethodCallExpr::cast)?; 794 .and_then(ast::MethodCallExpr::cast)
775 let expr = method_call_expr.expr()?; 795 .and_then(|method_call_expr| is_method_call_unsafe(sema, method_call_expr));
776 let field_expr = if let ast::Expr::FieldExpr(field_expr) = expr { 796 if is_unsafe.is_some() {
777 Some(field_expr) 797 h |= HighlightModifier::Unsafe;
778 } else { 798 }
779 None
780 }?;
781 let ty = sema.type_of_expr(&field_expr.expr()?)?;
782 if !ty.is_packed(db) {
783 return None;
784 }
785
786 let func = sema.resolve_method_call(&method_call_expr)?;
787 if func.self_param(db)?.is_ref {
788 Some(HighlightModifier::Unsafe)
789 } else {
790 None
791 }
792 })()
793 .map(|modifier| h |= modifier);
794 } 799 }
795 return h; 800 return h;
796 } 801 }
@@ -865,26 +870,11 @@ fn highlight_name_ref_by_syntax(name: ast::NameRef, sema: &Semantics<RootDatabas
865 match parent.kind() { 870 match parent.kind() {
866 METHOD_CALL_EXPR => { 871 METHOD_CALL_EXPR => {
867 let mut h = Highlight::new(HighlightTag::Function); 872 let mut h = Highlight::new(HighlightTag::Function);
868 let modifier: Option<HighlightModifier> = (|| { 873 let is_unsafe = ast::MethodCallExpr::cast(parent)
869 let method_call_expr = ast::MethodCallExpr::cast(parent)?; 874 .and_then(|method_call_expr| is_method_call_unsafe(sema, method_call_expr));
870 let expr = method_call_expr.expr()?;
871 let field_expr = if let ast::Expr::FieldExpr(field_expr) = expr {
872 field_expr
873 } else {
874 return None;
875 };
876
877 let expr = field_expr.expr()?;
878 let ty = sema.type_of_expr(&expr)?;
879 if ty.is_packed(sema.db) {
880 Some(HighlightModifier::Unsafe)
881 } else {
882 None
883 }
884 })();
885 875
886 if let Some(modifier) = modifier { 876 if is_unsafe.is_some() {
887 h |= modifier; 877 h |= HighlightModifier::Unsafe;
888 } 878 }
889 879
890 h 880 h