Ignore lifetimes from differing contexts in needless_lifetimes

This commit is contained in:
Alex Macleod 2023-02-20 09:47:05 +00:00
parent 85d4b5ac48
commit 09058388de
3 changed files with 28 additions and 0 deletions

View file

@ -144,6 +144,10 @@ fn check_fn_inner<'tcx>(
.filter(|param| matches!(param.kind, GenericParamKind::Type { .. })); .filter(|param| matches!(param.kind, GenericParamKind::Type { .. }));
for typ in types { for typ in types {
if !typ.span.eq_ctxt(span) {
return;
}
for pred in generics.bounds_for_param(typ.def_id) { for pred in generics.bounds_for_param(typ.def_id) {
if pred.origin == PredicateOrigin::WhereClause { if pred.origin == PredicateOrigin::WhereClause {
// has_where_lifetimes checked that this predicate contains no lifetime. // has_where_lifetimes checked that this predicate contains no lifetime.
@ -181,6 +185,10 @@ fn check_fn_inner<'tcx>(
} }
if let Some((elidable_lts, usages)) = could_use_elision(cx, sig.decl, body, trait_sig, generics.params) { if let Some((elidable_lts, usages)) = could_use_elision(cx, sig.decl, body, trait_sig, generics.params) {
if usages.iter().any(|usage| !usage.ident.span.eq_ctxt(span)) {
return;
}
let lts = elidable_lts let lts = elidable_lts
.iter() .iter()
// In principle, the result of the call to `Node::ident` could be `unwrap`ped, as `DefId` should refer to a // In principle, the result of the call to `Node::ident` could be `unwrap`ped, as `DefId` should refer to a

View file

@ -516,6 +516,16 @@ mod in_macro {
// no lint on external macro // no lint on external macro
macro_rules::needless_lifetime!(); macro_rules::needless_lifetime!();
macro_rules! expanded_lifetime {
($l:lifetime) => {
fn f<$l>(arg: &$l str) -> &$l str {
arg
}
}
}
expanded_lifetime!('a);
} }
mod issue5787 { mod issue5787 {

View file

@ -516,6 +516,16 @@ mod in_macro {
// no lint on external macro // no lint on external macro
macro_rules::needless_lifetime!(); macro_rules::needless_lifetime!();
macro_rules! expanded_lifetime {
($l:lifetime) => {
fn f<$l>(arg: &$l str) -> &$l str {
arg
}
}
}
expanded_lifetime!('a);
} }
mod issue5787 { mod issue5787 {