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`.
This commit is contained in:
commit
039b1b62ac
17 changed files with 34 additions and 28 deletions
|
@ -2372,7 +2372,7 @@ fn compute_type_parameters(cx: &CodegenCx<'ll, 'tcx>, ty: Ty<'tcx>) -> &'ll DIAr
|
||||||
fn get_parameter_names(cx: &CodegenCx<'_, '_>, generics: &ty::Generics) -> Vec<Symbol> {
|
fn get_parameter_names(cx: &CodegenCx<'_, '_>, generics: &ty::Generics) -> Vec<Symbol> {
|
||||||
let mut names = generics
|
let mut names = generics
|
||||||
.parent
|
.parent
|
||||||
.map_or(vec![], |def_id| get_parameter_names(cx, cx.tcx.generics_of(def_id)));
|
.map_or_else(Vec::new, |def_id| get_parameter_names(cx, cx.tcx.generics_of(def_id)));
|
||||||
names.extend(generics.params.iter().map(|param| param.name));
|
names.extend(generics.params.iter().map(|param| param.name));
|
||||||
names
|
names
|
||||||
}
|
}
|
||||||
|
|
|
@ -481,9 +481,9 @@ impl DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_parameter_names(cx: &CodegenCx<'_, '_>, generics: &ty::Generics) -> Vec<Symbol> {
|
fn get_parameter_names(cx: &CodegenCx<'_, '_>, generics: &ty::Generics) -> Vec<Symbol> {
|
||||||
let mut names = generics
|
let mut names = generics.parent.map_or_else(Vec::new, |def_id| {
|
||||||
.parent
|
get_parameter_names(cx, cx.tcx.generics_of(def_id))
|
||||||
.map_or(vec![], |def_id| get_parameter_names(cx, cx.tcx.generics_of(def_id)));
|
});
|
||||||
names.extend(generics.params.iter().map(|param| param.name));
|
names.extend(generics.params.iter().map(|param| param.name));
|
||||||
names
|
names
|
||||||
}
|
}
|
||||||
|
|
|
@ -65,8 +65,8 @@ fn search_meta_section<'a>(
|
||||||
while llvm::LLVMIsSectionIteratorAtEnd(of.llof, si.llsi) == False {
|
while llvm::LLVMIsSectionIteratorAtEnd(of.llof, si.llsi) == False {
|
||||||
let mut name_buf = None;
|
let mut name_buf = None;
|
||||||
let name_len = llvm::LLVMRustGetSectionName(si.llsi, &mut name_buf);
|
let name_len = llvm::LLVMRustGetSectionName(si.llsi, &mut name_buf);
|
||||||
let name = name_buf.map_or(
|
let name = name_buf.map_or_else(
|
||||||
String::new(), // We got a NULL ptr, ignore `name_len`.
|
String::new, // We got a NULL ptr, ignore `name_len`.
|
||||||
|buf| {
|
|buf| {
|
||||||
String::from_utf8(
|
String::from_utf8(
|
||||||
slice::from_raw_parts(buf.as_ptr() as *const u8, name_len as usize)
|
slice::from_raw_parts(buf.as_ptr() as *const u8, name_len as usize)
|
||||||
|
|
|
@ -2082,7 +2082,7 @@ fn add_upstream_rust_crates<'a, B: ArchiveBuilder<'a>>(
|
||||||
let filestem = cratepath.file_stem().unwrap().to_str().unwrap();
|
let filestem = cratepath.file_stem().unwrap().to_str().unwrap();
|
||||||
cmd.link_rust_dylib(
|
cmd.link_rust_dylib(
|
||||||
Symbol::intern(&unlib(&sess.target, filestem)),
|
Symbol::intern(&unlib(&sess.target, filestem)),
|
||||||
parent.unwrap_or(Path::new("")),
|
parent.unwrap_or_else(|| Path::new("")),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -201,7 +201,7 @@ fn check_panic_str<'tcx>(
|
||||||
Some(v) if v.len() == 1 => "panic message contains a brace",
|
Some(v) if v.len() == 1 => "panic message contains a brace",
|
||||||
_ => "panic message contains braces",
|
_ => "panic message contains braces",
|
||||||
};
|
};
|
||||||
cx.struct_span_lint(NON_FMT_PANIC, brace_spans.unwrap_or(vec![span]), |lint| {
|
cx.struct_span_lint(NON_FMT_PANIC, brace_spans.unwrap_or_else(|| vec![span]), |lint| {
|
||||||
let mut l = lint.build(msg);
|
let mut l = lint.build(msg);
|
||||||
l.note("this message is not used as a format string, but will be in Rust 2021");
|
l.note("this message is not used as a format string, but will be in Rust 2021");
|
||||||
if span.contains(arg.span) {
|
if span.contains(arg.span) {
|
||||||
|
|
|
@ -378,14 +378,14 @@ fn add_query_description_impl(
|
||||||
let t = &(t.0).0;
|
let t = &(t.0).0;
|
||||||
quote! { #t }
|
quote! { #t }
|
||||||
})
|
})
|
||||||
.unwrap_or(quote! { _ });
|
.unwrap_or_else(|| quote! { _ });
|
||||||
let value = args
|
let value = args
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.map(|t| {
|
.map(|t| {
|
||||||
let t = &(t.1).0;
|
let t = &(t.1).0;
|
||||||
quote! { #t }
|
quote! { #t }
|
||||||
})
|
})
|
||||||
.unwrap_or(quote! { _ });
|
.unwrap_or_else(|| quote! { _ });
|
||||||
// expr is a `Block`, meaning that `{ #expr }` gets expanded
|
// expr is a `Block`, meaning that `{ #expr }` gets expanded
|
||||||
// to `{ { stmts... } }`, which triggers the `unused_braces` lint.
|
// to `{ { stmts... } }`, which triggers the `unused_braces` lint.
|
||||||
quote! {
|
quote! {
|
||||||
|
@ -409,7 +409,7 @@ fn add_query_description_impl(
|
||||||
};
|
};
|
||||||
|
|
||||||
let (tcx, desc) = modifiers.desc;
|
let (tcx, desc) = modifiers.desc;
|
||||||
let tcx = tcx.as_ref().map_or(quote! { _ }, |t| quote! { #t });
|
let tcx = tcx.as_ref().map_or_else(|| quote! { _ }, |t| quote! { #t });
|
||||||
|
|
||||||
let desc = quote! {
|
let desc = quote! {
|
||||||
#[allow(unused_variables)]
|
#[allow(unused_variables)]
|
||||||
|
|
|
@ -473,9 +473,9 @@ impl<'a> SessionDiagnosticDeriveBuilder<'a> {
|
||||||
.map(
|
.map(
|
||||||
|applicability_idx| quote!(#binding.#applicability_idx),
|
|applicability_idx| quote!(#binding.#applicability_idx),
|
||||||
)
|
)
|
||||||
.unwrap_or(quote!(
|
.unwrap_or_else(|| {
|
||||||
rustc_errors::Applicability::Unspecified
|
quote!(rustc_errors::Applicability::Unspecified)
|
||||||
));
|
});
|
||||||
return Ok((span, applicability));
|
return Ok((span, applicability));
|
||||||
}
|
}
|
||||||
throw_span_err!(
|
throw_span_err!(
|
||||||
|
|
|
@ -50,7 +50,7 @@ fn eval_body_using_ecx<'mir, 'tcx>(
|
||||||
|
|
||||||
let name =
|
let name =
|
||||||
with_no_trimmed_paths(|| ty::tls::with(|tcx| tcx.def_path_str(cid.instance.def_id())));
|
with_no_trimmed_paths(|| ty::tls::with(|tcx| tcx.def_path_str(cid.instance.def_id())));
|
||||||
let prom = cid.promoted.map_or(String::new(), |p| format!("::promoted[{:?}]", p));
|
let prom = cid.promoted.map_or_else(String::new, |p| format!("::promoted[{:?}]", p));
|
||||||
trace!("eval_body_using_ecx: pushing stack frame for global: {}{}", name, prom);
|
trace!("eval_body_using_ecx: pushing stack frame for global: {}{}", name, prom);
|
||||||
|
|
||||||
ecx.push_stack_frame(
|
ecx.push_stack_frame(
|
||||||
|
|
|
@ -223,7 +223,7 @@ impl<'a> Parser<'a> {
|
||||||
fn tokens_to_string(tokens: &[TokenType]) -> String {
|
fn tokens_to_string(tokens: &[TokenType]) -> String {
|
||||||
let mut i = tokens.iter();
|
let mut i = tokens.iter();
|
||||||
// This might be a sign we need a connect method on `Iterator`.
|
// This might be a sign we need a connect method on `Iterator`.
|
||||||
let b = i.next().map_or(String::new(), |t| t.to_string());
|
let b = i.next().map_or_else(String::new, |t| t.to_string());
|
||||||
i.enumerate().fold(b, |mut b, (i, a)| {
|
i.enumerate().fold(b, |mut b, (i, a)| {
|
||||||
if tokens.len() > 2 && i == tokens.len() - 2 {
|
if tokens.len() > 2 && i == tokens.len() - 2 {
|
||||||
b.push_str(", or ");
|
b.push_str(", or ");
|
||||||
|
|
|
@ -1971,7 +1971,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
|
||||||
// Therefore, we would compute `object_lifetime_defaults` to a
|
// Therefore, we would compute `object_lifetime_defaults` to a
|
||||||
// vector like `['x, 'static]`. Note that the vector only
|
// vector like `['x, 'static]`. Note that the vector only
|
||||||
// includes type parameters.
|
// includes type parameters.
|
||||||
let object_lifetime_defaults = type_def_id.map_or(vec![], |def_id| {
|
let object_lifetime_defaults = type_def_id.map_or_else(Vec::new, |def_id| {
|
||||||
let in_body = {
|
let in_body = {
|
||||||
let mut scope = self.scope;
|
let mut scope = self.scope;
|
||||||
loop {
|
loop {
|
||||||
|
|
|
@ -169,7 +169,7 @@ pub fn get_or_default_sysroot() -> PathBuf {
|
||||||
|
|
||||||
// Check if sysroot is found using env::args().next(), and if is not found,
|
// Check if sysroot is found using env::args().next(), and if is not found,
|
||||||
// use env::current_exe() to imply sysroot.
|
// use env::current_exe() to imply sysroot.
|
||||||
from_env_args_next().unwrap_or(from_current_exe())
|
from_env_args_next().unwrap_or_else(from_current_exe)
|
||||||
}
|
}
|
||||||
|
|
||||||
// The name of the directory rustc expects libraries to be located.
|
// The name of the directory rustc expects libraries to be located.
|
||||||
|
|
|
@ -349,7 +349,7 @@ fn report_negative_positive_conflict(
|
||||||
E0751,
|
E0751,
|
||||||
"found both positive and negative implementation of trait `{}`{}:",
|
"found both positive and negative implementation of trait `{}`{}:",
|
||||||
overlap.trait_desc,
|
overlap.trait_desc,
|
||||||
overlap.self_desc.clone().map_or(String::new(), |ty| format!(" for type `{}`", ty))
|
overlap.self_desc.clone().map_or_else(String::new, |ty| format!(" for type `{}`", ty))
|
||||||
);
|
);
|
||||||
|
|
||||||
match tcx.span_of_impl(negative_impl_def_id) {
|
match tcx.span_of_impl(negative_impl_def_id) {
|
||||||
|
@ -397,7 +397,10 @@ fn report_conflicting_impls(
|
||||||
let msg = format!(
|
let msg = format!(
|
||||||
"conflicting implementations of trait `{}`{}:{}",
|
"conflicting implementations of trait `{}`{}:{}",
|
||||||
overlap.trait_desc,
|
overlap.trait_desc,
|
||||||
overlap.self_desc.clone().map_or(String::new(), |ty| { format!(" for type `{}`", ty) }),
|
overlap
|
||||||
|
.self_desc
|
||||||
|
.clone()
|
||||||
|
.map_or_else(String::new, |ty| { format!(" for type `{}`", ty) }),
|
||||||
match used_to_be_allowed {
|
match used_to_be_allowed {
|
||||||
Some(FutureCompatOverlapErrorKind::Issue33140) => " (E0119)",
|
Some(FutureCompatOverlapErrorKind::Issue33140) => " (E0119)",
|
||||||
_ => "",
|
_ => "",
|
||||||
|
@ -415,7 +418,7 @@ fn report_conflicting_impls(
|
||||||
impl_span,
|
impl_span,
|
||||||
format!(
|
format!(
|
||||||
"conflicting implementation{}",
|
"conflicting implementation{}",
|
||||||
overlap.self_desc.map_or(String::new(), |ty| format!(" for `{}`", ty))
|
overlap.self_desc.map_or_else(String::new, |ty| format!(" for `{}`", ty))
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1716,7 +1716,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
|
||||||
} else {
|
} else {
|
||||||
self.fcx
|
self.fcx
|
||||||
.associated_item(def_id, name, Namespace::ValueNS)
|
.associated_item(def_id, name, Namespace::ValueNS)
|
||||||
.map_or(Vec::new(), |x| vec![x])
|
.map_or_else(Vec::new, |x| vec![x])
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
self.tcx.associated_items(def_id).in_definition_order().copied().collect()
|
self.tcx.associated_items(def_id).in_definition_order().copied().collect()
|
||||||
|
|
|
@ -1062,7 +1062,10 @@ fn report_unexpected_variant_res(tcx: TyCtxt<'_>, res: Res, span: Span) {
|
||||||
E0533,
|
E0533,
|
||||||
"expected unit struct, unit variant or constant, found {}{}",
|
"expected unit struct, unit variant or constant, found {}{}",
|
||||||
res.descr(),
|
res.descr(),
|
||||||
tcx.sess.source_map().span_to_snippet(span).map_or(String::new(), |s| format!(" `{}`", s)),
|
tcx.sess
|
||||||
|
.source_map()
|
||||||
|
.span_to_snippet(span)
|
||||||
|
.map_or_else(|_| String::new(), |s| format!(" `{}`", s)),
|
||||||
)
|
)
|
||||||
.emit();
|
.emit();
|
||||||
}
|
}
|
||||||
|
|
|
@ -879,7 +879,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||||
let sm = tcx.sess.source_map();
|
let sm = tcx.sess.source_map();
|
||||||
let path_str = sm
|
let path_str = sm
|
||||||
.span_to_snippet(sm.span_until_char(pat.span, '('))
|
.span_to_snippet(sm.span_until_char(pat.span, '('))
|
||||||
.map_or(String::new(), |s| format!(" `{}`", s.trim_end()));
|
.map_or_else(|_| String::new(), |s| format!(" `{}`", s.trim_end()));
|
||||||
let msg = format!(
|
let msg = format!(
|
||||||
"expected tuple struct or tuple variant, found {}{}",
|
"expected tuple struct or tuple variant, found {}{}",
|
||||||
res.descr(),
|
res.descr(),
|
||||||
|
|
|
@ -348,9 +348,9 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> {
|
||||||
let min_list_wb = min_list
|
let min_list_wb = min_list
|
||||||
.iter()
|
.iter()
|
||||||
.map(|captured_place| {
|
.map(|captured_place| {
|
||||||
let locatable = captured_place.info.path_expr_id.unwrap_or(
|
let locatable = captured_place.info.path_expr_id.unwrap_or_else(|| {
|
||||||
self.tcx().hir().local_def_id_to_hir_id(closure_def_id.expect_local()),
|
self.tcx().hir().local_def_id_to_hir_id(closure_def_id.expect_local())
|
||||||
);
|
});
|
||||||
|
|
||||||
self.resolve(captured_place.clone(), &locatable)
|
self.resolve(captured_place.clone(), &locatable)
|
||||||
})
|
})
|
||||||
|
|
|
@ -2387,7 +2387,7 @@ fn compute_sig_of_foreign_fn_decl<'tcx>(
|
||||||
.sess
|
.sess
|
||||||
.source_map()
|
.source_map()
|
||||||
.span_to_snippet(ast_ty.span)
|
.span_to_snippet(ast_ty.span)
|
||||||
.map_or(String::new(), |s| format!(" `{}`", s));
|
.map_or_else(|_| String::new(), |s| format!(" `{}`", s));
|
||||||
tcx.sess
|
tcx.sess
|
||||||
.struct_span_err(
|
.struct_span_err(
|
||||||
ast_ty.span,
|
ast_ty.span,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue