Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions clippy_lints/src/lifetimes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down
9 changes: 9 additions & 0 deletions tests/ui/auxiliary/macro_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,12 @@ macro_rules! unsafe_macro {
}
};
}

#[macro_export]
macro_rules! needless_lifetime {
() => {
fn needless_lifetime<'a>(x: &'a u8) -> &'a u8 {
unimplemented!()
}
};
}
20 changes: 20 additions & 0 deletions tests/ui/needless_lifetimes.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// aux-build:macro_rules.rs
#![warn(clippy::needless_lifetimes)]
#![allow(
dead_code,
Expand All @@ -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) {}
Expand Down Expand Up @@ -495,4 +499,20 @@ mod pr_9743_output_lifetime_checks {
}
}

mod in_macro {
macro_rules! local_one_input_macro {
() => {
fn one_input<'a>(x: &'a u8) -> &'a u8 {
unimplemented!()
}
};
}

// lint local macro expands to function with needless lifetimes
local_one_input_macro!();

// no lint on external macro
macro_rules::needless_lifetime!();
}

fn main() {}
Loading