Skip to content

Commit

Permalink
needless-lifetime - fix nested elision site FPs
Browse files Browse the repository at this point in the history
  • Loading branch information
tnielens committed Sep 6, 2020
1 parent e9440cb commit 390a13b
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 11 deletions.
69 changes: 65 additions & 4 deletions clippy_lints/src/lifetimes.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_hir::def::{DefKind, Res};
use rustc_hir::intravisit::{
walk_fn_decl, walk_generic_param, walk_generics, walk_param_bound, walk_ty, NestedVisitorMap, Visitor,
walk_fn_decl, walk_generic_param, walk_generics, walk_param_bound, walk_trait_ref, walk_ty, NestedVisitorMap,
Visitor,
};
use rustc_hir::FnRetTy::Return;
use rustc_hir::{
BodyId, FnDecl, GenericArg, GenericBound, GenericParam, GenericParamKind, Generics, ImplItem, ImplItemKind, Item,
ItemKind, Lifetime, LifetimeName, ParamName, QPath, TraitBoundModifier, TraitFn, TraitItem, TraitItemKind, Ty,
TyKind, WhereClause, WherePredicate,
ItemKind, Lifetime, LifetimeName, ParamName, QPath, TraitBoundModifier, TraitFn, TraitItem, TraitItemKind,
TraitRef, Ty, TyKind, WhereClause, WherePredicate,
};
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::hir::map::Map;
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::source_map::Span;
use rustc_span::symbol::{kw, Symbol};

use crate::utils::{in_macro, last_path_segment, span_lint, trait_ref_of_method};
use crate::utils::paths;
use crate::utils::{get_trait_def_id, in_macro, last_path_segment, span_lint, trait_ref_of_method};

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

// fn pointers and closure trait bounds are also lifetime elision sites. This lint does not
// support nested elision sites in a fn item.
if FnPointerOrClosureTraitBoundFinder::find_in_generics(cx, generics)
|| FnPointerOrClosureTraitBoundFinder::find_in_fn_decl(cx, decl)
{
return;
}

let mut bounds_lts = Vec::new();
let types = generics
.params
Expand Down Expand Up @@ -523,3 +533,54 @@ impl<'tcx> Visitor<'tcx> for BodyLifetimeChecker {
NestedVisitorMap::None
}
}

const CLOSURE_TRAIT_BOUNDS: [&[&str]; 3] = [&paths::FN, &paths::FN_MUT, &paths::FN_ONCE];

struct FnPointerOrClosureTraitBoundFinder<'a, 'tcx> {
cx: &'a LateContext<'tcx>,
found: bool,
}

impl<'a, 'tcx> FnPointerOrClosureTraitBoundFinder<'a, 'tcx> {
fn find_in_generics(cx: &'a LateContext<'tcx>, generics: &'tcx Generics<'tcx>) -> bool {
let mut finder = Self { cx, found: false };
finder.visit_generics(generics);
finder.found
}

fn find_in_fn_decl(cx: &'a LateContext<'tcx>, generics: &'tcx FnDecl<'tcx>) -> bool {
let mut finder = Self { cx, found: false };
finder.visit_fn_decl(generics);
finder.found
}
}

impl<'a, 'tcx> Visitor<'tcx> for FnPointerOrClosureTraitBoundFinder<'a, 'tcx> {
type Map = Map<'tcx>;
fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
NestedVisitorMap::None
}

fn visit_trait_ref(&mut self, tref: &'tcx TraitRef<'tcx>) {
if CLOSURE_TRAIT_BOUNDS
.iter()
.any(|trait_path| tref.trait_def_id() == get_trait_def_id(self.cx, trait_path))
{
self.found = true;
}
walk_trait_ref(self, tref);
}

fn visit_ty(&mut self, ty: &'tcx Ty<'tcx>) {
match ty.kind {
TyKind::BareFn(..) => self.found = true,
TyKind::OpaqueDef(item_id, _) => {
let map = self.cx.tcx.hir();
let item = map.expect_item(item_id.id);
self.visit_item(item);
},
_ => (),
}
walk_ty(self, ty);
}
}
3 changes: 3 additions & 0 deletions clippy_lints/src/utils/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ pub const FILE_TYPE: [&str; 3] = ["std", "fs", "FileType"];
pub const FMT_ARGUMENTS_NEW_V1: [&str; 4] = ["core", "fmt", "Arguments", "new_v1"];
pub const FMT_ARGUMENTS_NEW_V1_FORMATTED: [&str; 4] = ["core", "fmt", "Arguments", "new_v1_formatted"];
pub const FMT_ARGUMENTV1_NEW: [&str; 4] = ["core", "fmt", "ArgumentV1", "new"];
pub const FN: [&str; 3] = ["core", "ops", "Fn"];
pub const FN_MUT: [&str; 3] = ["core", "ops", "FnMut"];
pub const FN_ONCE: [&str; 3] = ["core", "ops", "FnOnce"];
pub const FROM_FROM: [&str; 4] = ["core", "convert", "From", "from"];
pub const FROM_TRAIT: [&str; 3] = ["core", "convert", "From"];
pub const FUTURE_FROM_GENERATOR: [&str; 3] = ["core", "future", "from_generator"];
Expand Down
32 changes: 32 additions & 0 deletions tests/ui/needless_lifetimes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,4 +259,36 @@ mod issue4291 {
}
}

mod nested_elision_sites {
// Don't lint these cases, they cause FPs.
// The lint does not support nested elision sites.

fn nested_fn_trait_bound<'a>(i: &'a i32) -> impl Fn() -> &'a i32 {
move || i
}

fn nested_fn_mut_trait_bound<'a>(i: &'a i32) -> impl FnMut() -> &'a i32 {
move || i
}

fn nested_fn_once_trait_bound<'a>(i: &'a i32) -> impl FnOnce() -> &'a i32 {
move || i
}

fn nested_generic_fn_trait_bound<'a, T: Fn() -> &'a i32>(f: T) -> &'a i32 {
f()
}

fn nested_where_clause_fn_trait_bound<'a, T>(f: T) -> &'a i32
where
T: Fn() -> &'a i32,
{
f()
}

fn nested_pointer_fn<'a>(_: &'a i32) -> fn(&'a i32) -> &'a i32 {
|i| i
}
}

fn main() {}
8 changes: 1 addition & 7 deletions tests/ui/needless_lifetimes.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,6 @@ error: explicit lifetimes given in parameter types where they could be elided (o
LL | fn lifetime_param_2<'a, 'b>(_x: Ref<'a>, _y: &'b u8) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration)
--> $DIR/needless_lifetimes.rs:86:1
|
LL | fn fn_bound_2<'a, F, I>(_m: Lt<'a, I>, _f: F) -> Lt<'a, I>
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration)
--> $DIR/needless_lifetimes.rs:120:5
|
Expand Down Expand Up @@ -102,5 +96,5 @@ error: explicit lifetimes given in parameter types where they could be elided (o
LL | fn needless_lt<'a>(_x: &'a u8) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 17 previous errors
error: aborting due to 16 previous errors

0 comments on commit 390a13b

Please sign in to comment.