Skip to content

Commit 6e6683b

Browse files
committed
Auto merge of rust-lang#13030 - astra-90:master, r=Alexendoo
Fix rust-lang#12964 - false positive with `into_iter_without_iter` changelog: FP: `into_iter_without_iter`: No longer lints when the `iter` or `iter_mut` implementation is not within the first `impl` block fixes rust-lang#12964 --- I'm pretty new to this open-source thing, so hopefully I did everything right. Got a little annoyed this false positive was happening in my code and the issue was inactive for two weeks so I thought I'd fix it myself. As an aside, maybe `iter.map(...).next()` could be linted against? I don't see that ever being preferred over `iter.next().map(...)`, and it could've prevented the bug here.
2 parents c4f81d0 + 0dd8b27 commit 6e6683b

File tree

2 files changed

+46
-13
lines changed

2 files changed

+46
-13
lines changed

clippy_utils/src/ty.rs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,19 +1332,13 @@ pub fn deref_chain<'cx, 'tcx>(cx: &'cx LateContext<'tcx>, ty: Ty<'tcx>) -> impl
13321332
/// If you need this, you should wrap this call in `clippy_utils::ty::deref_chain().any(...)`.
13331333
pub fn get_adt_inherent_method<'a>(cx: &'a LateContext<'_>, ty: Ty<'_>, method_name: Symbol) -> Option<&'a AssocItem> {
13341334
if let Some(ty_did) = ty.ty_adt_def().map(AdtDef::did) {
1335-
cx.tcx
1336-
.inherent_impls(ty_did)
1337-
.into_iter()
1338-
.flatten()
1339-
.map(|&did| {
1340-
cx.tcx
1341-
.associated_items(did)
1342-
.filter_by_name_unhygienic(method_name)
1343-
.next()
1344-
.filter(|item| item.kind == AssocKind::Fn)
1345-
})
1346-
.next()
1347-
.flatten()
1335+
cx.tcx.inherent_impls(ty_did).into_iter().flatten().find_map(|&did| {
1336+
cx.tcx
1337+
.associated_items(did)
1338+
.filter_by_name_unhygienic(method_name)
1339+
.next()
1340+
.filter(|item| item.kind == AssocKind::Fn)
1341+
})
13481342
} else {
13491343
None
13501344
}

tests/ui/into_iter_without_iter.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,3 +185,42 @@ pub mod issue11635 {
185185
}
186186
}
187187
}
188+
189+
pub mod issue12964 {
190+
pub struct MyIter<'a, T: 'a> {
191+
iter: std::slice::Iter<'a, T>,
192+
}
193+
194+
impl<'a, T> Iterator for MyIter<'a, T> {
195+
type Item = &'a T;
196+
197+
fn next(&mut self) -> Option<Self::Item> {
198+
self.iter.next()
199+
}
200+
}
201+
202+
pub struct MyContainer<T> {
203+
inner: Vec<T>,
204+
}
205+
206+
impl<T> MyContainer<T> {}
207+
208+
impl<T> MyContainer<T> {
209+
#[must_use]
210+
pub fn iter(&self) -> MyIter<'_, T> {
211+
<&Self as IntoIterator>::into_iter(self)
212+
}
213+
}
214+
215+
impl<'a, T> IntoIterator for &'a MyContainer<T> {
216+
type Item = &'a T;
217+
218+
type IntoIter = MyIter<'a, T>;
219+
220+
fn into_iter(self) -> Self::IntoIter {
221+
Self::IntoIter {
222+
iter: self.inner.as_slice().iter(),
223+
}
224+
}
225+
}
226+
}

0 commit comments

Comments
 (0)