Update tests to register the required standard library types

This commit is contained in:
Phil Ellison 2021-01-01 21:40:11 +00:00
parent 7c691f51f9
commit 65a5ea581d
2 changed files with 48 additions and 16 deletions

View File

@ -670,21 +670,49 @@ fn foo() { break; }
); );
} }
// Register the required standard library types to make the tests work
fn add_filter_map_with_find_next_boilerplate(body: &str) -> String {
let prefix = r#"
//- /main.rs crate:main deps:core
use core::iter::Iterator;
use core::option::Option::{self, Some, None};
"#;
let suffix = r#"
//- /core/lib.rs crate:core
pub mod option {
pub enum Option<T> { Some(T), None }
}
pub mod iter {
pub trait Iterator {
type Item;
fn filter_map<B, F>(self, f: F) -> FilterMap where F: FnMut(Self::Item) -> Option<B> { FilterMap }
fn next(&mut self) -> Option<Self::Item>;
}
pub struct FilterMap {}
impl Iterator for FilterMap {
type Item = i32;
fn next(&mut self) -> i32 { 7 }
}
}
"#;
format!("{}{}{}", prefix, body, suffix)
}
#[test] #[test]
fn replace_filter_map_next_with_find_map() { fn replace_filter_map_next_with_find_map2() {
check_diagnostics( check_diagnostics(&add_filter_map_with_find_next_boilerplate(
r#" r#"
fn foo() { fn foo() {
let m = [1, 2, 3].iter().filter_map(|x| if *x == 2 { Some (4) } else { None }).next(); let m = [1, 2, 3].iter().filter_map(|x| if *x == 2 { Some (4) } else { None }).next();
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ replace filter_map(..).next() with find_map(..) //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ replace filter_map(..).next() with find_map(..)
} }
"#, "#,
); ));
} }
#[test] #[test]
fn replace_filter_map_next_with_find_map_no_diagnostic_without_next() { fn replace_filter_map_next_with_find_map_no_diagnostic_without_next() {
check_diagnostics( check_diagnostics(&add_filter_map_with_find_next_boilerplate(
r#" r#"
fn foo() { fn foo() {
let m = [1, 2, 3] let m = [1, 2, 3]
@ -693,12 +721,12 @@ fn foo() { break; }
.len(); .len();
} }
"#, "#,
); ));
} }
#[test] #[test]
fn replace_filter_map_next_with_find_map_no_diagnostic_with_intervening_methods() { fn replace_filter_map_next_with_find_map_no_diagnostic_with_intervening_methods() {
check_diagnostics( check_diagnostics(&add_filter_map_with_find_next_boilerplate(
r#" r#"
fn foo() { fn foo() {
let m = [1, 2, 3] let m = [1, 2, 3]
@ -708,12 +736,12 @@ fn foo() { break; }
.len(); .len();
} }
"#, "#,
); ));
} }
#[test] #[test]
fn replace_filter_map_next_with_find_map_no_diagnostic_if_not_in_chain() { fn replace_filter_map_next_with_find_map_no_diagnostic_if_not_in_chain() {
check_diagnostics( check_diagnostics(&add_filter_map_with_find_next_boilerplate(
r#" r#"
fn foo() { fn foo() {
let m = [1, 2, 3] let m = [1, 2, 3]
@ -722,6 +750,6 @@ fn foo() { break; }
let n = m.next(); let n = m.next();
} }
"#, "#,
); ));
} }
} }

View File

@ -2,7 +2,9 @@
use std::sync::Arc; use std::sync::Arc;
use hir_def::{AdtId, AssocItemId, DefWithBodyId, expr::Statement, path::path, resolver::HasResolver}; use hir_def::{
expr::Statement, path::path, resolver::HasResolver, AdtId, AssocItemId, DefWithBodyId,
};
use hir_expand::{diagnostics::DiagnosticSink, name}; use hir_expand::{diagnostics::DiagnosticSink, name};
use rustc_hash::FxHashSet; use rustc_hash::FxHashSet;
use syntax::{ast, AstPtr}; use syntax::{ast, AstPtr};
@ -163,11 +165,13 @@ impl<'a, 'b> ExprValidator<'a, 'b> {
None => return, None => return,
}; };
let iterator_trait_items = &db.trait_data(iterator_trait_id).items; let iterator_trait_items = &db.trait_data(iterator_trait_id).items;
let filter_map_function_id = match iterator_trait_items.iter().find(|item| item.0 == name![filter_map]) { let filter_map_function_id =
match iterator_trait_items.iter().find(|item| item.0 == name![filter_map]) {
Some((_, AssocItemId::FunctionId(id))) => id, Some((_, AssocItemId::FunctionId(id))) => id,
_ => return, _ => return,
}; };
let next_function_id = match iterator_trait_items.iter().find(|item| item.0 == name![next]) { let next_function_id = match iterator_trait_items.iter().find(|item| item.0 == name![next])
{
Some((_, AssocItemId::FunctionId(id))) => id, Some((_, AssocItemId::FunctionId(id))) => id,
_ => return, _ => return,
}; };