Skip to content

Commit 72aa8b1

Browse files
Rollup merge of rust-lang#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 99d4d41 + 08b1e80 commit 72aa8b1

File tree

17 files changed

+34
-28
lines changed

17 files changed

+34
-28
lines changed

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

+1-1
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

+3-3
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

+2-2
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

+1-1
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

+1-1
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

+3-3
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

+3-3
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

+1-1
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

+1-1
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

+1-1
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 {

compiler/rustc_session/src/filesearch.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ pub fn get_or_default_sysroot() -> PathBuf {
169169

170170
// Check if sysroot is found using env::args().next(), and if is not found,
171171
// use env::current_exe() to imply sysroot.
172-
from_env_args_next().unwrap_or(from_current_exe())
172+
from_env_args_next().unwrap_or_else(from_current_exe)
173173
}
174174

175175
// The name of the directory rustc expects libraries to be located.

compiler/rustc_trait_selection/src/traits/specialize/mod.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ fn report_negative_positive_conflict(
349349
E0751,
350350
"found both positive and negative implementation of trait `{}`{}:",
351351
overlap.trait_desc,
352-
overlap.self_desc.clone().map_or(String::new(), |ty| format!(" for type `{}`", ty))
352+
overlap.self_desc.clone().map_or_else(String::new, |ty| format!(" for type `{}`", ty))
353353
);
354354

355355
match tcx.span_of_impl(negative_impl_def_id) {
@@ -397,7 +397,10 @@ fn report_conflicting_impls(
397397
let msg = format!(
398398
"conflicting implementations of trait `{}`{}:{}",
399399
overlap.trait_desc,
400-
overlap.self_desc.clone().map_or(String::new(), |ty| { format!(" for type `{}`", ty) }),
400+
overlap
401+
.self_desc
402+
.clone()
403+
.map_or_else(String::new, |ty| { format!(" for type `{}`", ty) }),
401404
match used_to_be_allowed {
402405
Some(FutureCompatOverlapErrorKind::Issue33140) => " (E0119)",
403406
_ => "",
@@ -415,7 +418,7 @@ fn report_conflicting_impls(
415418
impl_span,
416419
format!(
417420
"conflicting implementation{}",
418-
overlap.self_desc.map_or(String::new(), |ty| format!(" for `{}`", ty))
421+
overlap.self_desc.map_or_else(String::new, |ty| format!(" for `{}`", ty))
419422
),
420423
);
421424
}

compiler/rustc_typeck/src/check/method/probe.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1716,7 +1716,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
17161716
} else {
17171717
self.fcx
17181718
.associated_item(def_id, name, Namespace::ValueNS)
1719-
.map_or(Vec::new(), |x| vec![x])
1719+
.map_or_else(Vec::new, |x| vec![x])
17201720
}
17211721
} else {
17221722
self.tcx.associated_items(def_id).in_definition_order().copied().collect()

compiler/rustc_typeck/src/check/mod.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1062,7 +1062,10 @@ fn report_unexpected_variant_res(tcx: TyCtxt<'_>, res: Res, span: Span) {
10621062
E0533,
10631063
"expected unit struct, unit variant or constant, found {}{}",
10641064
res.descr(),
1065-
tcx.sess.source_map().span_to_snippet(span).map_or(String::new(), |s| format!(" `{}`", s)),
1065+
tcx.sess
1066+
.source_map()
1067+
.span_to_snippet(span)
1068+
.map_or_else(|_| String::new(), |s| format!(" `{}`", s)),
10661069
)
10671070
.emit();
10681071
}

compiler/rustc_typeck/src/check/pat.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -879,7 +879,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
879879
let sm = tcx.sess.source_map();
880880
let path_str = sm
881881
.span_to_snippet(sm.span_until_char(pat.span, '('))
882-
.map_or(String::new(), |s| format!(" `{}`", s.trim_end()));
882+
.map_or_else(|_| String::new(), |s| format!(" `{}`", s.trim_end()));
883883
let msg = format!(
884884
"expected tuple struct or tuple variant, found {}{}",
885885
res.descr(),

compiler/rustc_typeck/src/check/writeback.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -348,9 +348,9 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> {
348348
let min_list_wb = min_list
349349
.iter()
350350
.map(|captured_place| {
351-
let locatable = captured_place.info.path_expr_id.unwrap_or(
352-
self.tcx().hir().local_def_id_to_hir_id(closure_def_id.expect_local()),
353-
);
351+
let locatable = captured_place.info.path_expr_id.unwrap_or_else(|| {
352+
self.tcx().hir().local_def_id_to_hir_id(closure_def_id.expect_local())
353+
});
354354

355355
self.resolve(captured_place.clone(), &locatable)
356356
})

compiler/rustc_typeck/src/collect.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2387,7 +2387,7 @@ fn compute_sig_of_foreign_fn_decl<'tcx>(
23872387
.sess
23882388
.source_map()
23892389
.span_to_snippet(ast_ty.span)
2390-
.map_or(String::new(), |s| format!(" `{}`", s));
2390+
.map_or_else(|_| String::new(), |s| format!(" `{}`", s));
23912391
tcx.sess
23922392
.struct_span_err(
23932393
ast_ty.span,

0 commit comments

Comments
 (0)