Skip to content

Commit

Permalink
Auto merge of #10380 - Alexendoo:needless-lifetime-macro-expansion, r…
Browse files Browse the repository at this point in the history
…=xFrednet

Ignore lifetimes from differing contexts in `needless_lifetimes`

Fixes #10379

changelog: [`needless_lifetimes`]: Don't lint signatures in macros if the lifetime is a metavariable
  • Loading branch information
bors committed Feb 21, 2023
2 parents 5ef3cc8 + 0905838 commit 803ce88
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
8 changes: 8 additions & 0 deletions clippy_lints/src/lifetimes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ fn check_fn_inner<'tcx>(
.filter(|param| matches!(param.kind, GenericParamKind::Type { .. }));

for typ in types {
if !typ.span.eq_ctxt(span) {
return;
}

for pred in generics.bounds_for_param(typ.def_id) {
if pred.origin == PredicateOrigin::WhereClause {
// has_where_lifetimes checked that this predicate contains no lifetime.
Expand Down Expand Up @@ -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 usages.iter().any(|usage| !usage.ident.span.eq_ctxt(span)) {
return;
}

let lts = elidable_lts
.iter()
// In principle, the result of the call to `Node::ident` could be `unwrap`ped, as `DefId` should refer to a
Expand Down
10 changes: 10 additions & 0 deletions tests/ui/needless_lifetimes.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,16 @@ mod in_macro {

// no lint on external macro
macro_rules::needless_lifetime!();

macro_rules! expanded_lifetime {
($l:lifetime) => {
fn f<$l>(arg: &$l str) -> &$l str {
arg
}
}
}

expanded_lifetime!('a);
}

mod issue5787 {
Expand Down
10 changes: 10 additions & 0 deletions tests/ui/needless_lifetimes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,16 @@ mod in_macro {

// no lint on external macro
macro_rules::needless_lifetime!();

macro_rules! expanded_lifetime {
($l:lifetime) => {
fn f<$l>(arg: &$l str) -> &$l str {
arg
}
}
}

expanded_lifetime!('a);
}

mod issue5787 {
Expand Down

0 comments on commit 803ce88

Please sign in to comment.