Skip to content

Commit 803ce88

Browse files
committed
Auto merge of #10380 - Alexendoo:needless-lifetime-macro-expansion, r=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
2 parents 5ef3cc8 + 0905838 commit 803ce88

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

clippy_lints/src/lifetimes.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ fn check_fn_inner<'tcx>(
144144
.filter(|param| matches!(param.kind, GenericParamKind::Type { .. }));
145145

146146
for typ in types {
147+
if !typ.span.eq_ctxt(span) {
148+
return;
149+
}
150+
147151
for pred in generics.bounds_for_param(typ.def_id) {
148152
if pred.origin == PredicateOrigin::WhereClause {
149153
// has_where_lifetimes checked that this predicate contains no lifetime.
@@ -181,6 +185,10 @@ fn check_fn_inner<'tcx>(
181185
}
182186

183187
if let Some((elidable_lts, usages)) = could_use_elision(cx, sig.decl, body, trait_sig, generics.params) {
188+
if usages.iter().any(|usage| !usage.ident.span.eq_ctxt(span)) {
189+
return;
190+
}
191+
184192
let lts = elidable_lts
185193
.iter()
186194
// In principle, the result of the call to `Node::ident` could be `unwrap`ped, as `DefId` should refer to a

tests/ui/needless_lifetimes.fixed

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,16 @@ mod in_macro {
516516

517517
// no lint on external macro
518518
macro_rules::needless_lifetime!();
519+
520+
macro_rules! expanded_lifetime {
521+
($l:lifetime) => {
522+
fn f<$l>(arg: &$l str) -> &$l str {
523+
arg
524+
}
525+
}
526+
}
527+
528+
expanded_lifetime!('a);
519529
}
520530

521531
mod issue5787 {

tests/ui/needless_lifetimes.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,16 @@ mod in_macro {
516516

517517
// no lint on external macro
518518
macro_rules::needless_lifetime!();
519+
520+
macro_rules! expanded_lifetime {
521+
($l:lifetime) => {
522+
fn f<$l>(arg: &$l str) -> &$l str {
523+
arg
524+
}
525+
}
526+
}
527+
528+
expanded_lifetime!('a);
519529
}
520530

521531
mod issue5787 {

0 commit comments

Comments
 (0)