From 567bea29b14e0365bddbd8b5f0ee9f49c8ea7e72 Mon Sep 17 00:00:00 2001 From: Astra Tsai Date: Mon, 1 Jul 2024 18:57:24 -0700 Subject: [PATCH 1/3] Fix `into_iter_without_iter` false positive when the implementation is not within the first `impl` block --- clippy_utils/src/ty.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/clippy_utils/src/ty.rs b/clippy_utils/src/ty.rs index e5d20564196..b0c842f9850 100644 --- a/clippy_utils/src/ty.rs +++ b/clippy_utils/src/ty.rs @@ -1336,15 +1336,13 @@ pub fn get_adt_inherent_method<'a>(cx: &'a LateContext<'_>, ty: Ty<'_>, method_n .inherent_impls(ty_did) .into_iter() .flatten() - .map(|&did| { + .find_map(|&did| { cx.tcx .associated_items(did) .filter_by_name_unhygienic(method_name) .next() .filter(|item| item.kind == AssocKind::Fn) }) - .next() - .flatten() } else { None } From ba05b764bcb776c0c732d058e0a0d1ac99c5facb Mon Sep 17 00:00:00 2001 From: Astra Tsai Date: Mon, 1 Jul 2024 19:00:10 -0700 Subject: [PATCH 2/3] Add regression test for #12964 --- tests/ui/into_iter_without_iter.rs | 39 ++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/tests/ui/into_iter_without_iter.rs b/tests/ui/into_iter_without_iter.rs index c8b9076041a..109259d6975 100644 --- a/tests/ui/into_iter_without_iter.rs +++ b/tests/ui/into_iter_without_iter.rs @@ -185,3 +185,42 @@ pub mod issue11635 { } } } + +pub mod issue12964 { + pub struct MyIter<'a, T: 'a> { + iter: std::slice::Iter<'a, T>, + } + + impl<'a, T> Iterator for MyIter<'a, T> { + type Item = &'a T; + + fn next(&mut self) -> Option { + self.iter.next() + } + } + + pub struct MyContainer { + inner: Vec, + } + + impl MyContainer {} + + impl MyContainer { + #[must_use] + pub fn iter(&self) -> MyIter<'_, T> { + <&Self as IntoIterator>::into_iter(self) + } + } + + impl<'a, T> IntoIterator for &'a MyContainer { + type Item = &'a T; + + type IntoIter = MyIter<'a, T>; + + fn into_iter(self) -> Self::IntoIter { + Self::IntoIter { + iter: self.inner.as_slice().iter(), + } + } + } +} From 0dd8b27b6b44533357848276e190f684cb36b797 Mon Sep 17 00:00:00 2001 From: Astra Tsai Date: Mon, 1 Jul 2024 19:10:10 -0700 Subject: [PATCH 3/3] Fix formatting --- clippy_utils/src/ty.rs | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/clippy_utils/src/ty.rs b/clippy_utils/src/ty.rs index b0c842f9850..ab9f6433e18 100644 --- a/clippy_utils/src/ty.rs +++ b/clippy_utils/src/ty.rs @@ -1332,17 +1332,13 @@ pub fn deref_chain<'cx, 'tcx>(cx: &'cx LateContext<'tcx>, ty: Ty<'tcx>) -> impl /// If you need this, you should wrap this call in `clippy_utils::ty::deref_chain().any(...)`. pub fn get_adt_inherent_method<'a>(cx: &'a LateContext<'_>, ty: Ty<'_>, method_name: Symbol) -> Option<&'a AssocItem> { if let Some(ty_did) = ty.ty_adt_def().map(AdtDef::did) { - cx.tcx - .inherent_impls(ty_did) - .into_iter() - .flatten() - .find_map(|&did| { - cx.tcx - .associated_items(did) - .filter_by_name_unhygienic(method_name) - .next() - .filter(|item| item.kind == AssocKind::Fn) - }) + cx.tcx.inherent_impls(ty_did).into_iter().flatten().find_map(|&did| { + cx.tcx + .associated_items(did) + .filter_by_name_unhygienic(method_name) + .next() + .filter(|item| item.kind == AssocKind::Fn) + }) } else { None }