Added check for type unification with the iter

This commit is contained in:
schvv31n 2024-05-21 22:21:33 +01:00
parent b31625cdc7
commit 7439ecb07c
3 changed files with 15 additions and 3 deletions

View File

@ -37,14 +37,18 @@ fn is_arg_ty_unified_in_fn<'tcx>(
) -> bool {
let fn_sig = cx.tcx.fn_sig(fn_id).instantiate_identity();
let arg_id_in_args = args.into_iter().position(|e| e.hir_id == arg_id).unwrap();
let arg_ty_in_args = fn_sig.input(arg_id_in_args);
let arg_ty_in_args = fn_sig.input(arg_id_in_args).skip_binder();
cx.tcx.predicates_of(fn_id).predicates.iter().any(|(clause, _)| {
clause
.as_projection_clause()
.and_then(|p| p.map_bound(|p| p.term.ty()).transpose())
.is_some_and(|ty| ty == arg_ty_in_args)
})
.is_some_and(|ty| ty.skip_binder() == arg_ty_in_args)
}) || fn_sig
.inputs()
.iter()
.enumerate()
.any(|(i, ty)| i != arg_id_in_args && ty.skip_binder().walk().any(|arg| arg.as_type() == Some(arg_ty_in_args)))
}
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>, method_name: &str, recv: &'tcx Expr<'tcx>) {

View File

@ -38,6 +38,10 @@ fn array() {
for i in Option::map_or(smth.as_ref(), [].iter(), |s| s.iter()) {
println!("{i}");
}
// Same as above, but when there are no predicates that mention the collection iter type.
let mut iter = [34, 228, 35].iter();
let _ = std::mem::replace(&mut iter, [].iter());
}
macro_rules! in_macros {

View File

@ -38,6 +38,10 @@ fn array() {
for i in Option::map_or(smth.as_ref(), [].iter(), |s| s.iter()) {
println!("{i}");
}
// Same as above, but when there are no predicates that mention the collection iter type.
let mut iter = [34, 228, 35].iter();
let _ = std::mem::replace(&mut iter, [].iter());
}
macro_rules! in_macros {