1
Fork 0

Lint redundant lifetimes in impl header

This commit is contained in:
Michael Goulet 2023-12-05 16:32:47 +00:00
parent 2d813547bf
commit ee78eab62b
3 changed files with 18 additions and 6 deletions

View file

@ -2028,19 +2028,20 @@ fn lint_redundant_lifetimes<'tcx>(
| DefKind::TraitAlias | DefKind::TraitAlias
| DefKind::Fn | DefKind::Fn
| DefKind::Const | DefKind::Const
| DefKind::Impl { of_trait: false } => { | DefKind::Impl { of_trait: _ } => {
// Proceed // Proceed
} }
DefKind::AssocFn | DefKind::AssocTy | DefKind::AssocConst => { DefKind::AssocFn | DefKind::AssocTy | DefKind::AssocConst => {
let parent_def_id = tcx.local_parent(owner_id); let parent_def_id = tcx.local_parent(owner_id);
if matches!(tcx.def_kind(parent_def_id), DefKind::Impl { of_trait: true }) { if matches!(tcx.def_kind(parent_def_id), DefKind::Impl { of_trait: true }) {
// Don't check for redundant lifetimes for trait implementations, // Don't check for redundant lifetimes for associated items of trait
// since the signature is required to be compatible with the trait. // implementations, since the signature is required to be compatible
// with the trait, even if the implementation implies some lifetimes
// are redundant.
return; return;
} }
} }
DefKind::Impl { of_trait: true } DefKind::Mod
| DefKind::Mod
| DefKind::Variant | DefKind::Variant
| DefKind::TyAlias | DefKind::TyAlias
| DefKind::ForeignTy | DefKind::ForeignTy

View file

@ -15,4 +15,7 @@ impl<'a> Bar<'a> {
fn ok(x: &'static &()) {} fn ok(x: &'static &()) {}
trait Tr<'a> {}
impl<'a: 'static> Tr<'a> for () {} //~ ERROR unnecessary lifetime parameter `'a`
fn main() {} fn main() {}

View file

@ -27,6 +27,14 @@ LL | fn c<'a>(_: Foo<&'a ()>) {}
| |
= note: you can use the `'static` lifetime directly, in place of `'a` = note: you can use the `'static` lifetime directly, in place of `'a`
error: unnecessary lifetime parameter `'a`
--> $DIR/transitively-redundant-lifetimes.rs:19:6
|
LL | impl<'a: 'static> Tr<'a> for () {}
| ^^
|
= note: you can use the `'static` lifetime directly, in place of `'a`
error: unnecessary lifetime parameter `'b` error: unnecessary lifetime parameter `'b`
--> $DIR/transitively-redundant-lifetimes.rs:13:10 --> $DIR/transitively-redundant-lifetimes.rs:13:10
| |
@ -35,5 +43,5 @@ LL | fn d<'b: 'a>(&'b self) {}
| |
= note: you can use the `'a` lifetime directly, in place of `'b` = note: you can use the `'a` lifetime directly, in place of `'b`
error: aborting due to 4 previous errors error: aborting due to 5 previous errors