]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_error_codes/src/error_codes/E0392.md
Rollup merge of #93556 - dtolnay:trailingcomma, r=cjgillot
[rust.git] / compiler / rustc_error_codes / src / error_codes / E0392.md
1 A type or lifetime parameter has been declared but is not actually used.
2
3 Erroneous code example:
4
5 ```compile_fail,E0392
6 enum Foo<T> {
7     Bar,
8 }
9 ```
10
11 If the type parameter was included by mistake, this error can be fixed
12 by simply removing the type parameter, as shown below:
13
14 ```
15 enum Foo {
16     Bar,
17 }
18 ```
19
20 Alternatively, if the type parameter was intentionally inserted, it must be
21 used. A simple fix is shown below:
22
23 ```
24 enum Foo<T> {
25     Bar(T),
26 }
27 ```
28
29 This error may also commonly be found when working with unsafe code. For
30 example, when using raw pointers one may wish to specify the lifetime for
31 which the pointed-at data is valid. An initial attempt (below) causes this
32 error:
33
34 ```compile_fail,E0392
35 struct Foo<'a, T> {
36     x: *const T,
37 }
38 ```
39
40 We want to express the constraint that Foo should not outlive `'a`, because
41 the data pointed to by `T` is only valid for that lifetime. The problem is
42 that there are no actual uses of `'a`. It's possible to work around this
43 by adding a PhantomData type to the struct, using it to tell the compiler
44 to act as if the struct contained a borrowed reference `&'a T`:
45
46 ```
47 use std::marker::PhantomData;
48
49 struct Foo<'a, T: 'a> {
50     x: *const T,
51     phantom: PhantomData<&'a T>
52 }
53 ```
54
55 [PhantomData] can also be used to express information about unused type
56 parameters.
57
58 [PhantomData]: https://doc.rust-lang.org/std/marker/struct.PhantomData.html