ide_diagnostics/handlers/
missing_lifetime.rs

1use crate::{Diagnostic, DiagnosticCode, DiagnosticsContext};
2
3// Diagnostic: missing-lifetime
4//
5// This diagnostic is triggered when a lifetime argument is missing.
6pub(crate) fn missing_lifetime(
7    ctx: &DiagnosticsContext<'_>,
8    d: &hir::MissingLifetime,
9) -> Diagnostic {
10    Diagnostic::new_with_syntax_node_ptr(
11        ctx,
12        DiagnosticCode::RustcHardError("E0106"),
13        "missing lifetime specifier",
14        d.generics_or_segment.map(Into::into),
15    )
16}
17
18#[cfg(test)]
19mod tests {
20    use crate::tests::check_diagnostics;
21
22    #[test]
23    fn in_fields() {
24        check_diagnostics(
25            r#"
26struct Foo<'a>(&'a ());
27struct Bar(Foo);
28        // ^^^ error: missing lifetime specifier
29        "#,
30        );
31    }
32
33    #[test]
34    fn bounds() {
35        check_diagnostics(
36            r#"
37struct Foo<'a, T>(&'a T);
38trait Trait<'a> {
39    type Assoc;
40}
41
42fn foo<'a, T: Trait>(
43           // ^^^^^ error: missing lifetime specifier
44    _: impl Trait<'a, Assoc: Trait>,
45                          // ^^^^^ error: missing lifetime specifier
46)
47where
48    Foo<T>: Trait<'a>,
49    // ^^^ error: missing lifetime specifier
50{
51}
52        "#,
53        );
54    }
55
56    #[test]
57    fn generic_defaults() {
58        check_diagnostics(
59            r#"
60struct Foo<'a>(&'a ());
61
62struct Bar<T = Foo>(T);
63            // ^^^ error: missing lifetime specifier
64        "#,
65        );
66    }
67
68    #[test]
69    fn type_alias_type() {
70        check_diagnostics(
71            r#"
72struct Foo<'a>(&'a ());
73
74type Bar = Foo;
75        // ^^^ error: missing lifetime specifier
76        "#,
77        );
78    }
79
80    #[test]
81    fn const_param_ty() {
82        check_diagnostics(
83            r#"
84struct Foo<'a>(&'a ());
85
86fn bar<const F: Foo>() {}
87             // ^^^ error: missing lifetime specifier
88        "#,
89        );
90    }
91
92    #[test]
93    fn fn_traits() {
94        check_diagnostics(
95            r#"
96//- minicore: fn
97struct WithLifetime<'a>(&'a ());
98
99fn foo<T: Fn(WithLifetime) -> WithLifetime>() {}
100        "#,
101        );
102    }
103}