Skip to content

Commit 9709785

Browse files
authored
Rollup merge of rust-lang#71544 - cuviper:filter_map_next, r=Mark-Simulacrum
Replace filter_map().next() calls with find_map() These are semantically the same, but `find_map()` is more concise.
2 parents 939c932 + 4282776 commit 9709785

File tree

21 files changed

+99
-151
lines changed

21 files changed

+99
-151
lines changed

src/librustc_codegen_ssa/back/linker.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -1127,11 +1127,7 @@ fn exported_symbols(tcx: TyCtxt<'_>, crate_type: CrateType) -> Vec<String> {
11271127
}
11281128

11291129
let formats = tcx.dependency_formats(LOCAL_CRATE);
1130-
let deps = formats
1131-
.iter()
1132-
.filter_map(|(t, list)| if *t == crate_type { Some(list) } else { None })
1133-
.next()
1134-
.unwrap();
1130+
let deps = formats.iter().find_map(|(t, list)| (*t == crate_type).then_some(list)).unwrap();
11351131

11361132
for (index, dep_format) in deps.iter().enumerate() {
11371133
let cnum = CrateNum::new(index + 1);

src/librustc_errors/emitter.rs

+10-13
Original file line numberDiff line numberDiff line change
@@ -285,21 +285,18 @@ pub trait Emitter {
285285
let has_macro_spans = iter::once(&*span)
286286
.chain(children.iter().map(|child| &child.span))
287287
.flat_map(|span| span.primary_spans())
288-
.copied()
289-
.flat_map(|sp| {
290-
sp.macro_backtrace().filter_map(|expn_data| {
291-
match expn_data.kind {
292-
ExpnKind::Root => None,
288+
.flat_map(|sp| sp.macro_backtrace())
289+
.find_map(|expn_data| {
290+
match expn_data.kind {
291+
ExpnKind::Root => None,
293292

294-
// Skip past non-macro entries, just in case there
295-
// are some which do actually involve macros.
296-
ExpnKind::Desugaring(..) | ExpnKind::AstPass(..) => None,
293+
// Skip past non-macro entries, just in case there
294+
// are some which do actually involve macros.
295+
ExpnKind::Desugaring(..) | ExpnKind::AstPass(..) => None,
297296

298-
ExpnKind::Macro(macro_kind, _) => Some(macro_kind),
299-
}
300-
})
301-
})
302-
.next();
297+
ExpnKind::Macro(macro_kind, _) => Some(macro_kind),
298+
}
299+
});
303300

304301
if !backtrace {
305302
self.fix_multispans_in_extern_macros(source_map, span, children);

src/librustc_infer/infer/error_reporting/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1632,8 +1632,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
16321632
];
16331633
if let Some(msg) = have_as_ref
16341634
.iter()
1635-
.filter_map(|(path, msg)| if &path_str == path { Some(msg) } else { None })
1636-
.next()
1635+
.find_map(|(path, msg)| (&path_str == path).then_some(msg))
16371636
{
16381637
let mut show_suggestion = true;
16391638
for (exp_ty, found_ty) in exp_substs.types().zip(found_substs.types()) {

src/librustc_infer/infer/error_reporting/nice_region_error/find_anon_type.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
4747
return fndecl
4848
.inputs
4949
.iter()
50-
.filter_map(|arg| self.find_component_for_bound_region(arg, br))
51-
.next()
50+
.find_map(|arg| self.find_component_for_bound_region(arg, br))
5251
.map(|ty| (ty, &**fndecl));
5352
}
5453
}

src/librustc_infer/infer/error_reporting/nice_region_error/util.rs

+25-29
Original file line numberDiff line numberDiff line change
@@ -58,37 +58,33 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
5858
let fn_decl = hir.fn_decl_by_hir_id(owner_id).unwrap();
5959
let poly_fn_sig = self.tcx().fn_sig(id);
6060
let fn_sig = self.tcx().liberate_late_bound_regions(id, &poly_fn_sig);
61-
body.params
62-
.iter()
63-
.enumerate()
64-
.filter_map(|(index, param)| {
65-
// May return None; sometimes the tables are not yet populated.
66-
let ty = fn_sig.inputs()[index];
67-
let mut found_anon_region = false;
68-
let new_param_ty = self.tcx().fold_regions(&ty, &mut false, |r, _| {
69-
if *r == *anon_region {
70-
found_anon_region = true;
71-
replace_region
72-
} else {
73-
r
74-
}
75-
});
76-
if found_anon_region {
77-
let ty_hir_id = fn_decl.inputs[index].hir_id;
78-
let param_ty_span = hir.span(ty_hir_id);
79-
let is_first = index == 0;
80-
Some(AnonymousParamInfo {
81-
param,
82-
param_ty: new_param_ty,
83-
param_ty_span,
84-
bound_region,
85-
is_first,
86-
})
61+
body.params.iter().enumerate().find_map(|(index, param)| {
62+
// May return None; sometimes the tables are not yet populated.
63+
let ty = fn_sig.inputs()[index];
64+
let mut found_anon_region = false;
65+
let new_param_ty = self.tcx().fold_regions(&ty, &mut false, |r, _| {
66+
if *r == *anon_region {
67+
found_anon_region = true;
68+
replace_region
8769
} else {
88-
None
70+
r
8971
}
90-
})
91-
.next()
72+
});
73+
if found_anon_region {
74+
let ty_hir_id = fn_decl.inputs[index].hir_id;
75+
let param_ty_span = hir.span(ty_hir_id);
76+
let is_first = index == 0;
77+
Some(AnonymousParamInfo {
78+
param,
79+
param_ty: new_param_ty,
80+
param_ty_span,
81+
bound_region,
82+
is_first,
83+
})
84+
} else {
85+
None
86+
}
87+
})
9288
}
9389

9490
// Here, we check for the case where the anonymous region

src/librustc_interface/util.rs

+8-11
Original file line numberDiff line numberDiff line change
@@ -267,17 +267,14 @@ pub fn rustc_path<'a>() -> Option<&'a Path> {
267267
}
268268

269269
fn get_rustc_path_inner(bin_path: &str) -> Option<PathBuf> {
270-
sysroot_candidates()
271-
.iter()
272-
.filter_map(|sysroot| {
273-
let candidate = sysroot.join(bin_path).join(if cfg!(target_os = "windows") {
274-
"rustc.exe"
275-
} else {
276-
"rustc"
277-
});
278-
candidate.exists().then_some(candidate)
279-
})
280-
.next()
270+
sysroot_candidates().iter().find_map(|sysroot| {
271+
let candidate = sysroot.join(bin_path).join(if cfg!(target_os = "windows") {
272+
"rustc.exe"
273+
} else {
274+
"rustc"
275+
});
276+
candidate.exists().then_some(candidate)
277+
})
281278
}
282279

283280
fn sysroot_candidates() -> Vec<PathBuf> {

src/librustc_middle/mir/interpret/error.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,7 @@ impl<'tcx> ConstEvalErr<'tcx> {
179179
.stacktrace
180180
.iter()
181181
.rev()
182-
.filter_map(|frame| frame.lint_root)
183-
.next()
182+
.find_map(|frame| frame.lint_root)
184183
.unwrap_or(lint_root);
185184
tcx.struct_span_lint_hir(
186185
rustc_session::lint::builtin::CONST_ERR,

src/librustc_middle/ty/print/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ pub fn characteristic_def_id_of_type(ty: Ty<'_>) -> Option<DefId> {
278278
ty::Ref(_, ty, _) => characteristic_def_id_of_type(ty),
279279

280280
ty::Tuple(ref tys) => {
281-
tys.iter().filter_map(|ty| characteristic_def_id_of_type(ty.expect_ty())).next()
281+
tys.iter().find_map(|ty| characteristic_def_id_of_type(ty.expect_ty()))
282282
}
283283

284284
ty::FnDef(def_id, _)

src/librustc_middle/ty/print/pretty.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -150,13 +150,10 @@ impl RegionHighlightMode {
150150

151151
/// Returns `Some(n)` with the number to use for the given region, if any.
152152
fn region_highlighted(&self, region: ty::Region<'_>) -> Option<usize> {
153-
self.highlight_regions
154-
.iter()
155-
.filter_map(|h| match h {
156-
Some((r, n)) if r == region => Some(*n),
157-
_ => None,
158-
})
159-
.next()
153+
self.highlight_regions.iter().find_map(|h| match h {
154+
Some((r, n)) if r == region => Some(*n),
155+
_ => None,
156+
})
160157
}
161158

162159
/// Highlight the given bound region.

src/librustc_mir/borrow_check/region_infer/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1815,11 +1815,10 @@ impl<'tcx> RegionInferenceContext<'tcx> {
18151815
RegionElement::PlaceholderRegion(error_placeholder) => self
18161816
.definitions
18171817
.iter_enumerated()
1818-
.filter_map(|(r, definition)| match definition.origin {
1818+
.find_map(|(r, definition)| match definition.origin {
18191819
NLLRegionVariableOrigin::Placeholder(p) if p == error_placeholder => Some(r),
18201820
_ => None,
18211821
})
1822-
.next()
18231822
.unwrap(),
18241823
}
18251824
}

src/librustc_mir/transform/rustc_peek.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,7 @@ pub fn sanity_check_via_rustc_peek<'tcx, A>(
113113
.statements
114114
.iter()
115115
.enumerate()
116-
.filter_map(|(i, stmt)| value_assigned_to_local(stmt, call.arg).map(|rval| (i, rval)))
117-
.next()
116+
.find_map(|(i, stmt)| value_assigned_to_local(stmt, call.arg).map(|rval| (i, rval)))
118117
.expect(
119118
"call to rustc_peek should be preceded by \
120119
assignment to temporary holding its argument",

src/librustc_resolve/late/lifetimes.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -480,14 +480,11 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
480480
let next_early_index = self.next_early_index();
481481
let was_in_fn_syntax = self.is_in_fn_syntax;
482482
self.is_in_fn_syntax = true;
483-
let lifetime_span: Option<Span> = c
484-
.generic_params
485-
.iter()
486-
.filter_map(|param| match param.kind {
483+
let lifetime_span: Option<Span> =
484+
c.generic_params.iter().rev().find_map(|param| match param.kind {
487485
GenericParamKind::Lifetime { .. } => Some(param.span),
488486
_ => None,
489-
})
490-
.last();
487+
});
491488
let (span, span_type) = if let Some(span) = lifetime_span {
492489
(span.shrink_to_hi(), ForLifetimeSpanType::TypeTail)
493490
} else {

src/librustc_typeck/astconv.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1140,13 +1140,12 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
11401140
.generic_args()
11411141
.bindings
11421142
.iter()
1143-
.filter_map(|b| match (b.ident.as_str() == "Output", &b.kind) {
1143+
.find_map(|b| match (b.ident.as_str() == "Output", &b.kind) {
11441144
(true, hir::TypeBindingKind::Equality { ty }) => {
11451145
sess.source_map().span_to_snippet(ty.span).ok()
11461146
}
11471147
_ => None,
11481148
})
1149-
.next()
11501149
.unwrap_or_else(|| "()".to_string()),
11511150
)),
11521151
)

src/librustc_typeck/check/closure.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -177,13 +177,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
177177

178178
match expected_ty.kind {
179179
ty::Dynamic(ref object_type, ..) => {
180-
let sig = object_type
181-
.projection_bounds()
182-
.filter_map(|pb| {
183-
let pb = pb.with_self_ty(self.tcx, self.tcx.types.err);
184-
self.deduce_sig_from_projection(None, &pb)
185-
})
186-
.next();
180+
let sig = object_type.projection_bounds().find_map(|pb| {
181+
let pb = pb.with_self_ty(self.tcx, self.tcx.types.err);
182+
self.deduce_sig_from_projection(None, &pb)
183+
});
187184
let kind = object_type
188185
.principal_def_id()
189186
.and_then(|did| self.tcx.fn_trait_kind_from_lang_item(did));

src/librustc_typeck/check/compare_method.rs

+7-10
Original file line numberDiff line numberDiff line change
@@ -453,16 +453,13 @@ fn extract_spans_for_error_reporting<'a, 'tcx>(
453453
.zip(trait_iter)
454454
.zip(impl_m_iter)
455455
.zip(trait_m_iter)
456-
.filter_map(
457-
|(((&impl_arg_ty, &trait_arg_ty), impl_arg), trait_arg)| match infcx
458-
.at(&cause, param_env)
459-
.sub(trait_arg_ty, impl_arg_ty)
460-
{
461-
Ok(_) => None,
462-
Err(_) => Some((impl_arg.span, Some(trait_arg.span))),
463-
},
464-
)
465-
.next()
456+
.find_map(|(((&impl_arg_ty, &trait_arg_ty), impl_arg), trait_arg)| match infcx
457+
.at(&cause, param_env)
458+
.sub(trait_arg_ty, impl_arg_ty)
459+
{
460+
Ok(_) => None,
461+
Err(_) => Some((impl_arg.span, Some(trait_arg.span))),
462+
})
466463
.unwrap_or_else(|| {
467464
if infcx
468465
.at(&cause, param_env)

src/librustc_typeck/check/method/confirm.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
269269
self.fcx
270270
.autoderef(self.span, self_ty)
271271
.include_raw_pointers()
272-
.filter_map(|(ty, _)| match ty.kind {
272+
.find_map(|(ty, _)| match ty.kind {
273273
ty::Dynamic(ref data, ..) => Some(closure(
274274
self,
275275
ty,
@@ -279,7 +279,6 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
279279
)),
280280
_ => None,
281281
})
282-
.next()
283282
.unwrap_or_else(|| {
284283
span_bug!(
285284
self.span,
@@ -579,20 +578,18 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
579578
.predicates
580579
.iter()
581580
.zip(predicates.spans.iter())
582-
.filter_map(
581+
.find_map(
583582
|(p, span)| if *p == obligation.predicate { Some(*span) } else { None },
584583
)
585-
.next()
586584
.unwrap_or(rustc_span::DUMMY_SP);
587585
Some((trait_pred, span))
588586
}
589587
_ => None,
590588
})
591-
.filter_map(|(trait_pred, span)| match trait_pred.skip_binder().self_ty().kind {
589+
.find_map(|(trait_pred, span)| match trait_pred.skip_binder().self_ty().kind {
592590
ty::Dynamic(..) => Some(span),
593591
_ => None,
594592
})
595-
.next()
596593
}
597594

598595
fn enforce_illegal_method_limitations(&self, pick: &probe::Pick<'_>) {

src/librustdoc/html/render.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -3527,14 +3527,13 @@ fn render_deref_methods(
35273527
.inner_impl()
35283528
.items
35293529
.iter()
3530-
.filter_map(|item| match item.inner {
3530+
.find_map(|item| match item.inner {
35313531
clean::TypedefItem(ref t, true) => Some(match *t {
35323532
clean::Typedef { item_type: Some(ref type_), .. } => (type_, &t.type_),
35333533
_ => (&t.type_, &t.type_),
35343534
}),
35353535
_ => None,
35363536
})
3537-
.next()
35383537
.expect("Expected associated type binding");
35393538
let what =
35403539
AssocItemRender::DerefFor { trait_: deref_type, type_: real_target, deref_mut_: deref_mut };
@@ -4111,18 +4110,14 @@ fn sidebar_assoc_items(it: &clean::Item) -> String {
41114110
.filter(|i| i.inner_impl().trait_.is_some())
41124111
.find(|i| i.inner_impl().trait_.def_id() == c.deref_trait_did)
41134112
{
4114-
if let Some((target, real_target)) = impl_
4115-
.inner_impl()
4116-
.items
4117-
.iter()
4118-
.filter_map(|item| match item.inner {
4113+
if let Some((target, real_target)) =
4114+
impl_.inner_impl().items.iter().find_map(|item| match item.inner {
41194115
clean::TypedefItem(ref t, true) => Some(match *t {
41204116
clean::Typedef { item_type: Some(ref type_), .. } => (type_, &t.type_),
41214117
_ => (&t.type_, &t.type_),
41224118
}),
41234119
_ => None,
41244120
})
4125-
.next()
41264121
{
41274122
let inner_impl = target
41284123
.def_id()

src/librustdoc/passes/collect_trait_impls.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,10 @@ pub fn collect_trait_impls(krate: Crate, cx: &DocContext<'_>) -> Crate {
8989
if cleaner.keep_item(for_) && trait_.def_id() == cx.tcx.lang_items().deref_trait() {
9090
let target = items
9191
.iter()
92-
.filter_map(|item| match item.inner {
92+
.find_map(|item| match item.inner {
9393
TypedefItem(ref t, true) => Some(&t.type_),
9494
_ => None,
9595
})
96-
.next()
9796
.expect("Deref impl without Target type");
9897

9998
if let Some(prim) = target.primitive_type() {

0 commit comments

Comments
 (0)