diff options
author | Paul Daniel Faria <[email protected]> | 2020-07-30 16:07:13 +0100 |
---|---|---|
committer | Paul Daniel Faria <[email protected]> | 2020-08-16 15:22:51 +0100 |
commit | a044ff0138d6bff9406b94de89fde43e7672ee1b (patch) | |
tree | c90b7a41f15d86b6bf7b6bd4fd4c9c8496197ae3 /crates/ide/src | |
parent | 7009d5ee2bff53b9beb555b1572c97ab3882cd98 (diff) |
Mark mutating functions with `mutable` modifier, and owning functions with `consuming`.
Diffstat (limited to 'crates/ide/src')
-rw-r--r-- | crates/ide/src/syntax_highlighting.rs | 42 | ||||
-rw-r--r-- | crates/ide/src/syntax_highlighting/tests.rs | 9 |
2 files changed, 41 insertions, 10 deletions
diff --git a/crates/ide/src/syntax_highlighting.rs b/crates/ide/src/syntax_highlighting.rs index 5d7c7e8d0..15a78a614 100644 --- a/crates/ide/src/syntax_highlighting.rs +++ b/crates/ide/src/syntax_highlighting.rs | |||
@@ -4,7 +4,7 @@ mod injection; | |||
4 | #[cfg(test)] | 4 | #[cfg(test)] |
5 | mod tests; | 5 | mod tests; |
6 | 6 | ||
7 | use hir::{Name, Semantics, VariantDef}; | 7 | use hir::{Mutability, Name, Semantics, VariantDef}; |
8 | use ide_db::{ | 8 | use ide_db::{ |
9 | defs::{classify_name, classify_name_ref, Definition, NameClass, NameRefClass}, | 9 | defs::{classify_name, classify_name_ref, Definition, NameClass, NameRefClass}, |
10 | RootDatabase, | 10 | RootDatabase, |
@@ -729,13 +729,23 @@ fn highlight_name( | |||
729 | let is_unsafe = name_ref | 729 | let is_unsafe = name_ref |
730 | .and_then(|name_ref| name_ref.syntax().parent()) | 730 | .and_then(|name_ref| name_ref.syntax().parent()) |
731 | .and_then(ast::MethodCallExpr::cast) | 731 | .and_then(ast::MethodCallExpr::cast) |
732 | .map(|method_call_expr| sema.is_unsafe_method_call(method_call_expr)) | 732 | .map(|method_call_expr| sema.is_unsafe_method_call(&method_call_expr)) |
733 | .unwrap_or(false); | 733 | .unwrap_or(false); |
734 | if is_unsafe { | 734 | if is_unsafe { |
735 | h |= HighlightModifier::Unsafe; | 735 | h |= HighlightModifier::Unsafe; |
736 | } | 736 | } |
737 | } | 737 | } |
738 | return h; | 738 | return if func.has_self_param(db) { |
739 | match func.mutability_of_self_param(db) { | ||
740 | Some(mutability) => match mutability { | ||
741 | Mutability::Mut => h | HighlightModifier::Mutable, | ||
742 | Mutability::Shared => h, | ||
743 | }, | ||
744 | None => h | HighlightModifier::Consuming, | ||
745 | } | ||
746 | } else { | ||
747 | h | ||
748 | }; | ||
739 | } | 749 | } |
740 | hir::ModuleDef::Adt(hir::Adt::Struct(_)) => HighlightTag::Struct, | 750 | hir::ModuleDef::Adt(hir::Adt::Struct(_)) => HighlightTag::Struct, |
741 | hir::ModuleDef::Adt(hir::Adt::Enum(_)) => HighlightTag::Enum, | 751 | hir::ModuleDef::Adt(hir::Adt::Enum(_)) => HighlightTag::Enum, |
@@ -808,14 +818,26 @@ fn highlight_name_ref_by_syntax(name: ast::NameRef, sema: &Semantics<RootDatabas | |||
808 | match parent.kind() { | 818 | match parent.kind() { |
809 | METHOD_CALL_EXPR => { | 819 | METHOD_CALL_EXPR => { |
810 | let mut h = Highlight::new(HighlightTag::Function); | 820 | let mut h = Highlight::new(HighlightTag::Function); |
811 | let is_unsafe = ast::MethodCallExpr::cast(parent) | 821 | ast::MethodCallExpr::cast(parent) |
812 | .map(|method_call_expr| sema.is_unsafe_method_call(method_call_expr)) | 822 | .and_then(|method_call_expr| { |
813 | .unwrap_or(false); | 823 | if sema.is_unsafe_method_call(&method_call_expr) { |
814 | if is_unsafe { | 824 | h |= HighlightModifier::Unsafe; |
815 | h |= HighlightModifier::Unsafe; | 825 | } |
816 | } | 826 | |
827 | let func = sema.resolve_method_call(&method_call_expr)?; | ||
828 | if !func.has_self_param(sema.db) { | ||
829 | return Some(h); | ||
830 | } | ||
817 | 831 | ||
818 | h | 832 | Some(match func.mutability_of_self_param(sema.db) { |
833 | Some(mutability) => match mutability { | ||
834 | Mutability::Mut => h | HighlightModifier::Mutable, | ||
835 | Mutability::Shared => h, | ||
836 | }, | ||
837 | None => h | HighlightModifier::Consuming, | ||
838 | }) | ||
839 | }) | ||
840 | .unwrap_or_else(|| h) | ||
819 | } | 841 | } |
820 | FIELD_EXPR => { | 842 | FIELD_EXPR => { |
821 | let h = HighlightTag::Field; | 843 | let h = HighlightTag::Field; |
diff --git a/crates/ide/src/syntax_highlighting/tests.rs b/crates/ide/src/syntax_highlighting/tests.rs index 94f37d773..6cb955d29 100644 --- a/crates/ide/src/syntax_highlighting/tests.rs +++ b/crates/ide/src/syntax_highlighting/tests.rs | |||
@@ -36,6 +36,10 @@ impl Foo { | |||
36 | fn qux(&mut self) { | 36 | fn qux(&mut self) { |
37 | self.x = 0; | 37 | self.x = 0; |
38 | } | 38 | } |
39 | |||
40 | fn quop(&self) -> i32 { | ||
41 | self.x | ||
42 | } | ||
39 | } | 43 | } |
40 | 44 | ||
41 | static mut STATIC_MUT: i32 = 0; | 45 | static mut STATIC_MUT: i32 = 0; |
@@ -87,6 +91,11 @@ fn main() { | |||
87 | let Foo { x: z, y } = Foo { x: z, y }; | 91 | let Foo { x: z, y } = Foo { x: z, y }; |
88 | 92 | ||
89 | y; | 93 | y; |
94 | |||
95 | let mut foo = Foo { x, y: x }; | ||
96 | foo.quop(); | ||
97 | foo.qux(); | ||
98 | foo.baz(); | ||
90 | } | 99 | } |
91 | 100 | ||
92 | enum Option<T> { | 101 | enum Option<T> { |