Skip to content

Commit ce1800d

Browse files
committed
Check lifetimes in Fn traits in generic bounds.
Add tests.
1 parent d761ba7 commit ce1800d

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed

clippy_lints/src/lifetimes.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ fn check_fn_inner<'a, 'tcx>(
103103
let mut bounds_lts = Vec::new();
104104
for typ in generics.ty_params() {
105105
for bound in &typ.bounds {
106+
let mut visitor = RefVisitor::new(cx);
107+
walk_ty_param_bound(&mut visitor, bound);
108+
if visitor.lts.iter().any(|lt| matches!(lt, RefLt::Named(_))) {
109+
return;
110+
}
106111
if let TraitTyParamBound(ref trait_ref, _) = *bound {
107112
let params = &trait_ref
108113
.trait_ref

tests/ui/lifetimes.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ fn fn_bound_2<'a, F, I>(_m: Lt<'a, I>, _f: F) -> Lt<'a, I>
5353
where for<'x> F: Fn(Lt<'x, I>) -> Lt<'x, I>
5454
{ unreachable!() }
5555

56+
fn fn_bound_3<'a, F: FnOnce(&'a ())>(x: &'a (), f: F) {} // no error, referenced
57+
58+
fn fn_bound_4<'a, F: FnOnce() -> &'a ()>(x: &'a (), f: F) {} // no error, referenced
59+
5660
struct X {
5761
x: u8,
5862
}

tests/ui/lifetimes.stderr

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,45 +45,45 @@ error: explicit lifetimes given in parameter types where they could be elided
4545
| |__________________^
4646

4747
error: explicit lifetimes given in parameter types where they could be elided
48-
--> $DIR/lifetimes.rs:61:5
48+
--> $DIR/lifetimes.rs:65:5
4949
|
50-
61 | fn self_and_out<'s>(&'s self) -> &'s u8 { &self.x }
50+
65 | fn self_and_out<'s>(&'s self) -> &'s u8 { &self.x }
5151
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5252

5353
error: explicit lifetimes given in parameter types where they could be elided
54-
--> $DIR/lifetimes.rs:65:5
54+
--> $DIR/lifetimes.rs:69:5
5555
|
56-
65 | fn distinct_self_and_in<'s, 't>(&'s self, _x: &'t u8) { }
56+
69 | fn distinct_self_and_in<'s, 't>(&'s self, _x: &'t u8) { }
5757
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5858

5959
error: explicit lifetimes given in parameter types where they could be elided
60-
--> $DIR/lifetimes.rs:81:1
60+
--> $DIR/lifetimes.rs:85:1
6161
|
62-
81 | fn struct_with_lt<'a>(_foo: Foo<'a>) -> &'a str { unimplemented!() }
62+
85 | fn struct_with_lt<'a>(_foo: Foo<'a>) -> &'a str { unimplemented!() }
6363
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6464

6565
error: explicit lifetimes given in parameter types where they could be elided
66-
--> $DIR/lifetimes.rs:101:1
66+
--> $DIR/lifetimes.rs:105:1
6767
|
68-
101 | fn trait_obj_elided2<'a>(_arg: &'a Drop) -> &'a str { unimplemented!() }
68+
105 | fn trait_obj_elided2<'a>(_arg: &'a Drop) -> &'a str { unimplemented!() }
6969
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7070

7171
error: explicit lifetimes given in parameter types where they could be elided
72-
--> $DIR/lifetimes.rs:105:1
72+
--> $DIR/lifetimes.rs:109:1
7373
|
74-
105 | fn alias_with_lt<'a>(_foo: FooAlias<'a>) -> &'a str { unimplemented!() }
74+
109 | fn alias_with_lt<'a>(_foo: FooAlias<'a>) -> &'a str { unimplemented!() }
7575
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7676

7777
error: explicit lifetimes given in parameter types where they could be elided
78-
--> $DIR/lifetimes.rs:116:1
78+
--> $DIR/lifetimes.rs:120:1
7979
|
80-
116 | fn named_input_elided_output<'a>(_arg: &'a str) -> &str { unimplemented!() }
80+
120 | fn named_input_elided_output<'a>(_arg: &'a str) -> &str { unimplemented!() }
8181
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8282

8383
error: explicit lifetimes given in parameter types where they could be elided
84-
--> $DIR/lifetimes.rs:120:1
84+
--> $DIR/lifetimes.rs:124:1
8585
|
86-
120 | fn trait_bound_ok<'a, T: WithLifetime<'static>>(_: &'a u8, _: T) { unimplemented!() }
86+
124 | fn trait_bound_ok<'a, T: WithLifetime<'static>>(_: &'a u8, _: T) { unimplemented!() }
8787
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8888

8989
error: aborting due to 14 previous errors

0 commit comments

Comments
 (0)