From 51b527266e9270ced4c6f1c1bd0735ce49b6ed88 Mon Sep 17 00:00:00 2001 From: Samuel Moelius Date: Wed, 25 Sep 2024 14:28:19 -0400 Subject: [PATCH] Fix bug found during review https://github.com/rust-lang/rust-clippy/pull/13286#issuecomment-2374245772 --- clippy_lints/src/lifetimes.rs | 28 ++++++++++++++-------------- tests/ui/needless_lifetimes.fixed | 19 +++++++++++++++++++ tests/ui/needless_lifetimes.rs | 19 +++++++++++++++++++ 3 files changed, 52 insertions(+), 14 deletions(-) diff --git a/clippy_lints/src/lifetimes.rs b/clippy_lints/src/lifetimes.rs index 5a3c749ca..a4555e003 100644 --- a/clippy_lints/src/lifetimes.rs +++ b/clippy_lints/src/lifetimes.rs @@ -6,12 +6,12 @@ use rustc_errors::Applicability; use rustc_hir::FnRetTy::Return; use rustc_hir::intravisit::nested_filter::{self as hir_nested_filter, NestedFilter}; use rustc_hir::intravisit::{ - Visitor, walk_fn_decl, walk_generic_arg, walk_generics, walk_impl_item_ref, walk_item, walk_param_bound, + Visitor, walk_fn_decl, walk_generic_args, walk_generics, walk_impl_item_ref, walk_item, walk_param_bound, walk_poly_trait_ref, walk_trait_ref, walk_ty, walk_where_predicate, }; use rustc_hir::{ - BareFnTy, BodyId, FnDecl, FnSig, GenericArg, GenericBound, GenericParam, GenericParamKind, Generics, Impl, - ImplItem, ImplItemKind, Item, ItemKind, Lifetime, LifetimeName, LifetimeParamKind, Node, PolyTraitRef, + BareFnTy, BodyId, FnDecl, FnSig, GenericArg, GenericArgs, GenericBound, GenericParam, GenericParamKind, Generics, + Impl, ImplItem, ImplItemKind, Item, ItemKind, Lifetime, LifetimeName, LifetimeParamKind, Node, PolyTraitRef, PredicateOrigin, TraitFn, TraitItem, TraitItemKind, Ty, TyKind, WherePredicate, lang_items, }; use rustc_lint::{LateContext, LateLintPass, LintContext}; @@ -494,14 +494,14 @@ fn has_where_lifetimes<'tcx>(cx: &LateContext<'tcx>, generics: &'tcx Generics<'_ struct Usage { lifetime: Lifetime, in_where_predicate: bool, - in_generic_arg: bool, + in_generics_arg: bool, } struct LifetimeChecker<'cx, 'tcx, F> { cx: &'cx LateContext<'tcx>, map: FxHashMap>, where_predicate_depth: usize, - generic_arg_depth: usize, + generic_args_depth: usize, phantom: std::marker::PhantomData, } @@ -512,7 +512,7 @@ impl<'cx, 'tcx, F> LifetimeChecker<'cx, 'tcx, F> { cx, map, where_predicate_depth: 0, - generic_arg_depth: 0, + generic_args_depth: 0, phantom: std::marker::PhantomData, } } @@ -533,7 +533,7 @@ where usages.push(Usage { lifetime: *lifetime, in_where_predicate: self.where_predicate_depth != 0, - in_generic_arg: self.generic_arg_depth != 0, + in_generics_arg: self.generic_args_depth != 0, }); } } @@ -544,10 +544,10 @@ where self.where_predicate_depth -= 1; } - fn visit_generic_arg(&mut self, generic_arg: &'tcx GenericArg<'tcx>) -> Self::Result { - self.generic_arg_depth += 1; - walk_generic_arg(self, generic_arg); - self.generic_arg_depth -= 1; + fn visit_generic_args(&mut self, generic_args: &'tcx GenericArgs<'tcx>) -> Self::Result { + self.generic_args_depth += 1; + walk_generic_args(self, generic_args); + self.generic_args_depth -= 1; } fn nested_visit_map(&mut self) -> Self::Map { @@ -574,7 +574,7 @@ fn report_extra_lifetimes<'tcx>(cx: &LateContext<'tcx>, func: &'tcx FnDecl<'_>, for (def_id, usages) in checker.map { if usages .iter() - .all(|usage| usage.in_where_predicate && !usage.in_generic_arg) + .all(|usage| usage.in_where_predicate && !usage.in_generics_arg) { span_lint( cx, @@ -612,7 +612,7 @@ fn report_extra_impl_lifetimes<'tcx>(cx: &LateContext<'tcx>, impl_: &'tcx Impl<' for (&def_id, usages) in &checker.map { if usages .iter() - .all(|usage| usage.in_where_predicate && !usage.in_generic_arg) + .all(|usage| usage.in_where_predicate && !usage.in_generics_arg) { span_lint( cx, @@ -646,7 +646,7 @@ fn report_elidable_impl_lifetimes<'tcx>( } | Usage { lifetime, - in_generic_arg: false, + in_generics_arg: false, .. }, ] = usages.as_slice() diff --git a/tests/ui/needless_lifetimes.fixed b/tests/ui/needless_lifetimes.fixed index 7ded5b423..cfa4cf9da 100644 --- a/tests/ui/needless_lifetimes.fixed +++ b/tests/ui/needless_lifetimes.fixed @@ -543,4 +543,23 @@ mod issue5787 { } } +// https://github.com/rust-lang/rust-clippy/pull/13286#issuecomment-2374245772 +mod rayon { + trait ParallelIterator { + type Item; + } + + struct Copied { + base: I, + } + + impl<'a, T, I> ParallelIterator for Copied + where + I: ParallelIterator, + T: 'a + Copy + Send + Sync, + { + type Item = T; + } +} + fn main() {} diff --git a/tests/ui/needless_lifetimes.rs b/tests/ui/needless_lifetimes.rs index 8b3a885f4..5e9d51164 100644 --- a/tests/ui/needless_lifetimes.rs +++ b/tests/ui/needless_lifetimes.rs @@ -543,4 +543,23 @@ mod issue5787 { } } +// https://github.com/rust-lang/rust-clippy/pull/13286#issuecomment-2374245772 +mod rayon { + trait ParallelIterator { + type Item; + } + + struct Copied { + base: I, + } + + impl<'a, T, I> ParallelIterator for Copied + where + I: ParallelIterator, + T: 'a + Copy + Send + Sync, + { + type Item = T; + } +} + fn main() {}