]> git.lizzy.rs Git - rust.git/blob - crates/ide/src/inlay_hints/implicit_static.rs
Move inlay hints tests into implementation modules
[rust.git] / crates / ide / src / inlay_hints / implicit_static.rs
1 use either::Either;
2 use syntax::{
3     ast::{self, AstNode},
4     SyntaxKind,
5 };
6
7 use crate::{InlayHint, InlayHintsConfig, InlayKind, InlayTooltip, LifetimeElisionHints};
8
9 pub(super) fn hints(
10     acc: &mut Vec<InlayHint>,
11     config: &InlayHintsConfig,
12     statik_or_const: Either<ast::Static, ast::Const>,
13 ) -> Option<()> {
14     if config.lifetime_elision_hints != LifetimeElisionHints::Always {
15         return None;
16     }
17
18     if let Either::Right(it) = &statik_or_const {
19         if ast::AssocItemList::can_cast(
20             it.syntax().parent().map_or(SyntaxKind::EOF, |it| it.kind()),
21         ) {
22             return None;
23         }
24     }
25
26     if let Some(ast::Type::RefType(ty)) = statik_or_const.either(|it| it.ty(), |it| it.ty()) {
27         if ty.lifetime().is_none() {
28             let t = ty.amp_token()?;
29             acc.push(InlayHint {
30                 range: t.text_range(),
31                 kind: InlayKind::LifetimeHint,
32                 label: "'static".to_owned().into(),
33                 tooltip: Some(InlayTooltip::String("Elided static lifetime".into())),
34             });
35         }
36     }
37
38     Some(())
39 }
40
41 #[cfg(test)]
42 mod tests {
43     use crate::{
44         inlay_hints::tests::{check_with_config, TEST_CONFIG},
45         InlayHintsConfig, LifetimeElisionHints,
46     };
47
48     #[test]
49     fn hints_lifetimes_static() {
50         check_with_config(
51             InlayHintsConfig {
52                 lifetime_elision_hints: LifetimeElisionHints::Always,
53                 ..TEST_CONFIG
54             },
55             r#"
56 trait Trait {}
57 static S: &str = "";
58 //        ^'static
59 const C: &str = "";
60 //       ^'static
61 const C: &dyn Trait = panic!();
62 //       ^'static
63
64 impl () {
65     const C: &str = "";
66     const C: &dyn Trait = panic!();
67 }
68 "#,
69         );
70     }
71 }