fix: replace_filter_map_next_with_find_map shouldn't work for dyn trait

This commit is contained in:
Young-Flash 2024-02-23 21:03:16 +08:00
parent deddea0d90
commit 4220f90edf

View file

@ -17,6 +17,7 @@ use tracing::debug;
use triomphe::Arc; use triomphe::Arc;
use typed_arena::Arena; use typed_arena::Arena;
use crate::Interner;
use crate::{ use crate::{
db::HirDatabase, db::HirDatabase,
diagnostics::match_check::{ diagnostics::match_check::{
@ -149,17 +150,18 @@ impl ExprValidator {
None => return, None => return,
}; };
if filter_map_next_checker let checker = filter_map_next_checker.get_or_insert_with(|| {
.get_or_insert_with(|| { FilterMapNextChecker::new(&self.owner.resolver(db.upcast()), db)
FilterMapNextChecker::new(&self.owner.resolver(db.upcast()), db) });
})
.check(call_id, receiver, &callee) if checker.check(call_id, receiver, &callee).is_some() {
.is_some()
{
self.diagnostics.push(BodyValidationDiagnostic::ReplaceFilterMapNextWithFindMap { self.diagnostics.push(BodyValidationDiagnostic::ReplaceFilterMapNextWithFindMap {
method_call_expr: call_id, method_call_expr: call_id,
}); });
} }
let receiver_ty = self.infer[receiver.clone()].strip_references().clone();
checker.prev_receiver_ty = Some(receiver_ty);
} }
} }
@ -393,6 +395,7 @@ struct FilterMapNextChecker {
filter_map_function_id: Option<hir_def::FunctionId>, filter_map_function_id: Option<hir_def::FunctionId>,
next_function_id: Option<hir_def::FunctionId>, next_function_id: Option<hir_def::FunctionId>,
prev_filter_map_expr_id: Option<ExprId>, prev_filter_map_expr_id: Option<ExprId>,
prev_receiver_ty: Option<chalk_ir::Ty<Interner>>,
} }
impl FilterMapNextChecker { impl FilterMapNextChecker {
@ -417,7 +420,12 @@ impl FilterMapNextChecker {
), ),
None => (None, None), None => (None, None),
}; };
Self { filter_map_function_id, next_function_id, prev_filter_map_expr_id: None } Self {
filter_map_function_id,
next_function_id,
prev_filter_map_expr_id: None,
prev_receiver_ty: None,
}
} }
// check for instances of .filter_map(..).next() // check for instances of .filter_map(..).next()
@ -434,7 +442,9 @@ impl FilterMapNextChecker {
if *function_id == self.next_function_id? { if *function_id == self.next_function_id? {
if let Some(prev_filter_map_expr_id) = self.prev_filter_map_expr_id { if let Some(prev_filter_map_expr_id) = self.prev_filter_map_expr_id {
if *receiver_expr_id == prev_filter_map_expr_id { let is_dyn_trait =
self.prev_receiver_ty.clone().map_or(false, |it| it.dyn_trait().is_some());
if *receiver_expr_id == prev_filter_map_expr_id && !is_dyn_trait {
return Some(()); return Some(());
} }
} }