Skip to content

Commit 390a13b

Browse files
committed
needless-lifetime - fix nested elision site FPs
1 parent e9440cb commit 390a13b

File tree

4 files changed

+101
-11
lines changed

4 files changed

+101
-11
lines changed

clippy_lints/src/lifetimes.rs

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
22
use rustc_hir::def::{DefKind, Res};
33
use rustc_hir::intravisit::{
4-
walk_fn_decl, walk_generic_param, walk_generics, walk_param_bound, walk_ty, NestedVisitorMap, Visitor,
4+
walk_fn_decl, walk_generic_param, walk_generics, walk_param_bound, walk_trait_ref, walk_ty, NestedVisitorMap,
5+
Visitor,
56
};
67
use rustc_hir::FnRetTy::Return;
78
use rustc_hir::{
89
BodyId, FnDecl, GenericArg, GenericBound, GenericParam, GenericParamKind, Generics, ImplItem, ImplItemKind, Item,
9-
ItemKind, Lifetime, LifetimeName, ParamName, QPath, TraitBoundModifier, TraitFn, TraitItem, TraitItemKind, Ty,
10-
TyKind, WhereClause, WherePredicate,
10+
ItemKind, Lifetime, LifetimeName, ParamName, QPath, TraitBoundModifier, TraitFn, TraitItem, TraitItemKind,
11+
TraitRef, Ty, TyKind, WhereClause, WherePredicate,
1112
};
1213
use rustc_lint::{LateContext, LateLintPass};
1314
use rustc_middle::hir::map::Map;
1415
use rustc_session::{declare_lint_pass, declare_tool_lint};
1516
use rustc_span::source_map::Span;
1617
use rustc_span::symbol::{kw, Symbol};
1718

18-
use crate::utils::{in_macro, last_path_segment, span_lint, trait_ref_of_method};
19+
use crate::utils::paths;
20+
use crate::utils::{get_trait_def_id, in_macro, last_path_segment, span_lint, trait_ref_of_method};
1921

2022
declare_clippy_lint! {
2123
/// **What it does:** Checks for lifetime annotations which can be removed by
@@ -127,6 +129,14 @@ fn check_fn_inner<'tcx>(
127129
return;
128130
}
129131

132+
// fn pointers and closure trait bounds are also lifetime elision sites. This lint does not
133+
// support nested elision sites in a fn item.
134+
if FnPointerOrClosureTraitBoundFinder::find_in_generics(cx, generics)
135+
|| FnPointerOrClosureTraitBoundFinder::find_in_fn_decl(cx, decl)
136+
{
137+
return;
138+
}
139+
130140
let mut bounds_lts = Vec::new();
131141
let types = generics
132142
.params
@@ -523,3 +533,54 @@ impl<'tcx> Visitor<'tcx> for BodyLifetimeChecker {
523533
NestedVisitorMap::None
524534
}
525535
}
536+
537+
const CLOSURE_TRAIT_BOUNDS: [&[&str]; 3] = [&paths::FN, &paths::FN_MUT, &paths::FN_ONCE];
538+
539+
struct FnPointerOrClosureTraitBoundFinder<'a, 'tcx> {
540+
cx: &'a LateContext<'tcx>,
541+
found: bool,
542+
}
543+
544+
impl<'a, 'tcx> FnPointerOrClosureTraitBoundFinder<'a, 'tcx> {
545+
fn find_in_generics(cx: &'a LateContext<'tcx>, generics: &'tcx Generics<'tcx>) -> bool {
546+
let mut finder = Self { cx, found: false };
547+
finder.visit_generics(generics);
548+
finder.found
549+
}
550+
551+
fn find_in_fn_decl(cx: &'a LateContext<'tcx>, generics: &'tcx FnDecl<'tcx>) -> bool {
552+
let mut finder = Self { cx, found: false };
553+
finder.visit_fn_decl(generics);
554+
finder.found
555+
}
556+
}
557+
558+
impl<'a, 'tcx> Visitor<'tcx> for FnPointerOrClosureTraitBoundFinder<'a, 'tcx> {
559+
type Map = Map<'tcx>;
560+
fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
561+
NestedVisitorMap::None
562+
}
563+
564+
fn visit_trait_ref(&mut self, tref: &'tcx TraitRef<'tcx>) {
565+
if CLOSURE_TRAIT_BOUNDS
566+
.iter()
567+
.any(|trait_path| tref.trait_def_id() == get_trait_def_id(self.cx, trait_path))
568+
{
569+
self.found = true;
570+
}
571+
walk_trait_ref(self, tref);
572+
}
573+
574+
fn visit_ty(&mut self, ty: &'tcx Ty<'tcx>) {
575+
match ty.kind {
576+
TyKind::BareFn(..) => self.found = true,
577+
TyKind::OpaqueDef(item_id, _) => {
578+
let map = self.cx.tcx.hir();
579+
let item = map.expect_item(item_id.id);
580+
self.visit_item(item);
581+
},
582+
_ => (),
583+
}
584+
walk_ty(self, ty);
585+
}
586+
}

