Test needless_lifetimes within external macro

Signed-off-by: Tyler Weaver <maybe@tylerjw.dev>
This commit is contained in:
Tyler Weaver 2023-01-30 16:04:19 -07:00
parent c959813bfd
commit 4fde96c30e
No known key found for this signature in database
4 changed files with 89 additions and 61 deletions

View file

@ -13,8 +13,9 @@ use rustc_hir::{
ImplItemKind, Item, ItemKind, Lifetime, LifetimeName, LifetimeParamKind, PolyTraitRef, PredicateOrigin, TraitFn,
TraitItem, TraitItemKind, Ty, TyKind, WherePredicate,
};
use rustc_lint::{LateContext, LateLintPass};
use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_middle::hir::nested_filter as middle_nested_filter;
use rustc_middle::lint::in_external_macro;
use rustc_middle::ty::TyCtxt;
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::def_id::LocalDefId;
@ -144,7 +145,7 @@ fn check_fn_inner<'tcx>(
span: Span,
report_extra_lifetimes: bool,
) {
if span.from_expansion() || has_where_lifetimes(cx, generics) {
if in_external_macro(cx.sess(), span) || has_where_lifetimes(cx, generics) {
return;
}

View file

@ -159,3 +159,12 @@ macro_rules! unsafe_macro {
}
};
}
#[macro_export]
macro_rules! needless_lifetime {
() => {
fn needless_lifetime<'a>(x: &'a u8) -> &'a u8 {
unimplemented!()
}
};
}

View file

@ -1,3 +1,4 @@
// aux-build:macro_rules.rs
#![warn(clippy::needless_lifetimes)]
#![allow(
dead_code,
@ -8,6 +9,9 @@
clippy::get_first
)]
#[macro_use]
extern crate macro_rules;
fn distinct_lifetimes<'a, 'b>(_x: &'a u8, _y: &'b u8, _z: u8) {}
fn distinct_and_static<'a, 'b>(_x: &'a u8, _y: &'b u8, _z: &'static u8) {}
@ -495,17 +499,20 @@ mod pr_9743_output_lifetime_checks {
}
}
mod skip_inside_macros {
macro_rules! print_with_one_input {
($a:expr) => {
fn print_with_one_input<'a>(x: &'a u8) -> &'a u8 {
println!("{}", $a);
mod in_macro {
macro_rules! local_one_input_macro {
() => {
fn one_input<'a>(x: &'a u8) -> &'a u8 {
unimplemented!()
}
};
}
print_with_one_input!("this is a dandy little string literal");
// lint local macro expands to function with needless lifetimes
local_one_input_macro!();
// no lint on external macro
macro_rules::needless_lifetime!();
}
fn main() {}

View file

