Skip to content

Commit 4fde96c

Browse files
committed
Test needless_lifetimes within external macro
Signed-off-by: Tyler Weaver <[email protected]>
1 parent c959813 commit 4fde96c

File tree

4 files changed

+89
-61
lines changed

4 files changed

+89
-61
lines changed

clippy_lints/src/lifetimes.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ use rustc_hir::{
1313
ImplItemKind, Item, ItemKind, Lifetime, LifetimeName, LifetimeParamKind, PolyTraitRef, PredicateOrigin, TraitFn,
1414
TraitItem, TraitItemKind, Ty, TyKind, WherePredicate,
1515
};
16-
use rustc_lint::{LateContext, LateLintPass};
16+
use rustc_lint::{LateContext, LateLintPass, LintContext};
1717
use rustc_middle::hir::nested_filter as middle_nested_filter;
18+
use rustc_middle::lint::in_external_macro;
1819
use rustc_middle::ty::TyCtxt;
1920
use rustc_session::{declare_lint_pass, declare_tool_lint};
2021
use rustc_span::def_id::LocalDefId;
@@ -144,7 +145,7 @@ fn check_fn_inner<'tcx>(
144145
span: Span,
145146
report_extra_lifetimes: bool,
146147
) {
147-
if span.from_expansion() || has_where_lifetimes(cx, generics) {
148+
if in_external_macro(cx.sess(), span) || has_where_lifetimes(cx, generics) {
148149
return;
149150
}
150151

tests/ui/auxiliary/macro_rules.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,12 @@ macro_rules! unsafe_macro {
159159
}
160160
};
161161
}
162+
163+
#[macro_export]
164+
macro_rules! needless_lifetime {
165+
() => {
166+
fn needless_lifetime<'a>(x: &'a u8) -> &'a u8 {
167+
unimplemented!()
168+
}
169+
};
170+
}

tests/ui/needless_lifetimes.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// aux-build:macro_rules.rs
12
#![warn(clippy::needless_lifetimes)]
23
#![allow(
34
dead_code,
@@ -8,6 +9,9 @@
89
clippy::get_first
910
)]
1011

12+
#[macro_use]
13+
extern crate macro_rules;
14+
1115
fn distinct_lifetimes<'a, 'b>(_x: &'a u8, _y: &'b u8, _z: u8) {}
1216

1317
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 {
495499
}
496500
}
497501

498-
mod skip_inside_macros {
499-
macro_rules! print_with_one_input {
500-
($a:expr) => {
501-
fn print_with_one_input<'a>(x: &'a u8) -> &'a u8 {
502-
println!("{}", $a);
502+
mod in_macro {
503+
macro_rules! local_one_input_macro {
504+
() => {
505+
fn one_input<'a>(x: &'a u8) -> &'a u8 {
503506
unimplemented!()
504507
}
505508
};
506509
}
507510

508-
print_with_one_input!("this is a dandy little string literal");
511+
// lint local macro expands to function with needless lifetimes
512+
local_one_input_macro!();
513+
514+
// no lint on external macro
515+
macro_rules::needless_lifetime!();
509516
}
510517

511518
fn main() {}

0 commit comments

Comments
 (0)