Skip to content

Commit 65a5ea5

Browse files
committed
Update tests to register the required standard library types
1 parent 7c691f5 commit 65a5ea5

File tree

2 files changed

+48
-16
lines changed

2 files changed

+48
-16
lines changed

crates/hir_ty/src/diagnostics.rs

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -670,21 +670,49 @@ fn foo() { break; }
670670
);
671671
}
672672

673+
// Register the required standard library types to make the tests work
674+
fn add_filter_map_with_find_next_boilerplate(body: &str) -> String {
675+
let prefix = r#"
676+
//- /main.rs crate:main deps:core
677+
use core::iter::Iterator;
678+
use core::option::Option::{self, Some, None};
679+
"#;
680+
let suffix = r#"
681+
//- /core/lib.rs crate:core
682+
pub mod option {
683+
pub enum Option<T> { Some(T), None }
684+
}
685+
pub mod iter {
686+
pub trait Iterator {
687+
type Item;
688+
fn filter_map<B, F>(self, f: F) -> FilterMap where F: FnMut(Self::Item) -> Option<B> { FilterMap }
689+
fn next(&mut self) -> Option<Self::Item>;
690+
}
691+
pub struct FilterMap {}
692+
impl Iterator for FilterMap {
693+
type Item = i32;
694+
fn next(&mut self) -> i32 { 7 }
695+
}
696+
}
697+
"#;
698+
format!("{}{}{}", prefix, body, suffix)
699+
}
700+
673701
#[test]
674-
fn replace_filter_map_next_with_find_map() {
675-
check_diagnostics(
702+
fn replace_filter_map_next_with_find_map2() {
703+
check_diagnostics(&add_filter_map_with_find_next_boilerplate(
676704
r#"
677705
fn foo() {
678706
let m = [1, 2, 3].iter().filter_map(|x| if *x == 2 { Some (4) } else { None }).next();
679707
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ replace filter_map(..).next() with find_map(..)
680708
}
681-
"#,
682-
);
709+
"#,
710+
));
683711
}
684712

685713
#[test]
686714
fn replace_filter_map_next_with_find_map_no_diagnostic_without_next() {
687-
check_diagnostics(
715+
check_diagnostics(&add_filter_map_with_find_next_boilerplate(
688716
r#"
689717
fn foo() {
690718
let m = [1, 2, 3]
@@ -693,12 +721,12 @@ fn foo() { break; }
693721
.len();
694722
}
695723
"#,
696-
);
724+
));
697725
}
698726

699727
#[test]
700728
fn replace_filter_map_next_with_find_map_no_diagnostic_with_intervening_methods() {
701-
check_diagnostics(
729+
check_diagnostics(&add_filter_map_with_find_next_boilerplate(
702730
r#"
703731
fn foo() {
704732
let m = [1, 2, 3]
@@ -708,12 +736,12 @@ fn foo() { break; }
708736
.len();
709737
}
710738
"#,
711-
);
739+
));
712740
}
713741

714742
#[test]
715743
fn replace_filter_map_next_with_find_map_no_diagnostic_if_not_in_chain() {
716-
check_diagnostics(
744+
check_diagnostics(&add_filter_map_with_find_next_boilerplate(
717745
r#"
718746
fn foo() {
719747
let m = [1, 2, 3]
@@ -722,6 +750,6 @@ fn foo() { break; }
722750
let n = m.next();
723751
}
724752
"#,
725-
);
753+
));
726754
}
727755
}

crates/hir_ty/src/diagnostics/expr.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
33
use std::sync::Arc;
44

5-
use hir_def::{AdtId, AssocItemId, DefWithBodyId, expr::Statement, path::path, resolver::HasResolver};
5+
use hir_def::{
6+
expr::Statement, path::path, resolver::HasResolver, AdtId, AssocItemId, DefWithBodyId,
7+
};
68
use hir_expand::{diagnostics::DiagnosticSink, name};
79
use rustc_hash::FxHashSet;
810
use syntax::{ast, AstPtr};
@@ -163,11 +165,13 @@ impl<'a, 'b> ExprValidator<'a, 'b> {
163165
None => return,
164166
};
165167
let iterator_trait_items = &db.trait_data(iterator_trait_id).items;
166-
let filter_map_function_id = match iterator_trait_items.iter().find(|item| item.0 == name![filter_map]) {
167-
Some((_, AssocItemId::FunctionId(id))) => id,
168-
_ => return,
169-
};
170-
let next_function_id = match iterator_trait_items.iter().find(|item| item.0 == name![next]) {
168+
let filter_map_function_id =
169+
match iterator_trait_items.iter().find(|item| item.0 == name![filter_map]) {
170+
Some((_, AssocItemId::FunctionId(id))) => id,
171+
_ => return,
172+
};
173+
let next_function_id = match iterator_trait_items.iter().find(|item| item.0 == name![next])
174+
{
171175
Some((_, AssocItemId::FunctionId(id))) => id,
172176
_ => return,
173177
};

0 commit comments

Comments
 (0)