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