Skip to content

Commit e1224cd

Browse files
committed
Auto merge of rust-lang#10257 - tylerjw:issue_5283, r=flip1995
needless_lifetimes: lint local macros fixes rust-lang#5283 changelog: [`needless_lifetimes`]: lint local macros
2 parents d020fd7 + 4fde96c commit e1224cd

File tree

4 files changed

+96
-55
lines changed

4 files changed

+96
-55
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: 20 additions & 0 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,4 +499,20 @@ mod pr_9743_output_lifetime_checks {
495499
}
496500
}
497501

502+
mod in_macro {
503+
macro_rules! local_one_input_macro {
504+
() => {
505+
fn one_input<'a>(x: &'a u8) -> &'a u8 {
506+
unimplemented!()
507+
}
508+
};
509+
}
510+
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!();
516+
}
517+
498518
fn main() {}

0 commit comments

Comments
 (0)