Skip to content

Commit c8e9e52

Browse files
committed
needless-lifetime / add test cases for nested elision sites
1 parent a60e5de commit c8e9e52

File tree

2 files changed

+90
-10
lines changed

2 files changed

+90
-10
lines changed

tests/ui/needless_lifetimes.rs

Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -259,36 +259,86 @@ mod issue4291 {
259259
}
260260
}
261261

262+
mod issue2944 {
263+
trait Foo {}
264+
struct Bar {}
265+
struct Baz<'a> {
266+
bar: &'a Bar,
267+
}
268+
269+
impl<'a> Foo for Baz<'a> {}
270+
impl Bar {
271+
fn baz<'a>(&'a self) -> impl Foo + 'a {
272+
Baz { bar: self }
273+
}
274+
}
275+
}
276+
262277
mod nested_elision_sites {
263-
// Don't lint these cases, they cause FPs.
264-
// The lint does not support nested elision sites.
278+
// issue #issue2944
265279

266-
fn nested_fn_trait_bound<'a>(i: &'a i32) -> impl Fn() -> &'a i32 {
280+
// closure trait bounds subject to nested elision
281+
// don't lint because they refer to outer lifetimes
282+
fn trait_fn<'a>(i: &'a i32) -> impl Fn() -> &'a i32 {
267283
move || i
268284
}
269-
270-
fn nested_fn_mut_trait_bound<'a>(i: &'a i32) -> impl FnMut() -> &'a i32 {
285+
fn trait_fn_mut<'a>(i: &'a i32) -> impl FnMut() -> &'a i32 {
286+
move || i
287+
}
288+
fn trait_fn_once<'a>(i: &'a i32) -> impl FnOnce() -> &'a i32 {
271289
move || i
272290
}
273291

274-
fn nested_fn_once_trait_bound<'a>(i: &'a i32) -> impl FnOnce() -> &'a i32 {
292+
// don't lint
293+
fn impl_trait_in_input_position<'a>(f: impl Fn() -> &'a i32) -> &'a i32 {
294+
f()
295+
}
296+
fn impl_trait_in_output_position<'a>(i: &'a i32) -> impl Fn() -> &'a i32 {
275297
move || i
276298
}
299+
// lint
300+
fn impl_trait_elidable_nested_named_lifetimes<'a>(i: &'a i32, f: impl for<'b> Fn(&'b i32) -> &'b i32) -> &'a i32 {
301+
f(i)
302+
}
303+
fn impl_trait_elidable_nested_anonymous_lifetimes<'a>(i: &'a i32, f: impl Fn(&i32) -> &i32) -> &'a i32 {
304+
f(i)
305+
}
277306

278-
fn nested_generic_fn_trait_bound<'a, T: Fn() -> &'a i32>(f: T) -> &'a i32 {
307+
// don't lint
308+
fn generics_not_elidable<'a, T: Fn() -> &'a i32>(f: T) -> &'a i32 {
279309
f()
280310
}
311+
// lint
312+
fn generics_elidable<'a, T: Fn(&i32) -> &i32>(i: &'a i32, f: T) -> &'a i32 {
313+
f(i)
314+
}
281315

282-
fn nested_where_clause_fn_trait_bound<'a, T>(f: T) -> &'a i32
316+
// don't lint
317+
fn where_clause_not_elidable<'a, T>(f: T) -> &'a i32
283318
where
284319
T: Fn() -> &'a i32,
285320
{
286321
f()
287322
}
323+
// lint
324+
fn where_clause_elidadable<'a, T>(i: &'a i32, f: T) -> &'a i32
325+
where
326+
T: Fn(&i32) -> &i32,
327+
{
328+
f(i)
329+
}
288330

289-
fn nested_pointer_fn<'a>(_: &'a i32) -> fn(&'a i32) -> &'a i32 {
331+
// don't lint
332+
fn pointer_fn_in_input_position<'a>(f: fn(&'a i32) -> &'a i32, i: &'a i32) -> &'a i32 {
333+
f(i)
334+
}
335+
fn pointer_fn_in_output_position<'a>(_: &'a i32) -> fn(&'a i32) -> &'a i32 {
290336
|i| i
291337
}
338+
// lint
339+
fn pointer_fn_elidable<'a>(f: fn(&i32) -> &i32, i: &'a i32) -> &'a i32 {
340+
f(i)
341+
}
292342
}
293343

294344
fn main() {}

tests/ui/needless_lifetimes.stderr

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,5 +102,35 @@ error: explicit lifetimes given in parameter types where they could be elided (o
102102
LL | fn needless_lt<'a>(_x: &'a u8) {}
103103
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
104104

105-
error: aborting due to 17 previous errors
105+
error: explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration)
106+
--> $DIR/needless_lifetimes.rs:271:9
107+
|
108+
LL | fn baz<'a>(&'a self) -> impl Foo + 'a {
109+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
110+
111+
error: explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration)
112+
--> $DIR/needless_lifetimes.rs:300:5
113+
|
114+
LL | fn impl_trait_elidable_nested_named_lifetimes<'a>(i: &'a i32, f: impl for<'b> Fn(&'b i32) -> &'b i32) -> &'a i32 {
115+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
116+
117+
error: explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration)
118+
--> $DIR/needless_lifetimes.rs:303:5
119+
|
120+
LL | fn impl_trait_elidable_nested_anonymous_lifetimes<'a>(i: &'a i32, f: impl Fn(&i32) -> &i32) -> &'a i32 {
121+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
122+
123+
error: explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration)
124+
--> $DIR/needless_lifetimes.rs:312:5
125+
|
126+
LL | fn generics_elidable<'a, T: Fn(&i32) -> &i32>(i: &'a i32, f: T) -> &'a i32 {
127+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
128+
129+
error: explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration)
130+
--> $DIR/needless_lifetimes.rs:324:5
131+
|
132+
LL | fn where_clause_elidadable<'a, T>(i: &'a i32, f: T) -> &'a i32
133+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
134+
135+
error: aborting due to 22 previous errors
106136

0 commit comments

Comments
 (0)