about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2023-02-21 20:47:30 +0000
committerbors <bors@rust-lang.org>2023-02-21 20:47:30 +0000
commit803ce88477f83810d8adb04ec794616c8e1ab4ed (patch)
treedde3c5e0a95b4341a6cc5389e60b0a28da52758b
parent5ef3cc8ca1af8df6421adf2e600b774c8e055589 (diff)
parent09058388dec7b62680fd3d33df4b416d84975884 (diff)
downloadrust-803ce88477f83810d8adb04ec794616c8e1ab4ed.tar.gz
rust-803ce88477f83810d8adb04ec794616c8e1ab4ed.zip
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
-rw-r--r--clippy_lints/src/lifetimes.rs8
-rw-r--r--tests/ui/needless_lifetimes.fixed10
-rw-r--r--tests/ui/needless_lifetimes.rs10
3 files changed, 28 insertions, 0 deletions
diff --git a/clippy_lints/src/lifetimes.rs b/clippy_lints/src/lifetimes.rs
index 43a1a65a43a..986ffcad883 100644
--- a/clippy_lints/src/lifetimes.rs
+++ b/clippy_lints/src/lifetimes.rs
@@ -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.
@@ -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
diff --git a/tests/ui/needless_lifetimes.fixed b/tests/ui/needless_lifetimes.fixed
index d286ef4ba37..f0f1f9298ac 100644
--- a/tests/ui/needless_lifetimes.fixed
+++ b/tests/ui/needless_lifetimes.fixed
@@ -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 {
diff --git a/tests/ui/needless_lifetimes.rs b/tests/ui/needless_lifetimes.rs
index 409528b291d..ddfd1043003 100644
--- a/tests/ui/needless_lifetimes.rs
+++ b/tests/ui/needless_lifetimes.rs
@@ -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 {