Skip to content

Commit b27c5b4

Browse files
authored
Merge pull request #19050 from ChayimFriedman2/iter-self
fix: Don't suggest `into_iter().method()` on iterators
2 parents 8384bc5 + cdc3f89 commit b27c5b4

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

Diff for: crates/hir/src/lib.rs

+11
Original file line numberDiff line numberDiff line change
@@ -4961,6 +4961,17 @@ impl Type {
49614961
self.normalize_trait_assoc_type(db, &[], iterator_item.into())
49624962
}
49634963

4964+
pub fn impls_iterator(self, db: &dyn HirDatabase) -> bool {
4965+
let Some(iterator_trait) =
4966+
db.lang_item(self.env.krate, LangItem::Iterator).and_then(|it| it.as_trait())
4967+
else {
4968+
return false;
4969+
};
4970+
let canonical_ty =
4971+
Canonical { value: self.ty.clone(), binders: CanonicalVarKinds::empty(Interner) };
4972+
method_resolution::implements_trait_unique(&canonical_ty, db, &self.env, iterator_trait)
4973+
}
4974+
49644975
/// Resolves the projection `<Self as IntoIterator>::IntoIter` and returns the resulting type
49654976
pub fn into_iterator_iter(self, db: &dyn HirDatabase) -> Option<Type> {
49664977
let trait_ = db.lang_item(self.env.krate, LangItem::IntoIterIntoIter).and_then(|it| {

Diff for: crates/ide-completion/src/completions/dot.rs

+25-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ pub(crate) fn complete_dot(
8989
acc.add_method(ctx, dot_access, func, None, None)
9090
});
9191

92-
if ctx.config.enable_auto_iter {
92+
if ctx.config.enable_auto_iter && !receiver_ty.strip_references().impls_iterator(ctx.db) {
9393
// FIXME:
9494
// Checking for the existence of `iter()` is complicated in our setup, because we need to substitute
9595
// its return type, so we instead check for `<&Self as IntoIterator>::IntoIter`.
@@ -1505,4 +1505,28 @@ fn main() {
15051505
"#]],
15061506
);
15071507
}
1508+
1509+
#[test]
1510+
fn no_iter_suggestion_on_iterator() {
1511+
check_no_kw(
1512+
r#"
1513+
//- minicore: iterator
1514+
struct MyIter;
1515+
impl Iterator for MyIter {
1516+
type Item = ();
1517+
fn next(&mut self) -> Option<Self::Item> { None }
1518+
}
1519+
1520+
fn main() {
1521+
MyIter.$0
1522+
}
1523+
"#,
1524+
expect![[r#"
1525+
me by_ref() (as Iterator) fn(&mut self) -> &mut Self
1526+
me into_iter() (as IntoIterator) fn(self) -> <Self as IntoIterator>::IntoIter
1527+
me next() (as Iterator) fn(&mut self) -> Option<<Self as Iterator>::Item>
1528+
me nth(…) (as Iterator) fn(&mut self, usize) -> Option<<Self as Iterator>::Item>
1529+
"#]],
1530+
);
1531+
}
15081532
}

0 commit comments

Comments
 (0)