clippy_lints/src/utils/paths.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ pub const FILE_TYPE: [&str; 3] = ["std", "fs", "FileType"];
4242
pub const FMT_ARGUMENTS_NEW_V1: [&str; 4] = ["core", "fmt", "Arguments", "new_v1"];
4343
pub const FMT_ARGUMENTS_NEW_V1_FORMATTED: [&str; 4] = ["core", "fmt", "Arguments", "new_v1_formatted"];
4444
pub const FMT_ARGUMENTV1_NEW: [&str; 4] = ["core", "fmt", "ArgumentV1", "new"];
45+
pub const FN: [&str; 3] = ["core", "ops", "Fn"];
46+
pub const FN_MUT: [&str; 3] = ["core", "ops", "FnMut"];
47+
pub const FN_ONCE: [&str; 3] = ["core", "ops", "FnOnce"];
4548
pub const FROM_FROM: [&str; 4] = ["core", "convert", "From", "from"];
4649
pub const FROM_TRAIT: [&str; 3] = ["core", "convert", "From"];
4750
pub const FUTURE_FROM_GENERATOR: [&str; 3] = ["core", "future", "from_generator"];

tests/ui/needless_lifetimes.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,4 +259,36 @@ mod issue4291 {
259259
}
260260
}
261261

262+
mod nested_elision_sites {
263+
// Don't lint these cases, they cause FPs.
264+
// The lint does not support nested elision sites.
265+
266+
fn nested_fn_trait_bound<'a>(i: &'a i32) -> impl Fn() -> &'a i32 {
267+
move || i
268+
}
269+
270+
fn nested_fn_mut_trait_bound<'a>(i: &'a i32) -> impl FnMut() -> &'a i32 {
271+
move || i
272+
}
273+
274+
fn nested_fn_once_trait_bound<'a>(i: &'a i32) -> impl FnOnce() -> &'a i32 {
275+
move || i
276+
}
277+
278+
fn nested_generic_fn_trait_bound<'a, T: Fn() -> &'a i32>(f: T) -> &'a i32 {
279+
f()
280+
}
281+
282+
fn nested_where_clause_fn_trait_bound<'a, T>(f: T) -> &'a i32
283+
where
284+
T: Fn() -> &'a i32,
285+
{
286+
f()
287+
}
288+
289+
fn nested_pointer_fn<'a>(_: &'a i32) -> fn(&'a i32) -> &'a i32 {
290+
|i| i
291+
}
292+
}
293+
262294
fn main() {}

tests/ui/needless_lifetimes.stderr

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,6 @@ error: explicit lifetimes given in parameter types where they could be elided (o
3636
LL | fn lifetime_param_2<'a, 'b>(_x: Ref<'a>, _y: &'b u8) {}
3737
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3838

39-
error: explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration)
40-
--> $DIR/needless_lifetimes.rs:86:1
41-
|
42-
LL | fn fn_bound_2<'a, F, I>(_m: Lt<'a, I>, _f: F) -> Lt<'a, I>
43-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
44-
4539
error: explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration)
4640
--> $DIR/needless_lifetimes.rs:120:5
4741
|
@@ -102,5 +96,5 @@ error: explicit lifetimes given in parameter types where they could be elided (o
10296
LL | fn needless_lt<'a>(_x: &'a u8) {}
10397
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
10498

105-
error: aborting due to 17 previous errors
99+
error: aborting due to 16 previous errors
106100

0 commit comments

Comments
 (0)