Make UnordItems::flat_map take UnordItems as args, too

This commit is contained in:
Oli Scherer
2026-05-04 17:56:47 +02:00
parent 1887b9cd74
commit 66a2b8565f
4 changed files with 26 additions and 6 deletions
+16 -3
View File
@@ -34,6 +34,11 @@
pub struct UnordItems<T, I: Iterator<Item = T>>(I);
impl<T, I: Iterator<Item = T>> UnordItems<T, I> {
#[inline]
pub fn new(iter: I) -> UnordItems<T, I> {
UnordItems(iter)
}
#[inline]
pub fn map<U, F: Fn(T) -> U>(self, f: F) -> UnordItems<U, impl Iterator<Item = U>> {
UnordItems(self.0.map(f))
@@ -62,6 +67,14 @@ pub fn filter_map<U, F: Fn(T) -> Option<U>>(
UnordItems(self.0.filter_map(f))
}
#[inline]
pub fn chain(
self,
other: UnordItems<T, impl Iterator<Item = T>>,
) -> UnordItems<T, impl Iterator<Item = T>> {
UnordItems(self.0.chain(other.0))
}
#[inline]
pub fn max(self) -> Option<T>
where
@@ -102,10 +115,10 @@ pub fn count(self) -> usize {
#[inline]
pub fn flat_map<U, F, O>(self, f: F) -> UnordItems<O, impl Iterator<Item = O>>
where
U: IntoIterator<Item = O>,
F: Fn(T) -> U,
U: Iterator<Item = O>,
F: Fn(T) -> UnordItems<O, U>,
{
UnordItems(self.0.flat_map(f))
UnordItems(self.0.flat_map(move |x| f(x).0))
}
pub fn collect<C: From<UnordItems<T, I>>>(self) -> C {
@@ -12,6 +12,7 @@
};
use rustc_ast_pretty::pprust::{path_to_string, where_bound_predicate_to_string};
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet};
use rustc_data_structures::unord::UnordItems;
use rustc_errors::codes::*;
use rustc_errors::{
Applicability, Diag, Diagnostic, ErrorGuaranteed, MultiSpan, SuggestionStyle, pluralize,
@@ -2670,7 +2671,11 @@ fn suggest_alternative_construction_methods(
.extern_crate_map
.items()
// FIXME: This doesn't include impls like `impl Default for String`.
.flat_map(|(_, crate_)| self.r.tcx.implementations_of_trait((*crate_, default_trait)))
.flat_map(|(_, crate_)| {
UnordItems::new(
self.r.tcx.implementations_of_trait((*crate_, default_trait)).into_iter(),
)
})
.filter_map(|(_, simplified_self_ty)| *simplified_self_ty)
.filter_map(|simplified_self_ty| match simplified_self_ty {
SimplifiedType::Adt(did) => Some(did),
@@ -1,6 +1,7 @@
use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
use clippy_utils::res::MaybeDef;
use clippy_utils::{fulfill_or_allowed, get_parent_as_impl, sym};
use rustc_data_structures::unord::UnordItems;
use rustc_hir::def::Res;
use rustc_hir::def_id::{DefId, DefIdSet};
use rustc_hir::{
@@ -130,7 +131,7 @@ fn fill_trait_set(traitt: DefId, set: &mut DefIdSet, cx: &LateContext<'_>) {
fill_trait_set(visited_trait.owner_id.to_def_id(), &mut current_and_super_traits, cx);
let is_empty_method_found = current_and_super_traits
.items()
.flat_map(|&i| cx.tcx.associated_items(i).filter_by_name_unhygienic(sym::is_empty))
.flat_map(|&i| UnordItems::new(cx.tcx.associated_items(i).filter_by_name_unhygienic(sym::is_empty)))
.any(|i| i.is_method() && cx.tcx.fn_sig(i.def_id).skip_binder().inputs().skip_binder().len() == 1);
if !is_empty_method_found {
@@ -6,6 +6,7 @@
use core::ops::ControlFlow;
use rustc_abi::ExternAbi;
use rustc_data_structures::fx::FxHashSet;
use rustc_data_structures::unord::UnordItems;
use rustc_errors::Applicability;
use rustc_hir as hir;
use rustc_hir::attrs::InlineAttr;
@@ -181,7 +182,7 @@ fn check_poly_fn(&self, cx: &LateContext<'_>, def_id: LocalDefId, decl: &FnDecl<
|| typeck
.adjustments()
.items()
.flat_map(|(_, a)| a)
.flat_map(|(_, a)| UnordItems::new(a.iter()))
.any(|a| matches!(a.kind, Adjust::Pointer(PointerCoercion::UnsafeFnPointer))))
{
continue;