@ -1,5 +1,5 @@
error: the following explicit lifetimes could be elided: 'a, 'b
--> $DIR/needless_lifetimes.rs:11:1
--> $DIR/needless_lifetimes.rs:15:1
|
LL | fn distinct_lifetimes<'a, 'b>(_x: &'a u8, _y: &'b u8, _z: u8) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -7,310 +7,321 @@ LL | fn distinct_lifetimes<'a, 'b>(_x: &'a u8, _y: &'b u8, _z: u8) {}
= note: `-D clippy::needless-lifetimes` implied by `-D warnings`
error: the following explicit lifetimes could be elided: 'a, 'b
--> $DIR/needless_lifetimes.rs:13:1
--> $DIR/needless_lifetimes.rs:17:1
|
LL | fn distinct_and_static<'a, 'b>(_x: &'a u8, _y: &'b u8, _z: &'static u8) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: the following explicit lifetimes could be elided: 'a
--> $DIR/needless_lifetimes.rs:23:1
--> $DIR/needless_lifetimes.rs:27:1
|
LL | fn in_and_out<'a>(x: &'a u8, _y: u8) -> &'a u8 {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: the following explicit lifetimes could be elided: 'b
--> $DIR/needless_lifetimes.rs:35:1
--> $DIR/needless_lifetimes.rs:39:1
|
LL | fn multiple_in_and_out_2a<'a, 'b>(x: &'a u8, _y: &'b u8) -> &'a u8 {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: the following explicit lifetimes could be elided: 'a
--> $DIR/needless_lifetimes.rs:42:1
--> $DIR/needless_lifetimes.rs:46:1
|
LL | fn multiple_in_and_out_2b<'a, 'b>(_x: &'a u8, y: &'b u8) -> &'b u8 {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: the following explicit lifetimes could be elided: 'b
--> $DIR/needless_lifetimes.rs:59:1
--> $DIR/needless_lifetimes.rs:63:1
|
LL | fn deep_reference_1a<'a, 'b>(x: &'a u8, _y: &'b u8) -> Result<&'a u8, ()> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: the following explicit lifetimes could be elided: 'a
--> $DIR/needless_lifetimes.rs:66:1
--> $DIR/needless_lifetimes.rs:70:1
|
LL | fn deep_reference_1b<'a, 'b>(_x: &'a u8, y: &'b u8) -> Result<&'b u8, ()> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: the following explicit lifetimes could be elided: 'a
--> $DIR/needless_lifetimes.rs:75:1
--> $DIR/needless_lifetimes.rs:79:1
|
LL | fn deep_reference_3<'a>(x: &'a u8, _y: u8) -> Result<&'a u8, ()> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: the following explicit lifetimes could be elided: 'a
--> $DIR/needless_lifetimes.rs:80:1
--> $DIR/needless_lifetimes.rs:84:1
|
LL | fn where_clause_without_lt<'a, T>(x: &'a u8, _y: u8) -> Result<&'a u8, ()>
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: the following explicit lifetimes could be elided: 'a, 'b
--> $DIR/needless_lifetimes.rs:92:1
--> $DIR/needless_lifetimes.rs:96:1
|
LL | fn lifetime_param_2<'a, 'b>(_x: Ref<'a>, _y: &'b u8) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: replace with `'_` in generic arguments such as here
--> $DIR/needless_lifetimes.rs:92:37
--> $DIR/needless_lifetimes.rs:96:37
|
LL | fn lifetime_param_2<'a, 'b>(_x: Ref<'a>, _y: &'b u8) {}
| ^^
error: the following explicit lifetimes could be elided: 'a
--> $DIR/needless_lifetimes.rs:116:1
--> $DIR/needless_lifetimes.rs:120:1
|
LL | fn fn_bound_2<'a, F, I>(_m: Lt<'a, I>, _f: F) -> Lt<'a, I>
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: replace with `'_` in generic arguments such as here
--> $DIR/needless_lifetimes.rs:116:32
--> $DIR/needless_lifetimes.rs:120:32
|
LL | fn fn_bound_2<'a, F, I>(_m: Lt<'a, I>, _f: F) -> Lt<'a, I>
| ^^
error: the following explicit lifetimes could be elided: 's
--> $DIR/needless_lifetimes.rs:146:5
--> $DIR/needless_lifetimes.rs:150:5
|
LL | fn self_and_out<'s>(&'s self) -> &'s u8 {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: the following explicit lifetimes could be elided: 't
--> $DIR/needless_lifetimes.rs:153:5
--> $DIR/needless_lifetimes.rs:157:5
|
LL | fn self_and_in_out_1<'s, 't>(&'s self, _x: &'t u8) -> &'s u8 {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: the following explicit lifetimes could be elided: 's
--> $DIR/needless_lifetimes.rs:160:5
--> $DIR/needless_lifetimes.rs:164:5
|
LL | fn self_and_in_out_2<'s, 't>(&'s self, x: &'t u8) -> &'t u8 {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: the following explicit lifetimes could be elided: 's, 't
--> $DIR/needless_lifetimes.rs:164:5
--> $DIR/needless_lifetimes.rs:168:5
|
LL | fn distinct_self_and_in<'s, 't>(&'s self, _x: &'t u8) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: the following explicit lifetimes could be elided: 'a
--> $DIR/needless_lifetimes.rs:183:1
--> $DIR/needless_lifetimes.rs:187:1
|
LL | fn struct_with_lt<'a>(_foo: Foo<'a>) -> &'a str {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: replace with `'_` in generic arguments such as here
--> $DIR/needless_lifetimes.rs:183:33
--> $DIR/needless_lifetimes.rs:187:33
|
LL | fn struct_with_lt<'a>(_foo: Foo<'a>) -> &'a str {
| ^^
error: the following explicit lifetimes could be elided: 'b
--> $DIR/needless_lifetimes.rs:201:1
--> $DIR/needless_lifetimes.rs:205:1
|
LL | fn struct_with_lt4a<'a, 'b>(_foo: &'a Foo<'b>) -> &'a str {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: replace with `'_` in generic arguments such as here
--> $DIR/needless_lifetimes.rs:201:43
--> $DIR/needless_lifetimes.rs:205:43
|
LL | fn struct_with_lt4a<'a, 'b>(_foo: &'a Foo<'b>) -> &'a str {
| ^^
error: the following explicit lifetimes could be elided: 'a
--> $DIR/needless_lifetimes.rs:209:1
--> $DIR/needless_lifetimes.rs:213:1
|
LL | fn struct_with_lt4b<'a, 'b>(_foo: &'a Foo<'b>) -> &'b str {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: the following explicit lifetimes could be elided: 'a
--> $DIR/needless_lifetimes.rs:224:1
--> $DIR/needless_lifetimes.rs:228:1
|
LL | fn trait_obj_elided2<'a>(_arg: &'a dyn Drop) -> &'a str {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: the following explicit lifetimes could be elided: 'a
--> $DIR/needless_lifetimes.rs:230:1
--> $DIR/needless_lifetimes.rs:234:1
|
LL | fn alias_with_lt<'a>(_foo: FooAlias<'a>) -> &'a str {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: replace with `'_` in generic arguments such as here
--> $DIR/needless_lifetimes.rs:230:37
--> $DIR/needless_lifetimes.rs:234:37
|
LL | fn alias_with_lt<'a>(_foo: FooAlias<'a>) -> &'a str {
| ^^
error: the following explicit lifetimes could be elided: 'b
--> $DIR/needless_lifetimes.rs:248:1
--> $DIR/needless_lifetimes.rs:252:1
|
LL | fn alias_with_lt4a<'a, 'b>(_foo: &'a FooAlias<'b>) -> &'a str {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: replace with `'_` in generic arguments such as here
--> $DIR/needless_lifetimes.rs:248:47
--> $DIR/needless_lifetimes.rs:252:47
|
LL | fn alias_with_lt4a<'a, 'b>(_foo: &'a FooAlias<'b>) -> &'a str {
| ^^
error: the following explicit lifetimes could be elided: 'a
--> $DIR/needless_lifetimes.rs:256:1
--> $DIR/needless_lifetimes.rs:260:1
|
LL | fn alias_with_lt4b<'a, 'b>(_foo: &'a FooAlias<'b>) -> &'b str {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: the following explicit lifetimes could be elided: 'a
--> $DIR/needless_lifetimes.rs:260:1
--> $DIR/needless_lifetimes.rs:264:1
|
LL | fn named_input_elided_output<'a>(_arg: &'a str) -> &str {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: the following explicit lifetimes could be elided: 'a
--> $DIR/needless_lifetimes.rs:268:1
--> $DIR/needless_lifetimes.rs:272:1
|
LL | fn trait_bound_ok<'a, T: WithLifetime<'static>>(_: &'a u8, _: T) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: the following explicit lifetimes could be elided: 'a
--> $DIR/needless_lifetimes.rs:304:1
--> $DIR/needless_lifetimes.rs:308:1
|
LL | fn out_return_type_lts<'a>(e: &'a str) -> Cow<'a> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: replace with `'_` in generic arguments such as here
--> $DIR/needless_lifetimes.rs:304:47
--> $DIR/needless_lifetimes.rs:308:47
|
LL | fn out_return_type_lts<'a>(e: &'a str) -> Cow<'a> {
| ^^
error: the following explicit lifetimes could be elided: 'a
--> $DIR/needless_lifetimes.rs:311:9
--> $DIR/needless_lifetimes.rs:315:9
|
LL | fn needless_lt<'a>(x: &'a u8) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: the following explicit lifetimes could be elided: 'a
--> $DIR/needless_lifetimes.rs:315:9
--> $DIR/needless_lifetimes.rs:319:9
|
LL | fn needless_lt<'a>(_x: &'a u8) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: the following explicit lifetimes could be elided: 'a
--> $DIR/needless_lifetimes.rs:328:9
--> $DIR/needless_lifetimes.rs:332:9
|
LL | fn baz<'a>(&'a self) -> impl Foo + 'a {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: the following explicit lifetimes could be elided: 'a
--> $DIR/needless_lifetimes.rs:360:5
--> $DIR/needless_lifetimes.rs:364:5
|
LL | fn impl_trait_elidable_nested_anonymous_lifetimes<'a>(i: &'a i32, f: impl Fn(&i32) -> &i32) -> &'a i32 {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: the following explicit lifetimes could be elided: 'a
--> $DIR/needless_lifetimes.rs:369:5
--> $DIR/needless_lifetimes.rs:373:5
|
LL | fn generics_elidable<'a, T: Fn(&i32) -> &i32>(i: &'a i32, f: T) -> &'a i32 {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: the following explicit lifetimes could be elided: 'a
--> $DIR/needless_lifetimes.rs:381:5
--> $DIR/needless_lifetimes.rs:385:5
|
LL | fn where_clause_elidadable<'a, T>(i: &'a i32, f: T) -> &'a i32
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: the following explicit lifetimes could be elided: 'a
--> $DIR/needless_lifetimes.rs:396:5
--> $DIR/needless_lifetimes.rs:400:5
|
LL | fn pointer_fn_elidable<'a>(i: &'a i32, f: fn(&i32) -> &i32) -> &'a i32 {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: the following explicit lifetimes could be elided: 'a
--> $DIR/needless_lifetimes.rs:409:5
--> $DIR/needless_lifetimes.rs:413:5
|
LL | fn nested_fn_pointer_3<'a>(_: &'a i32) -> fn(fn(&i32) -> &i32) -> i32 {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: the following explicit lifetimes could be elided: 'a
--> $DIR/needless_lifetimes.rs:412:5
--> $DIR/needless_lifetimes.rs:416:5
|
LL | fn nested_fn_pointer_4<'a>(_: &'a i32) -> impl Fn(fn(&i32)) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: the following explicit lifetimes could be elided: 'a
--> $DIR/needless_lifetimes.rs:434:9
--> $DIR/needless_lifetimes.rs:438:9
|
LL | fn implicit<'a>(&'a self) -> &'a () {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: the following explicit lifetimes could be elided: 'a
--> $DIR/needless_lifetimes.rs:437:9
--> $DIR/needless_lifetimes.rs:441:9
|
LL | fn implicit_mut<'a>(&'a mut self) -> &'a () {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: the following explicit lifetimes could be elided: 'a
--> $DIR/needless_lifetimes.rs:448:9
--> $DIR/needless_lifetimes.rs:452:9
|
LL | fn lifetime_elsewhere<'a>(self: Box<Self>, here: &'a ()) -> &'a () {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: the following explicit lifetimes could be elided: 'a
--> $DIR/needless_lifetimes.rs:454:9
--> $DIR/needless_lifetimes.rs:458:9
|
LL | fn implicit<'a>(&'a self) -> &'a ();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: the following explicit lifetimes could be elided: 'a
--> $DIR/needless_lifetimes.rs:455:9
--> $DIR/needless_lifetimes.rs:459:9
|
LL | fn implicit_provided<'a>(&'a self) -> &'a () {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: the following explicit lifetimes could be elided: 'a
--> $DIR/needless_lifetimes.rs:464:9
--> $DIR/needless_lifetimes.rs:468:9
|
LL | fn lifetime_elsewhere<'a>(self: Box<Self>, here: &'a ()) -> &'a ();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: the following explicit lifetimes could be elided: 'a
--> $DIR/needless_lifetimes.rs:465:9
--> $DIR/needless_lifetimes.rs:469:9
|
LL | fn lifetime_elsewhere_provided<'a>(self: Box<Self>, here: &'a ()) -> &'a () {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: the following explicit lifetimes could be elided: 'a
--> $DIR/needless_lifetimes.rs:474:5
--> $DIR/needless_lifetimes.rs:478:5
|
LL | fn foo<'a>(x: &'a u8, y: &'_ u8) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: the following explicit lifetimes could be elided: 'a
--> $DIR/needless_lifetimes.rs:476:5
--> $DIR/needless_lifetimes.rs:480:5
|
LL | fn bar<'a>(x: &'a u8, y: &'_ u8, z: &'_ u8) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: the following explicit lifetimes could be elided: 'a
--> $DIR/needless_lifetimes.rs:483:5
--> $DIR/needless_lifetimes.rs:487:5
|
LL | fn one_input<'a>(x: &'a u8) -> &'a u8 {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: the following explicit lifetimes could be elided: 'a
--> $DIR/needless_lifetimes.rs:488:5
--> $DIR/needless_lifetimes.rs:492:5
|
LL | fn multiple_inputs_output_not_elided<'a, 'b>(x: &'a u8, y: &'b u8, z: &'b u8) -> &'b u8 {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 45 previous errors
error: the following explicit lifetimes could be elided: 'a
--> $DIR/needless_lifetimes.rs:505:13
|
LL | fn one_input<'a>(x: &'a u8) -> &'a u8 {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
LL | local_one_input_macro!();
| ------------------------ in this macro invocation
|
= note: this error originates in the macro `local_one_input_macro` (in Nightly builds, run with -Z macro-backtrace for more info)
error: aborting due to 46 previous errors