Skip to content

Commit 7690fe3

Browse files
Simplify some iterator combinators
1 parent c757267 commit 7690fe3

File tree

12 files changed

+91
-135
lines changed

12 files changed

+91
-135
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -527,26 +527,21 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
527527
// that are *partially* initialized by assigning to a field of an uninitialized
528528
// binding. We differentiate between them for more accurate wording here.
529529
"isn't fully initialized"
530-
} else if spans
531-
.iter()
532-
.filter(|i| {
533-
// We filter these to avoid misleading wording in cases like the following,
534-
// where `x` has an `init`, but it is in the same place we're looking at:
535-
// ```
536-
// let x;
537-
// x += 1;
538-
// ```
539-
!i.contains(span)
530+
} else if !spans.iter().any(|i| {
531+
// We filter these to avoid misleading wording in cases like the following,
532+
// where `x` has an `init`, but it is in the same place we're looking at:
533+
// ```
534+
// let x;
535+
// x += 1;
536+
// ```
537+
!i.contains(span)
540538
// We filter these to avoid incorrect main message on `match-cfg-fake-edges.rs`
541539
&& !visitor
542540
.errors
543541
.iter()
544542
.map(|(sp, _)| *sp)
545543
.any(|sp| span < sp && !sp.contains(span))
546-
})
547-
.count()
548-
== 0
549-
{
544+
}) {
550545
show_assign_sugg = true;
551546
"isn't initialized"
552547
} else {

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -316,35 +316,29 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
316316
// obligation comes from the `impl`. Find that `impl` so that we can point
317317
// at it in the suggestion.
318318
let trait_did = trait_did.to_def_id();
319-
match tcx
320-
.hir()
321-
.trait_impls(trait_did)
322-
.iter()
323-
.filter_map(|&impl_did| {
324-
match tcx.hir().get_if_local(impl_did.to_def_id()) {
325-
Some(Node::Item(Item {
326-
kind: ItemKind::Impl(hir::Impl { self_ty, .. }),
327-
..
328-
})) if trait_objects.iter().all(|did| {
329-
// FIXME: we should check `self_ty` against the receiver
330-
// type in the `UnifyReceiver` context, but for now, use
331-
// this imperfect proxy. This will fail if there are
332-
// multiple `impl`s for the same trait like
333-
// `impl Foo for Box<dyn Bar>` and `impl Foo for dyn Bar`.
334-
// In that case, only the first one will get suggestions.
335-
let mut traits = vec![];
336-
let mut hir_v = HirTraitObjectVisitor(&mut traits, *did);
337-
hir_v.visit_ty(self_ty);
338-
!traits.is_empty()
339-
}) =>
340-
{
341-
Some(self_ty)
342-
}
343-
_ => None,
319+
match tcx.hir().trait_impls(trait_did).iter().find_map(|&impl_did| {
320+
match tcx.hir().get_if_local(impl_did.to_def_id()) {
321+
Some(Node::Item(Item {
322+
kind: ItemKind::Impl(hir::Impl { self_ty, .. }),
323+
..
324+
})) if trait_objects.iter().all(|did| {
325+
// FIXME: we should check `self_ty` against the receiver
326+
// type in the `UnifyReceiver` context, but for now, use
327+
// this imperfect proxy. This will fail if there are
328+
// multiple `impl`s for the same trait like
329+
// `impl Foo for Box<dyn Bar>` and `impl Foo for dyn Bar`.
330+
// In that case, only the first one will get suggestions.
331+
let mut traits = vec![];
332+
let mut hir_v = HirTraitObjectVisitor(&mut traits, *did);
333+
hir_v.visit_ty(self_ty);
334+
!traits.is_empty()
335+
}) =>
336+
{
337+
Some(self_ty)
344338
}
345-
})
346-
.next()
347-
{
339+
_ => None,
340+
}
341+
}) {
348342
Some(self_ty) => Some((trait_item.ident, self_ty)),
349343
_ => None,
350344
}

compiler/rustc_codegen_gcc/src/builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1244,7 +1244,7 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
12441244
) -> RValue<'gcc> {
12451245
// FIXME(antoyo): remove when having a proper API.
12461246
let gcc_func = unsafe { std::mem::transmute(func) };
1247-
let call = if self.functions.borrow().values().find(|value| **value == gcc_func).is_some() {
1247+
let call = if self.functions.borrow().values().any(|value| *value == gcc_func) {
12481248
self.function_call(func, args, funclet)
12491249
}
12501250
else {

compiler/rustc_codegen_gcc/src/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
253253

254254
pub fn rvalue_as_function(&self, value: RValue<'gcc>) -> Function<'gcc> {
255255
let function: Function<'gcc> = unsafe { std::mem::transmute(value) };
256-
debug_assert!(self.functions.borrow().values().find(|value| **value == function).is_some(),
256+
debug_assert!(self.functions.borrow().values().any(|value| *value == function),
257257
"{:?} ({:?}) is not a function", value, value.get_type());
258258
function
259259
}

compiler/rustc_hir_typeck/src/lib.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -240,10 +240,8 @@ fn typeck_with_fallback<'tcx>(
240240
}),
241241
Node::Expr(&hir::Expr { kind: hir::ExprKind::InlineAsm(asm), .. })
242242
| Node::Item(&hir::Item { kind: hir::ItemKind::GlobalAsm(asm), .. }) => {
243-
let operand_ty = asm
244-
.operands
245-
.iter()
246-
.filter_map(|(op, _op_sp)| match op {
243+
let operand_ty =
244+
asm.operands.iter().find_map(|(op, _op_sp)| match op {
247245
hir::InlineAsmOperand::Const { anon_const }
248246
if anon_const.hir_id == id =>
249247
{
@@ -259,8 +257,7 @@ fn typeck_with_fallback<'tcx>(
259257
}))
260258
}
261259
_ => None,
262-
})
263-
.next();
260+
});
264261
operand_ty.unwrap_or_else(fallback)
265262
}
266263
_ => fallback(),

compiler/rustc_hir_typeck/src/method/prelude2021.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,8 +341,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
341341
// Find an identifier with which this trait was imported (note that `_` doesn't count).
342342
let any_id = import_items
343343
.iter()
344-
.filter_map(|item| if item.ident.name != Underscore { Some(item.ident) } else { None })
345-
.next();
344+
.find_map(|item| if item.ident.name != Underscore { Some(item.ident) } else { None });
346345
if let Some(any_id) = any_id {
347346
if any_id.name == Empty {
348347
// Glob import, so just use its name.

compiler/rustc_hir_typeck/src/method/probe.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,7 +1111,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
11111111
// a raw pointer
11121112
!step.self_ty.references_error() && !step.from_unsafe_deref
11131113
})
1114-
.flat_map(|step| {
1114+
.find_map(|step| {
11151115
let InferOk { value: self_ty, obligations: _ } = self
11161116
.fcx
11171117
.probe_instantiate_query_response(
@@ -1147,7 +1147,6 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
11471147
})
11481148
})
11491149
})
1150-
.next()
11511150
}
11521151

