Skip to content

Commit 039b1b6

Browse files
Rollup merge of #82456 - klensy:or-else, r=estebank
Replaced some unwrap_or and map_or with lazy variants Replaced some `unwrap_or` and `map_or` with `unwrap_or_else` and `map_or_else`.
2 parents a56bbb1 + 08b1e80 commit 039b1b6

File tree

17 files changed

+34
-28
lines changed

17 files changed

+34
-28
lines changed

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2372,7 +2372,7 @@ fn compute_type_parameters(cx: &CodegenCx<'ll, 'tcx>, ty: Ty<'tcx>) -> &'ll DIAr
23722372
fn get_parameter_names(cx: &CodegenCx<'_, '_>, generics: &ty::Generics) -> Vec<Symbol> {
23732373
let mut names = generics
23742374
.parent
2375-
.map_or(vec![], |def_id| get_parameter_names(cx, cx.tcx.generics_of(def_id)));
2375+
.map_or_else(Vec::new, |def_id| get_parameter_names(cx, cx.tcx.generics_of(def_id)));
23762376
names.extend(generics.params.iter().map(|param| param.name));
23772377
names
23782378
}

compiler/rustc_codegen_llvm/src/debuginfo/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -481,9 +481,9 @@ impl DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> {
481481
}
482482

483483
fn get_parameter_names(cx: &CodegenCx<'_, '_>, generics: &ty::Generics) -> Vec<Symbol> {
484-
let mut names = generics
485-
.parent
486-
.map_or(vec![], |def_id| get_parameter_names(cx, cx.tcx.generics_of(def_id)));
484+
let mut names = generics.parent.map_or_else(Vec::new, |def_id| {
485+
get_parameter_names(cx, cx.tcx.generics_of(def_id))
486+
});
487487
names.extend(generics.params.iter().map(|param| param.name));
488488
names
489489
}

compiler/rustc_codegen_llvm/src/metadata.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ fn search_meta_section<'a>(
6565
while llvm::LLVMIsSectionIteratorAtEnd(of.llof, si.llsi) == False {
6666
let mut name_buf = None;
6767
let name_len = llvm::LLVMRustGetSectionName(si.llsi, &mut name_buf);
68-
let name = name_buf.map_or(
69-
String::new(), // We got a NULL ptr, ignore `name_len`.
68+
let name = name_buf.map_or_else(
69+
String::new, // We got a NULL ptr, ignore `name_len`.
7070
|buf| {
7171
String::from_utf8(
7272
slice::from_raw_parts(buf.as_ptr() as *const u8, name_len as usize)

compiler/rustc_codegen_ssa/src/back/link.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2082,7 +2082,7 @@ fn add_upstream_rust_crates<'a, B: ArchiveBuilder<'a>>(
20822082
let filestem = cratepath.file_stem().unwrap().to_str().unwrap();
20832083
cmd.link_rust_dylib(
20842084
Symbol::intern(&unlib(&sess.target, filestem)),
2085-
parent.unwrap_or(Path::new("")),
2085+
parent.unwrap_or_else(|| Path::new("")),
20862086
);
20872087
}
20882088
}

compiler/rustc_lint/src/non_fmt_panic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ fn check_panic_str<'tcx>(
201201
Some(v) if v.len() == 1 => "panic message contains a brace",
202202
_ => "panic message contains braces",
203203
};
204-
cx.struct_span_lint(NON_FMT_PANIC, brace_spans.unwrap_or(vec![span]), |lint| {
204+
cx.struct_span_lint(NON_FMT_PANIC, brace_spans.unwrap_or_else(|| vec![span]), |lint| {
205205
let mut l = lint.build(msg);
206206
l.note("this message is not used as a format string, but will be in Rust 2021");
207207
if span.contains(arg.span) {

compiler/rustc_macros/src/query.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -378,14 +378,14 @@ fn add_query_description_impl(
378378
let t = &(t.0).0;
379379
quote! { #t }
380380
})
381-
.unwrap_or(quote! { _ });
381+
.unwrap_or_else(|| quote! { _ });
382382
let value = args
383383
.as_ref()
384384
.map(|t| {
385385
let t = &(t.1).0;
386386
quote! { #t }
387387
})
388-
.unwrap_or(quote! { _ });
388+
.unwrap_or_else(|| quote! { _ });
389389
// expr is a `Block`, meaning that `{ #expr }` gets expanded
390390
// to `{ { stmts... } }`, which triggers the `unused_braces` lint.
391391
quote! {
@@ -409,7 +409,7 @@ fn add_query_description_impl(
409409
};
410410

411411
let (tcx, desc) = modifiers.desc;
412-
let tcx = tcx.as_ref().map_or(quote! { _ }, |t| quote! { #t });
412+
let tcx = tcx.as_ref().map_or_else(|| quote! { _ }, |t| quote! { #t });
413413

414414
let desc = quote! {
415415
#[allow(unused_variables)]

compiler/rustc_macros/src/session_diagnostic.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -473,9 +473,9 @@ impl<'a> SessionDiagnosticDeriveBuilder<'a> {
473473
.map(
474474
|applicability_idx| quote!(#binding.#applicability_idx),
475475
)
476-
.unwrap_or(quote!(
477-
rustc_errors::Applicability::Unspecified
478-
));
476+
.unwrap_or_else(|| {
477+
quote!(rustc_errors::Applicability::Unspecified)
478+
});
479479
return Ok((span, applicability));
480480
}
481481
throw_span_err!(

compiler/rustc_mir/src/const_eval/eval_queries.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ fn eval_body_using_ecx<'mir, 'tcx>(
5050

5151
let name =
5252
with_no_trimmed_paths(|| ty::tls::with(|tcx| tcx.def_path_str(cid.instance.def_id())));
53-
let prom = cid.promoted.map_or(String::new(), |p| format!("::promoted[{:?}]", p));
53+
let prom = cid.promoted.map_or_else(String::new, |p| format!("::promoted[{:?}]", p));
5454
trace!("eval_body_using_ecx: pushing stack frame for global: {}{}", name, prom);
5555

5656
ecx.push_stack_frame(

compiler/rustc_parse/src/parser/diagnostics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ impl<'a> Parser<'a> {
223223
fn tokens_to_string(tokens: &[TokenType]) -> String {
224224
let mut i = tokens.iter();
225225
// This might be a sign we need a connect method on `Iterator`.
226-
let b = i.next().map_or(String::new(), |t| t.to_string());
226+
let b = i.next().map_or_else(String::new, |t| t.to_string());
227227
i.enumerate().fold(b, |mut b, (i, a)| {
228228
if tokens.len() > 2 && i == tokens.len() - 2 {
229229
b.push_str(", or ");

compiler/rustc_resolve/src/late/lifetimes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1971,7 +1971,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
19711971
// Therefore, we would compute `object_lifetime_defaults` to a
19721972
// vector like `['x, 'static]`. Note that the vector only
19731973
// includes type parameters.
1974-
let object_lifetime_defaults = type_def_id.map_or(vec![], |def_id| {
1974+
let object_lifetime_defaults = type_def_id.map_or_else(Vec::new, |def_id| {
19751975
let in_body = {
19761976
let mut scope = self.scope;
19771977
loop {

0 commit comments

Comments
 (0)