Only warn single-use lifetime when the binders match.

This commit is contained in:
Camille GILLOT 2023-05-10 19:46:40 +00:00
parent cba14074bb
commit a2fe9935ea
2 changed files with 12 additions and 5 deletions

View File

@ -1482,7 +1482,7 @@ fn resolve_lifetime(&mut self, lifetime: &'ast Lifetime, use_ctxt: visit::Lifeti
if let Some(&(_, res)) = rib.bindings.get(&normalized_ident) {
self.record_lifetime_res(lifetime.id, res, LifetimeElisionCandidate::Named);
if let LifetimeRes::Param { param, .. } = res {
if let LifetimeRes::Param { param, binder } = res {
match self.lifetime_uses.entry(param) {
Entry::Vacant(v) => {
debug!("First use of {:?} at {:?}", res, ident.span);
@ -1496,10 +1496,16 @@ fn resolve_lifetime(&mut self, lifetime: &'ast Lifetime, use_ctxt: visit::Lifeti
LifetimeRibKind::Item
| LifetimeRibKind::AnonymousReportError
| LifetimeRibKind::ElisionFailure => Some(LifetimeUseSet::Many),
// An anonymous lifetime is legal here, go ahead.
LifetimeRibKind::AnonymousCreateParameter { .. } => {
Some(LifetimeUseSet::One { use_span: ident.span, use_ctxt })
}
// An anonymous lifetime is legal here, and bound to the right
// place, go ahead.
LifetimeRibKind::AnonymousCreateParameter {
binder: anon_binder,
..
} => Some(if binder == anon_binder {
LifetimeUseSet::One { use_span: ident.span, use_ctxt }
} else {
LifetimeUseSet::Many
}),
// Only report if eliding the lifetime would have the same
// semantics.
LifetimeRibKind::Elided(r) => Some(if res == r {

View File

@ -2,6 +2,7 @@
#![feature(inherent_associated_types)]
#![allow(incomplete_features)]
#![deny(single_use_lifetimes)]
struct Foo<T>(T);