]> git.lizzy.rs Git - rust.git/blob - crates/ide/src/syntax_highlighting/highlight.rs
Split main highlighting function up into a few subfunctions
[rust.git] / crates / ide / src / syntax_highlighting / highlight.rs
1 //! Computes color for a single element.
2
3 use hir::{AsAssocItem, HasVisibility, Semantics};
4 use ide_db::{
5     defs::{Definition, NameClass, NameRefClass},
6     RootDatabase, SymbolKind,
7 };
8 use rustc_hash::FxHashMap;
9 use syntax::{
10     ast, AstNode, AstToken, NodeOrToken, SyntaxElement,
11     SyntaxKind::{self, *},
12     SyntaxNode, SyntaxToken, T,
13 };
14
15 use crate::{
16     syntax_highlighting::tags::{HlOperator, HlPunct},
17     Highlight, HlMod, HlTag,
18 };
19
20 pub(super) fn element(
21     sema: &Semantics<RootDatabase>,
22     krate: Option<hir::Crate>,
23     bindings_shadow_count: &mut FxHashMap<hir::Name, u32>,
24     syntactic_name_ref_highlighting: bool,
25     element: SyntaxElement,
26 ) -> Option<(Highlight, Option<u64>)> {
27     let db = sema.db;
28     let mut binding_hash = None;
29     let highlight: Highlight = match element.kind() {
30         FN => {
31             bindings_shadow_count.clear();
32             return None;
33         }
34         // Highlight definitions depending on the "type" of the definition.
35         NAME => {
36             let name = element.into_node().and_then(ast::Name::cast).unwrap();
37             highlight_name(sema, bindings_shadow_count, &mut binding_hash, krate, name)
38         }
39         // Highlight references like the definitions they resolve to
40         NAME_REF if element.ancestors().any(|it| it.kind() == ATTR) => {
41             // FIXME: We highlight paths in attributes slightly differently to work around this module
42             // currently not knowing about tool attributes and rustc builtin attributes as
43             // we do not want to resolve those to functions that may be defined in scope.
44             let name_ref = element.into_node().and_then(ast::NameRef::cast).unwrap();
45             highlight_name_ref_in_attr(sema, name_ref)
46         }
47         NAME_REF => {
48             let name_ref = element.into_node().and_then(ast::NameRef::cast).unwrap();
49             highlight_name_ref(
50                 sema,
51                 krate,
52                 bindings_shadow_count,
53                 &mut binding_hash,
54                 syntactic_name_ref_highlighting,
55                 name_ref,
56             )
57         }
58
59         // Simple token-based highlighting
60         COMMENT => {
61             let comment = element.into_token().and_then(ast::Comment::cast)?;
62             let h = HlTag::Comment;
63             match comment.kind().doc {
64                 Some(_) => h | HlMod::Documentation,
65                 None => h.into(),
66             }
67         }
68         STRING | BYTE_STRING => HlTag::StringLiteral.into(),
69         ATTR => HlTag::Attribute.into(),
70         INT_NUMBER if element.ancestors().nth(1).map_or(false, |it| it.kind() == FIELD_EXPR) => {
71             SymbolKind::Field.into()
72         }
73         INT_NUMBER | FLOAT_NUMBER => HlTag::NumericLiteral.into(),
74         BYTE => HlTag::ByteLiteral.into(),
75         CHAR => HlTag::CharLiteral.into(),
76         QUESTION => HlTag::Operator(HlOperator::Other) | HlMod::ControlFlow,
77         LIFETIME => {
78             let lifetime = element.into_node().and_then(ast::Lifetime::cast).unwrap();
79
80             match NameClass::classify_lifetime(sema, &lifetime) {
81                 Some(NameClass::Definition(def)) => {
82                     highlight_def(db, krate, def) | HlMod::Definition
83                 }
84                 None => match NameRefClass::classify_lifetime(sema, &lifetime) {
85                     Some(NameRefClass::Definition(def)) => highlight_def(db, krate, def),
86                     _ => SymbolKind::LifetimeParam.into(),
87                 },
88                 _ => Highlight::from(SymbolKind::LifetimeParam) | HlMod::Definition,
89             }
90         }
91         IDENT if parent_matches::<ast::TokenTree>(&element) => HlTag::None.into(),
92         p if p.is_punct() => match p {
93             T![&] if parent_matches::<ast::BinExpr>(&element) => HlOperator::Bitwise.into(),
94             T![&] => {
95                 let h = HlTag::Operator(HlOperator::Other).into();
96                 let is_unsafe = element
97                     .parent()
98                     .and_then(ast::RefExpr::cast)
99                     .map_or(false, |ref_expr| sema.is_unsafe_ref_expr(&ref_expr));
100                 if is_unsafe {
101                     h | HlMod::Unsafe
102                 } else {
103                     h
104                 }
105             }
106             T![::] | T![->] | T![=>] | T![..] | T![=] | T![@] | T![.] => HlOperator::Other.into(),
107             T![!] if parent_matches::<ast::MacroCall>(&element) => SymbolKind::Macro.into(),
108             T![!] if parent_matches::<ast::NeverType>(&element) => HlTag::BuiltinType.into(),
109             T![!] if parent_matches::<ast::PrefixExpr>(&element) => HlOperator::Logical.into(),
110             T![*] if parent_matches::<ast::PtrType>(&element) => HlTag::Keyword.into(),
111             T![*] if parent_matches::<ast::PrefixExpr>(&element) => {
112                 let prefix_expr = element.parent().and_then(ast::PrefixExpr::cast)?;
113
114                 let expr = prefix_expr.expr()?;
115                 let ty = sema.type_of_expr(&expr)?;
116                 if ty.is_raw_ptr() {
117                     HlTag::Operator(HlOperator::Other) | HlMod::Unsafe
118                 } else if let Some(ast::PrefixOp::Deref) = prefix_expr.op_kind() {
119                     HlOperator::Other.into()
120                 } else {
121                     HlPunct::Other.into()
122                 }
123             }
124             T![-] if parent_matches::<ast::PrefixExpr>(&element) => {
125                 let prefix_expr = element.parent().and_then(ast::PrefixExpr::cast)?;
126
127                 let expr = prefix_expr.expr()?;
128                 match expr {
129                     ast::Expr::Literal(_) => HlTag::NumericLiteral,
130                     _ => HlTag::Operator(HlOperator::Other),
131                 }
132                 .into()
133             }
134             _ if parent_matches::<ast::PrefixExpr>(&element) => HlOperator::Other.into(),
135             T![+] | T![-] | T![*] | T![/] | T![+=] | T![-=] | T![*=] | T![/=]
136                 if parent_matches::<ast::BinExpr>(&element) =>
137             {
138                 HlOperator::Arithmetic.into()
139             }
140             T![|] | T![&] | T![!] | T![^] | T![|=] | T![&=] | T![^=]
141                 if parent_matches::<ast::BinExpr>(&element) =>
142             {
143                 HlOperator::Bitwise.into()
144             }
145             T![&&] | T![||] if parent_matches::<ast::BinExpr>(&element) => {
146                 HlOperator::Logical.into()
147             }
148             T![>] | T![<] | T![==] | T![>=] | T![<=] | T![!=]
149                 if parent_matches::<ast::BinExpr>(&element) =>
150             {
151                 HlOperator::Comparison.into()
152             }
153             _ if parent_matches::<ast::BinExpr>(&element) => HlOperator::Other.into(),
154             _ if parent_matches::<ast::RangeExpr>(&element) => HlOperator::Other.into(),
155             _ if parent_matches::<ast::RangePat>(&element) => HlOperator::Other.into(),
156             _ if parent_matches::<ast::RestPat>(&element) => HlOperator::Other.into(),
157             _ if parent_matches::<ast::Attr>(&element) => HlTag::Attribute.into(),
158             kind => match kind {
159                 T!['['] | T![']'] => HlPunct::Bracket,
160                 T!['{'] | T!['}'] => HlPunct::Brace,
161                 T!['('] | T![')'] => HlPunct::Parenthesis,
162                 T![<] | T![>] => HlPunct::Angle,
163                 T![,] => HlPunct::Comma,
164                 T![:] => HlPunct::Colon,
165                 T![;] => HlPunct::Semi,
166                 T![.] => HlPunct::Dot,
167                 _ => HlPunct::Other,
168             }
169             .into(),
170         },
171
172         k if k.is_keyword() => {
173             let h = Highlight::new(HlTag::Keyword);
174             match k {
175                 T![await] => h | HlMod::Async | HlMod::ControlFlow,
176                 T![break]
177                 | T![continue]
178                 | T![else]
179                 | T![if]
180                 | T![in]
181                 | T![loop]
182                 | T![match]
183                 | T![return]
184                 | T![while]
185                 | T![yield] => h | HlMod::ControlFlow,
186                 T![for] if !is_child_of_impl(&element) => h | HlMod::ControlFlow,
187                 T![unsafe] => h | HlMod::Unsafe,
188                 T![true] | T![false] => HlTag::BoolLiteral.into(),
189                 // self is handled as either a Name or NameRef already
190                 T![self] => return None,
191                 T![ref] => element
192                     .parent()
193                     .and_then(ast::IdentPat::cast)
194                     .and_then(|ident_pat| {
195                         if sema.is_unsafe_ident_pat(&ident_pat) {
196                             Some(HlMod::Unsafe)
197                         } else {
198                             None
199                         }
200                     })
201                     .map(|modifier| h | modifier)
202                     .unwrap_or(h),
203                 T![async] => h | HlMod::Async,
204                 _ => h,
205             }
206         }
207
208         _ => return None,
209     };
210
211     return Some((highlight, binding_hash));
212 }
213
214 fn highlight_name_ref_in_attr(sema: &Semantics<RootDatabase>, name_ref: ast::NameRef) -> Highlight {
215     match NameRefClass::classify(sema, &name_ref) {
216         Some(name_class) => match name_class {
217             NameRefClass::Definition(Definition::ModuleDef(hir::ModuleDef::Module(_)))
218                 if name_ref
219                     .syntax()
220                     .ancestors()
221                     .find_map(ast::Path::cast)
222                     .map_or(false, |it| it.parent_path().is_some()) =>
223             {
224                 HlTag::Symbol(SymbolKind::Module)
225             }
226             NameRefClass::Definition(Definition::Macro(m)) if m.kind() == hir::MacroKind::Attr => {
227                 HlTag::Symbol(SymbolKind::Macro)
228             }
229             _ => HlTag::BuiltinAttr,
230         },
231         None => HlTag::BuiltinAttr,
232     }
233     .into()
234 }
235
236 fn highlight_name_ref(
237     sema: &Semantics<RootDatabase>,
238     krate: Option<hir::Crate>,
239     bindings_shadow_count: &mut FxHashMap<hir::Name, u32>,
240     binding_hash: &mut Option<u64>,
241     syntactic_name_ref_highlighting: bool,
242     name_ref: ast::NameRef,
243 ) -> Highlight {
244     let db = sema.db;
245     highlight_method_call_by_name_ref(sema, krate, &name_ref).unwrap_or_else(|| {
246         let name_class = match NameRefClass::classify(sema, &name_ref) {
247             Some(name_kind) => name_kind,
248             None => {
249                 return if syntactic_name_ref_highlighting {
250                     highlight_name_ref_by_syntax(name_ref, sema, krate)
251                 } else {
252                     HlTag::UnresolvedReference.into()
253                 }
254             }
255         };
256         let h = match name_class {
257             NameRefClass::Definition(def) => {
258                 if let Definition::Local(local) = &def {
259                     if let Some(name) = local.name(db) {
260                         let shadow_count = bindings_shadow_count.entry(name.clone()).or_default();
261                         *binding_hash = Some(calc_binding_hash(&name, *shadow_count))
262                     }
263                 };
264
265                 let mut h = highlight_def(db, krate, def);
266
267                 match def {
268                     Definition::Local(local)
269                         if is_consumed_lvalue(name_ref.syntax(), &local, db) =>
270                     {
271                         h |= HlMod::Consuming;
272                     }
273                     Definition::ModuleDef(hir::ModuleDef::Trait(trait_))
274                         if trait_.is_unsafe(db) =>
275                     {
276                         if ast::Impl::for_trait_name_ref(&name_ref).is_some() {
277                             h |= HlMod::Unsafe;
278                         }
279                     }
280                     Definition::Field(field) => {
281                         if let Some(parent) = name_ref.syntax().parent() {
282                             if matches!(parent.kind(), FIELD_EXPR | RECORD_PAT_FIELD) {
283                                 if let hir::VariantDef::Union(_) = field.parent_def(db) {
284                                     h |= HlMod::Unsafe;
285                                 }
286                             }
287                         }
288                     }
289                     _ => (),
290                 }
291
292                 h
293             }
294             NameRefClass::FieldShorthand { .. } => SymbolKind::Field.into(),
295         };
296         if h.tag == HlTag::Symbol(SymbolKind::Module) && name_ref.self_token().is_some() {
297             SymbolKind::SelfParam.into()
298         } else {
299             h
300         }
301     })
302 }
303
304 fn highlight_name(
305     sema: &Semantics<RootDatabase>,
306     bindings_shadow_count: &mut FxHashMap<hir::Name, u32>,
307     binding_hash: &mut Option<u64>,
308     krate: Option<hir::Crate>,
309     name: ast::Name,
310 ) -> Highlight {
311     let db = sema.db;
312     let name_kind = NameClass::classify(sema, &name);
313     if let Some(NameClass::Definition(Definition::Local(local))) = &name_kind {
314         if let Some(name) = local.name(db) {
315             let shadow_count = bindings_shadow_count.entry(name.clone()).or_default();
316             *shadow_count += 1;
317             *binding_hash = Some(calc_binding_hash(&name, *shadow_count))
318         }
319     };
320     match name_kind {
321         Some(NameClass::Definition(def)) => {
322             let mut h = highlight_def(db, krate, def) | HlMod::Definition;
323             if let Definition::ModuleDef(hir::ModuleDef::Trait(trait_)) = &def {
324                 if trait_.is_unsafe(db) {
325                     h |= HlMod::Unsafe;
326                 }
327             }
328             h
329         }
330         Some(NameClass::ConstReference(def)) => highlight_def(db, krate, def),
331         Some(NameClass::PatFieldShorthand { field_ref, .. }) => {
332             let mut h = HlTag::Symbol(SymbolKind::Field).into();
333             if let hir::VariantDef::Union(_) = field_ref.parent_def(db) {
334                 h |= HlMod::Unsafe;
335             }
336             h
337         }
338         None => highlight_name_by_syntax(name) | HlMod::Definition,
339     }
340 }
341
342 fn calc_binding_hash(name: &hir::Name, shadow_count: u32) -> u64 {
343     fn hash<T: std::hash::Hash + std::fmt::Debug>(x: T) -> u64 {
344         use std::{collections::hash_map::DefaultHasher, hash::Hasher};
345
346         let mut hasher = DefaultHasher::new();
347         x.hash(&mut hasher);
348         hasher.finish()
349     }
350
351     hash((name, shadow_count))
352 }
353
354 fn highlight_def(db: &RootDatabase, krate: Option<hir::Crate>, def: Definition) -> Highlight {
355     let mut h = match def {
356         Definition::Macro(_) => Highlight::new(HlTag::Symbol(SymbolKind::Macro)),
357         Definition::Field(_) => Highlight::new(HlTag::Symbol(SymbolKind::Field)),
358         Definition::ModuleDef(def) => match def {
359             hir::ModuleDef::Module(_) => Highlight::new(HlTag::Symbol(SymbolKind::Module)),
360             hir::ModuleDef::Function(func) => {
361                 let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Function));
362                 if let Some(item) = func.as_assoc_item(db) {
363                     h |= HlMod::Associated;
364                     match func.self_param(db) {
365                         Some(sp) => {
366                             if let hir::Access::Exclusive = sp.access(db) {
367                                 h |= HlMod::Mutable;
368                             }
369                         }
370                         None => h |= HlMod::Static,
371                     }
372
373                     match item.container(db) {
374                         hir::AssocItemContainer::Impl(i) => {
375                             if i.trait_(db).is_some() {
376                                 h |= HlMod::Trait;
377                             }
378                         }
379                         hir::AssocItemContainer::Trait(_t) => {
380                             h |= HlMod::Trait;
381                         }
382                     }
383                 }
384
385                 if func.is_unsafe(db) {
386                     h |= HlMod::Unsafe;
387                 }
388                 if func.is_async(db) {
389                     h |= HlMod::Async;
390                 }
391
392                 h
393             }
394             hir::ModuleDef::Adt(adt) => {
395                 let h = match adt {
396                     hir::Adt::Struct(_) => HlTag::Symbol(SymbolKind::Struct),
397                     hir::Adt::Enum(_) => HlTag::Symbol(SymbolKind::Enum),
398                     hir::Adt::Union(_) => HlTag::Symbol(SymbolKind::Union),
399                 };
400
401                 Highlight::new(h)
402             }
403             hir::ModuleDef::Variant(_) => Highlight::new(HlTag::Symbol(SymbolKind::Variant)),
404             hir::ModuleDef::Const(konst) => {
405                 let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Const));
406
407                 if let Some(item) = konst.as_assoc_item(db) {
408                     h |= HlMod::Associated;
409                     match item.container(db) {
410                         hir::AssocItemContainer::Impl(i) => {
411                             if i.trait_(db).is_some() {
412                                 h |= HlMod::Trait;
413                             }
414                         }
415                         hir::AssocItemContainer::Trait(_t) => {
416                             h |= HlMod::Trait;
417                         }
418                     }
419                 }
420
421                 h
422             }
423             hir::ModuleDef::Trait(_) => Highlight::new(HlTag::Symbol(SymbolKind::Trait)),
424             hir::ModuleDef::TypeAlias(type_) => {
425                 let mut h = Highlight::new(HlTag::Symbol(SymbolKind::TypeAlias));
426
427                 if let Some(item) = type_.as_assoc_item(db) {
428                     h |= HlMod::Associated;
429                     match item.container(db) {
430                         hir::AssocItemContainer::Impl(i) => {
431                             if i.trait_(db).is_some() {
432                                 h |= HlMod::Trait;
433                             }
434                         }
435                         hir::AssocItemContainer::Trait(_t) => {
436                             h |= HlMod::Trait;
437                         }
438                     }
439                 }
440
441                 h
442             }
443             hir::ModuleDef::BuiltinType(_) => Highlight::new(HlTag::BuiltinType),
444             hir::ModuleDef::Static(s) => {
445                 let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Static));
446
447                 if s.is_mut(db) {
448                     h |= HlMod::Mutable;
449                     h |= HlMod::Unsafe;
450                 }
451
452                 h
453             }
454         },
455         Definition::SelfType(_) => Highlight::new(HlTag::Symbol(SymbolKind::Impl)),
456         Definition::GenericParam(it) => match it {
457             hir::GenericParam::TypeParam(_) => Highlight::new(HlTag::Symbol(SymbolKind::TypeParam)),
458             hir::GenericParam::ConstParam(_) => {
459                 Highlight::new(HlTag::Symbol(SymbolKind::ConstParam))
460             }
461             hir::GenericParam::LifetimeParam(_) => {
462                 Highlight::new(HlTag::Symbol(SymbolKind::LifetimeParam))
463             }
464         },
465         Definition::Local(local) => {
466             let tag = if local.is_self(db) {
467                 HlTag::Symbol(SymbolKind::SelfParam)
468             } else if local.is_param(db) {
469                 HlTag::Symbol(SymbolKind::ValueParam)
470             } else {
471                 HlTag::Symbol(SymbolKind::Local)
472             };
473             let mut h = Highlight::new(tag);
474             let ty = local.ty(db);
475             if local.is_mut(db) || ty.is_mutable_reference() {
476                 h |= HlMod::Mutable;
477             }
478             if ty.as_callable(db).is_some() || ty.impls_fnonce(db) {
479                 h |= HlMod::Callable;
480             }
481             h
482         }
483         Definition::Label(_) => Highlight::new(HlTag::Symbol(SymbolKind::Label)),
484     };
485
486     let is_from_other_crate = def.module(db).map(hir::Module::krate) != krate;
487     let is_builtin_type = matches!(def, Definition::ModuleDef(hir::ModuleDef::BuiltinType(_)));
488     let is_public = def.visibility(db) == Some(hir::Visibility::Public);
489
490     match (is_from_other_crate, is_builtin_type, is_public) {
491         (true, false, _) => h |= HlMod::Library,
492         (false, _, true) => h |= HlMod::Public,
493         _ => {}
494     }
495
496     h
497 }
498
499 fn highlight_method_call_by_name_ref(
500     sema: &Semantics<RootDatabase>,
501     krate: Option<hir::Crate>,
502     name_ref: &ast::NameRef,
503 ) -> Option<Highlight> {
504     let mc = name_ref.syntax().parent().and_then(ast::MethodCallExpr::cast)?;
505     highlight_method_call(sema, krate, &mc)
506 }
507
508 fn highlight_method_call(
509     sema: &Semantics<RootDatabase>,
510     krate: Option<hir::Crate>,
511     method_call: &ast::MethodCallExpr,
512 ) -> Option<Highlight> {
513     let func = sema.resolve_method_call(method_call)?;
514
515     let mut h = SymbolKind::Function.into();
516     h |= HlMod::Associated;
517
518     if func.is_unsafe(sema.db) || sema.is_unsafe_method_call(method_call) {
519         h |= HlMod::Unsafe;
520     }
521     if func.is_async(sema.db) {
522         h |= HlMod::Async;
523     }
524     if func.as_assoc_item(sema.db).and_then(|it| it.containing_trait(sema.db)).is_some() {
525         h |= HlMod::Trait;
526     }
527
528     let is_from_other_crate = Some(func.module(sema.db).krate()) != krate;
529     let is_public = func.visibility(sema.db) == hir::Visibility::Public;
530
531     if is_from_other_crate {
532         h |= HlMod::Library;
533     } else if is_public {
534         h |= HlMod::Public;
535     }
536
537     if let Some(self_param) = func.self_param(sema.db) {
538         match self_param.access(sema.db) {
539             hir::Access::Shared => (),
540             hir::Access::Exclusive => h |= HlMod::Mutable,
541             hir::Access::Owned => {
542                 if let Some(receiver_ty) =
543                     method_call.receiver().and_then(|it| sema.type_of_expr(&it))
544                 {
545                     if !receiver_ty.is_copy(sema.db) {
546                         h |= HlMod::Consuming
547                     }
548                 }
549             }
550         }
551     }
552     Some(h)
553 }
554
555 fn highlight_name_by_syntax(name: ast::Name) -> Highlight {
556     let default = HlTag::UnresolvedReference;
557
558     let parent = match name.syntax().parent() {
559         Some(it) => it,
560         _ => return default.into(),
561     };
562
563     let tag = match parent.kind() {
564         STRUCT => SymbolKind::Struct,
565         ENUM => SymbolKind::Enum,
566         VARIANT => SymbolKind::Variant,
567         UNION => SymbolKind::Union,
568         TRAIT => SymbolKind::Trait,
569         TYPE_ALIAS => SymbolKind::TypeAlias,
570         TYPE_PARAM => SymbolKind::TypeParam,
571         RECORD_FIELD => SymbolKind::Field,
572         MODULE => SymbolKind::Module,
573         FN => SymbolKind::Function,
574         CONST => SymbolKind::Const,
575         STATIC => SymbolKind::Static,
576         IDENT_PAT => SymbolKind::Local,
577         _ => return default.into(),
578     };
579
580     tag.into()
581 }
582
583 fn highlight_name_ref_by_syntax(
584     name: ast::NameRef,
585     sema: &Semantics<RootDatabase>,
586     krate: Option<hir::Crate>,
587 ) -> Highlight {
588     let default = HlTag::UnresolvedReference;
589
590     let parent = match name.syntax().parent() {
591         Some(it) => it,
592         _ => return default.into(),
593     };
594
595     match parent.kind() {
596         METHOD_CALL_EXPR => ast::MethodCallExpr::cast(parent)
597             .and_then(|it| highlight_method_call(sema, krate, &it))
598             .unwrap_or_else(|| SymbolKind::Function.into()),
599         FIELD_EXPR => {
600             let h = HlTag::Symbol(SymbolKind::Field);
601             let is_union = ast::FieldExpr::cast(parent)
602                 .and_then(|field_expr| sema.resolve_field(&field_expr))
603                 .map_or(false, |field| {
604                     matches!(field.parent_def(sema.db), hir::VariantDef::Union(_))
605                 });
606             if is_union {
607                 h | HlMod::Unsafe
608             } else {
609                 h.into()
610             }
611         }
612         PATH_SEGMENT => {
613             let path = match parent.parent().and_then(ast::Path::cast) {
614                 Some(it) => it,
615                 _ => return default.into(),
616             };
617             let expr = match path.syntax().parent().and_then(ast::PathExpr::cast) {
618                 Some(it) => it,
619                 _ => {
620                     // within path, decide whether it is module or adt by checking for uppercase name
621                     return if name.text().chars().next().unwrap_or_default().is_uppercase() {
622                         SymbolKind::Struct
623                     } else {
624                         SymbolKind::Module
625                     }
626                     .into();
627                 }
628             };
629             let parent = match expr.syntax().parent() {
630                 Some(it) => it,
631                 None => return default.into(),
632             };
633
634             match parent.kind() {
635                 CALL_EXPR => SymbolKind::Function.into(),
636                 _ => if name.text().chars().next().unwrap_or_default().is_uppercase() {
637                     SymbolKind::Struct
638                 } else {
639                     SymbolKind::Const
640                 }
641                 .into(),
642             }
643         }
644         _ => default.into(),
645     }
646 }
647
648 fn is_consumed_lvalue(node: &SyntaxNode, local: &hir::Local, db: &RootDatabase) -> bool {
649     // When lvalues are passed as arguments and they're not Copy, then mark them as Consuming.
650     parents_match(node.clone().into(), &[PATH_SEGMENT, PATH, PATH_EXPR, ARG_LIST])
651         && !local.ty(db).is_copy(db)
652 }
653
654 /// Returns true if the parent nodes of `node` all match the `SyntaxKind`s in `kinds` exactly.
655 fn parents_match(mut node: NodeOrToken<SyntaxNode, SyntaxToken>, mut kinds: &[SyntaxKind]) -> bool {
656     while let (Some(parent), [kind, rest @ ..]) = (&node.parent(), kinds) {
657         if parent.kind() != *kind {
658             return false;
659         }
660
661         // FIXME: Would be nice to get parent out of the match, but binding by-move and by-value
662         // in the same pattern is unstable: rust-lang/rust#68354.
663         node = node.parent().unwrap().into();
664         kinds = rest;
665     }
666
667     // Only true if we matched all expected kinds
668     kinds.len() == 0
669 }
670
671 #[inline]
672 fn parent_matches<N: AstNode>(element: &SyntaxElement) -> bool {
673     element.parent().map_or(false, |it| N::can_cast(it.kind()))
674 }
675
676 fn is_child_of_impl(element: &SyntaxElement) -> bool {
677     match element.parent() {
678         Some(e) => e.kind() == IMPL,
679         _ => false,
680     }
681 }