Skip to content

Commit 9709785

Browse files
authored
Rollup merge of #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

Lines changed: 1 addition & 5 deletions
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

Lines changed: 10 additions & 13 deletions
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

Lines changed: 1 addition & 2 deletions
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

Lines changed: 1 addition & 2 deletions
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

Lines changed: 25 additions & 29 deletions
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

Lines changed: 8 additions & 11 deletions
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

Lines changed: 1 addition & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 4 additions & 7 deletions
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

Lines changed: 1 addition & 2 deletions
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
}

0 commit comments

Comments
 (0)