Skip to content

Commit d67286b

Browse files
authored
Merge pull request #3286 from vmalloc/lifetime_elision_msg
Improve diagnostics in case of lifetime elision (closes #3284)
2 parents 02705d4 + 1ef32e4 commit d67286b

File tree

3 files changed

+28
-16
lines changed

3 files changed

+28
-16
lines changed

clippy_lints/src/lifetimes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ fn check_fn_inner<'a, 'tcx>(
152152
cx,
153153
NEEDLESS_LIFETIMES,
154154
span,
155-
"explicit lifetimes given in parameter types where they could be elided",
155+
"explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration)",
156156
);
157157
}
158158
report_extra_lifetimes(cx, decl, generics);

tests/ui/lifetimes.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,5 +170,11 @@ fn test<'a>(x: &'a [u8]) -> u8 {
170170
*y
171171
}
172172

173+
// #3284 - Give a hint regarding lifetime in return type
174+
175+
struct Cow<'a> { x: &'a str, }
176+
fn out_return_type_lts<'a>(e: &'a str) -> Cow<'a> { unimplemented!() }
177+
178+
173179
fn main() {
174180
}

tests/ui/lifetimes.stderr

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,96 @@
1-
error: explicit lifetimes given in parameter types where they could be elided
1+
error: explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration)
22
--> $DIR/lifetimes.rs:17:1
33
|
44
17 | fn distinct_lifetimes<'a, 'b>(_x: &'a u8, _y: &'b u8, _z: u8) { }
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
77
= note: `-D clippy::needless-lifetimes` implied by `-D warnings`
88

9-
error: explicit lifetimes given in parameter types where they could be elided
9+
error: explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration)
1010
--> $DIR/lifetimes.rs:19:1
1111
|
1212
19 | fn distinct_and_static<'a, 'b>(_x: &'a u8, _y: &'b u8, _z: &'static u8) { }
1313
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1414

15-
error: explicit lifetimes given in parameter types where they could be elided
15+
error: explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration)
1616
--> $DIR/lifetimes.rs:27:1
1717
|
1818
27 | fn in_and_out<'a>(x: &'a u8, _y: u8) -> &'a u8 { x }
1919
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2020

21-
error: explicit lifetimes given in parameter types where they could be elided
21+
error: explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration)
2222
--> $DIR/lifetimes.rs:39:1
2323
|
2424
39 | fn deep_reference_3<'a>(x: &'a u8, _y: u8) -> Result<&'a u8, ()> { Ok(x) }
2525
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2626

27-
error: explicit lifetimes given in parameter types where they could be elided
27+
error: explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration)
2828
--> $DIR/lifetimes.rs:42:1
2929
|
3030
42 | fn where_clause_without_lt<'a, T>(x: &'a u8, _y: u8) -> Result<&'a u8, ()> where T: Copy { Ok(x) }
3131
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3232

33-
error: explicit lifetimes given in parameter types where they could be elided
33+
error: explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration)
3434
--> $DIR/lifetimes.rs:48:1
3535
|
3636
48 | fn lifetime_param_2<'a, 'b>(_x: Ref<'a>, _y: &'b u8) { }
3737
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3838

39-
error: explicit lifetimes given in parameter types where they could be elided
39+
error: explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration)
4040
--> $DIR/lifetimes.rs:62:1
4141
|
4242
62 | / fn fn_bound_2<'a, F, I>(_m: Lt<'a, I>, _f: F) -> Lt<'a, I>
4343
63 | | where for<'x> F: Fn(Lt<'x, I>) -> Lt<'x, I>
4444
64 | | { unreachable!() }
4545
| |__________________^
4646

47-
error: explicit lifetimes given in parameter types where they could be elided
47+
error: explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration)
4848
--> $DIR/lifetimes.rs:87:5
4949
|
5050
87 | fn self_and_out<'s>(&'s self) -> &'s u8 { &self.x }
5151
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5252

53-
error: explicit lifetimes given in parameter types where they could be elided
53+
error: explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration)
5454
--> $DIR/lifetimes.rs:91:5
5555
|
5656
91 | fn distinct_self_and_in<'s, 't>(&'s self, _x: &'t u8) { }
5757
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5858

59-
error: explicit lifetimes given in parameter types where they could be elided
59+
error: explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration)
6060
--> $DIR/lifetimes.rs:107:1
6161
|
6262
107 | fn struct_with_lt<'a>(_foo: Foo<'a>) -> &'a str { unimplemented!() }
6363
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6464

65-
error: explicit lifetimes given in parameter types where they could be elided
65+
error: explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration)
6666
--> $DIR/lifetimes.rs:127:1
6767
|
6868
127 | fn trait_obj_elided2<'a>(_arg: &'a Drop) -> &'a str { unimplemented!() }
6969
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7070

71-
error: explicit lifetimes given in parameter types where they could be elided
71+
error: explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration)
7272
--> $DIR/lifetimes.rs:131:1
7373
|
7474
131 | fn alias_with_lt<'a>(_foo: FooAlias<'a>) -> &'a str { unimplemented!() }
7575
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7676

77-
error: explicit lifetimes given in parameter types where they could be elided
77+
error: explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration)
7878
--> $DIR/lifetimes.rs:142:1
7979
|
8080
142 | fn named_input_elided_output<'a>(_arg: &'a str) -> &str { unimplemented!() }
8181
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8282

83-
error: explicit lifetimes given in parameter types where they could be elided
83+
error: explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration)
8484
--> $DIR/lifetimes.rs:146:1
8585
|
8686
146 | fn trait_bound_ok<'a, T: WithLifetime<'static>>(_: &'a u8, _: T) { unimplemented!() }
8787
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8888

89-
error: aborting due to 14 previous errors
89+
error: explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration)
90+
--> $DIR/lifetimes.rs:176:1
91+
|
92+
176 | fn out_return_type_lts<'a>(e: &'a str) -> Cow<'a> { unimplemented!() }
93+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
94+
95+
error: aborting due to 15 previous errors
9096

0 commit comments

Comments
 (0)