11531152
/// For each type `T` in the step list, this attempts to find a method where

compiler/rustc_hir_typeck/src/method/suggest.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,9 +257,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
257257
self.tcx
258258
.inherent_impls(adt_def.did())
259259
.iter()
260-
.filter_map(|def_id| self.associated_value(*def_id, item_name))
261-
.count()
262-
>= 1
260+
.any(|def_id| self.associated_value(*def_id, item_name).is_some())
263261
} else {
264262
false
265263
}

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/static_impl_trait.rs

Lines changed: 36 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -309,19 +309,12 @@ pub fn suggest_new_region_bound(
309309
let did = item_id.owner_id.to_def_id();
310310
let ty = tcx.mk_opaque(did, ty::InternalSubsts::identity_for_item(tcx, did));
311311

312-
if let Some(span) = opaque
313-
.bounds
314-
.iter()
315-
.filter_map(|arg| match arg {
316-
GenericBound::Outlives(Lifetime {
317-
res: LifetimeName::Static,
318-
ident,
319-
..
320-
}) => Some(ident.span),
321-
_ => None,
322-
})
323-
.next()
324-
{
312+
if let Some(span) = opaque.bounds.iter().find_map(|arg| match arg {
313+
GenericBound::Outlives(Lifetime {
314+
res: LifetimeName::Static, ident, ..
315+
}) => Some(ident.span),
316+
_ => None,
317+
}) {
325318
if let Some(explicit_static) = &explicit_static {
326319
err.span_suggestion_verbose(
327320
span,
@@ -338,20 +331,14 @@ pub fn suggest_new_region_bound(
338331
Applicability::MaybeIncorrect,
339332
);
340333
}
341-
} else if opaque
342-
.bounds
343-
.iter()
344-
.filter_map(|arg| match arg {
345-
GenericBound::Outlives(Lifetime { ident, .. })
346-
if ident.name.to_string() == lifetime_name =>
347-
{
348-
Some(ident.span)
349-
}
350-
_ => None,
351-
})
352-
.next()
353-
.is_some()
354-
{
334+
} else if opaque.bounds.iter().any(|arg| match arg {
335+
GenericBound::Outlives(Lifetime { ident, .. })
336+
if ident.name.to_string() == lifetime_name =>
337+
{
338+
true
339+
}
340+
_ => false,
341+
}) {
355342
} else {
356343
err.span_suggestion_verbose(
357344
fn_return.span.shrink_to_hi(),
@@ -428,35 +415,29 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
428415
// obligation comes from the `impl`. Find that `impl` so that we can point
429416
// at it in the suggestion.
430417
let trait_did = trait_did.to_def_id();
431-
match tcx
432-
.hir()
433-
.trait_impls(trait_did)
434-
.iter()
435-
.filter_map(|&impl_did| {
436-
match tcx.hir().get_if_local(impl_did.to_def_id()) {
437-
Some(Node::Item(Item {
438-
kind: ItemKind::Impl(hir::Impl { self_ty, .. }),
439-
..
440-
})) if trait_objects.iter().all(|did| {
441-
// FIXME: we should check `self_ty` against the receiver
442-
// type in the `UnifyReceiver` context, but for now, use
443-
// this imperfect proxy. This will fail if there are
444-
// multiple `impl`s for the same trait like
445-
// `impl Foo for Box<dyn Bar>` and `impl Foo for dyn Bar`.
446-
// In that case, only the first one will get suggestions.
447-
let mut traits = vec![];
448-
let mut hir_v = HirTraitObjectVisitor(&mut traits, *did);
449-
hir_v.visit_ty(self_ty);
450-
!traits.is_empty()
451-
}) =>
452-
{
453-
Some(self_ty)
454-
}
455-
_ => None,
418+
match tcx.hir().trait_impls(trait_did).iter().find_map(|&impl_did| {
419+
match tcx.hir().get_if_local(impl_did.to_def_id()) {
420+
Some(Node::Item(Item {
421+
kind: ItemKind::Impl(hir::Impl { self_ty, .. }),
422+
..
423+
})) if trait_objects.iter().all(|did| {
424+
// FIXME: we should check `self_ty` against the receiver
425+
// type in the `UnifyReceiver` context, but for now, use
426+
// this imperfect proxy. This will fail if there are
427+
// multiple `impl`s for the same trait like
428+
// `impl Foo for Box<dyn Bar>` and `impl Foo for dyn Bar`.
429+
// In that case, only the first one will get suggestions.
430+
let mut traits = vec![];
431+
let mut hir_v = HirTraitObjectVisitor(&mut traits, *did);
432+
hir_v.visit_ty(self_ty);
433+
!traits.is_empty()
434+
}) =>
435+
{
436+
Some(self_ty)
456437
}
457-
})
458-
.next()
459-
{
438+
_ => None,
439+
}
440+
}) {
460441
Some(self_ty) => Some((trait_item.ident, self_ty)),
461442
_ => None,
462443
}

compiler/rustc_lint/src/unused.rs

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults {
256256
cx.tcx,
257257
cx.tcx.explicit_item_bounds(def).iter().cloned(),
258258
)
259-
.filter_map(|obligation| {
259+
.find_map(|obligation| {
260260
// We only look at the `DefId`, so it is safe to skip the binder here.
261261
if let ty::PredicateKind::Clause(ty::Clause::Trait(
262262
ref poly_trait_predicate,
@@ -270,22 +270,17 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults {
270270
}
271271
})
272272
.map(|inner| MustUsePath::Opaque(Box::new(inner)))
273-
.next()
274273
}
275-
ty::Dynamic(binders, _, _) => binders
276-
.iter()
277-
.filter_map(|predicate| {
278-
if let ty::ExistentialPredicate::Trait(ref trait_ref) =
279-
predicate.skip_binder()
280-
{
281-
let def_id = trait_ref.def_id;
282-
is_def_must_use(cx, def_id, span)
283-
} else {
284-
None
285-
}
286-
.map(|inner| MustUsePath::TraitObject(Box::new(inner)))
287-
})
288-
.next(),
274+
ty::Dynamic(binders, _, _) => binders.iter().find_map(|predicate| {
275+
if let ty::ExistentialPredicate::Trait(ref trait_ref) = predicate.skip_binder()
276+
{
277+
let def_id = trait_ref.def_id;
278+
is_def_must_use(cx, def_id, span)
279+
} else {
280+
None
281+
}
282+
.map(|inner| MustUsePath::TraitObject(Box::new(inner)))
283+
}),
289284
ty::Tuple(tys) => {
290285
let elem_exprs = if let hir::ExprKind::Tup(elem_exprs) = expr.kind {
291286
debug_assert_eq!(elem_exprs.len(), tys.len());

compiler/rustc_macros/src/symbols/tests.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,13 @@ fn test_symbols() {
1616
let m: &syn::ItemMacro = file
1717
.items
1818
.iter()
19-
.filter_map(|i| {
19+
.find_map(|i| {
2020
if let syn::Item::Macro(m) = i {
2121
if m.mac.path == symbols_path { Some(m) } else { None }
2222
} else {
2323
None
2424
}
2525
})
26-
.next()
2726
.expect("did not find `symbols!` macro invocation.");
2827

2928
let body_tokens = m.mac.tokens.clone();

compiler/rustc_resolve/src/ident.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -820,13 +820,12 @@ impl<'a> Resolver<'a> {
820820
// binding if it exists. What we really want here is having two separate scopes in
821821
// a module - one for non-globs and one for globs, but until that's done use this
822822
// hack to avoid inconsistent resolution ICEs during import validation.
823-
let binding = [resolution.binding, resolution.shadowed_glob]
824-
.into_iter()
825-
.filter_map(|binding| match (binding, ignore_binding) {
823+
let binding = [resolution.binding, resolution.shadowed_glob].into_iter().find_map(
824+
|binding| match (binding, ignore_binding) {
826825
(Some(binding), Some(ignored)) if ptr::eq(binding, ignored) => None,
827826
_ => binding,
828-
})
829-
.next();
827+
},
828+
);
830829
let Some(binding) = binding else {
831830
return Err((Determined, Weak::No));
832831
};

0 commit comments

Comments
 (0)