Skip to content

Commit ee78eab

Browse files
Lint redundant lifetimes in impl header
1 parent 2d81354 commit ee78eab

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -2028,19 +2028,20 @@ fn lint_redundant_lifetimes<'tcx>(
20282028
| DefKind::TraitAlias
20292029
| DefKind::Fn
20302030
| DefKind::Const
2031-
| DefKind::Impl { of_trait: false } => {
2031+
| DefKind::Impl { of_trait: _ } => {
20322032
// Proceed
20332033
}
20342034
DefKind::AssocFn | DefKind::AssocTy | DefKind::AssocConst => {
20352035
let parent_def_id = tcx.local_parent(owner_id);
20362036
if matches!(tcx.def_kind(parent_def_id), DefKind::Impl { of_trait: true }) {
2037-
// Don't check for redundant lifetimes for trait implementations,
2038-
// since the signature is required to be compatible with the trait.
2037+
// Don't check for redundant lifetimes for associated items of trait
2038+
// implementations, since the signature is required to be compatible
2039+
// with the trait, even if the implementation implies some lifetimes
2040+
// are redundant.
20392041
return;
20402042
}
20412043
}
2042-
DefKind::Impl { of_trait: true }
2043-
| DefKind::Mod
2044+
DefKind::Mod
20442045
| DefKind::Variant
20452046
| DefKind::TyAlias
20462047
| DefKind::ForeignTy

tests/ui/regions/transitively-redundant-lifetimes.rs

+3
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,7 @@ impl<'a> Bar<'a> {
1515

1616
fn ok(x: &'static &()) {}
1717

18+
trait Tr<'a> {}
19+
impl<'a: 'static> Tr<'a> for () {} //~ ERROR unnecessary lifetime parameter `'a`
20+
1821
fn main() {}

tests/ui/regions/transitively-redundant-lifetimes.stderr

+9-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ LL | fn c<'a>(_: Foo<&'a ()>) {}
2727
|
2828
= note: you can use the `'static` lifetime directly, in place of `'a`
2929

30+
error: unnecessary lifetime parameter `'a`
31+
--> $DIR/transitively-redundant-lifetimes.rs:19:6
32+
|
33+
LL | impl<'a: 'static> Tr<'a> for () {}
34+
| ^^
35+
|
36+
= note: you can use the `'static` lifetime directly, in place of `'a`
37+
3038
error: unnecessary lifetime parameter `'b`
3139
--> $DIR/transitively-redundant-lifetimes.rs:13:10
3240
|
@@ -35,5 +43,5 @@ LL | fn d<'b: 'a>(&'b self) {}
3543
|
3644
= note: you can use the `'a` lifetime directly, in place of `'b`
3745

38-
error: aborting due to 4 previous errors
46+
error: aborting due to 5 previous errors
3947

0 commit comments

Comments
 (0)