ide_diagnostics/handlers/
elided_lifetimes_in_path.rs

1use crate::{Diagnostic, DiagnosticCode, DiagnosticsContext};
2
3// Diagnostic: elided-lifetimes-in-path
4//
5// This diagnostic is triggered when lifetimes are elided in paths. It is a lint only for some cases,
6// and a hard error for others.
7pub(crate) fn elided_lifetimes_in_path(
8    ctx: &DiagnosticsContext<'_>,
9    d: &hir::ElidedLifetimesInPath,
10) -> Diagnostic {
11    if d.hard_error {
12        Diagnostic::new_with_syntax_node_ptr(
13            ctx,
14            DiagnosticCode::RustcHardError("E0726"),
15            "implicit elided lifetime not allowed here",
16            d.generics_or_segment.map(Into::into),
17        )
18    } else {
19        Diagnostic::new_with_syntax_node_ptr(
20            ctx,
21            DiagnosticCode::RustcLint("elided_lifetimes_in_paths"),
22            "hidden lifetime parameters in types are deprecated",
23            d.generics_or_segment.map(Into::into),
24        )
25    }
26}
27
28#[cfg(test)]
29mod tests {
30    use crate::tests::check_diagnostics;
31
32    #[test]
33    fn fn_() {
34        check_diagnostics(
35            r#"
36#![warn(elided_lifetimes_in_paths)]
37
38struct Foo<'a>(&'a ());
39
40fn foo(_: Foo) {}
41       // ^^^ warn: hidden lifetime parameters in types are deprecated
42        "#,
43        );
44        check_diagnostics(
45            r#"
46#![warn(elided_lifetimes_in_paths)]
47
48struct Foo<'a>(&'a ());
49
50fn foo(_: Foo<'_>) -> Foo { loop {} }
51                   // ^^^ warn: hidden lifetime parameters in types are deprecated
52        "#,
53        );
54    }
55
56    #[test]
57    fn async_fn() {
58        check_diagnostics(
59            r#"
60struct Foo<'a>(&'a ());
61
62async fn foo(_: Foo) {}
63             // ^^^ error: implicit elided lifetime not allowed here
64        "#,
65        );
66        check_diagnostics(
67            r#"
68#![warn(elided_lifetimes_in_paths)]
69
70struct Foo<'a>(&'a ());
71
72fn foo(_: Foo<'_>) -> Foo { loop {} }
73                   // ^^^ warn: hidden lifetime parameters in types are deprecated
74        "#,
75        );
76    }
77
78    #[test]
79    fn no_error_when_explicitly_elided() {
80        check_diagnostics(
81            r#"
82#![warn(elided_lifetimes_in_paths)]
83
84struct Foo<'a>(&'a ());
85trait Trait<'a> {}
86
87fn foo(_: Foo<'_>) -> Foo<'_> { loop {} }
88async fn bar(_: Foo<'_>) -> Foo<'_> { loop {} }
89impl Foo<'_> {}
90impl Trait<'_> for Foo<'_> {}
91        "#,
92        );
93    }
94
95    #[test]
96    fn impl_() {
97        check_diagnostics(
98            r#"
99struct Foo<'a>(&'a ());
100trait Trait<'a> {}
101
102impl Foo {}
103  // ^^^ error: implicit elided lifetime not allowed here
104
105impl Trait for Foo<'_> {}
106  // ^^^^^ error: implicit elided lifetime not allowed here
107        "#,
108        );
109    }
110}