From 5ff1be197eaa8a014718b26b1fcbe0ced33b6e1a Mon Sep 17 00:00:00 2001 From: klensy Date: Tue, 23 Feb 2021 23:56:04 +0300 Subject: [PATCH 1/3] replaced some unwrap_or with unwrap_or_else --- compiler/rustc_codegen_ssa/src/back/link.rs | 2 +- compiler/rustc_lint/src/non_fmt_panic.rs | 2 +- compiler/rustc_macros/src/query.rs | 4 ++-- compiler/rustc_macros/src/session_diagnostic.rs | 6 +++--- compiler/rustc_session/src/filesearch.rs | 2 +- compiler/rustc_typeck/src/check/writeback.rs | 6 +++--- 6 files changed, 11 insertions(+), 11 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/back/link.rs b/compiler/rustc_codegen_ssa/src/back/link.rs index b93a579df1526..531edd28c23ce 100644 --- a/compiler/rustc_codegen_ssa/src/back/link.rs +++ b/compiler/rustc_codegen_ssa/src/back/link.rs @@ -2082,7 +2082,7 @@ fn add_upstream_rust_crates<'a, B: ArchiveBuilder<'a>>( let filestem = cratepath.file_stem().unwrap().to_str().unwrap(); cmd.link_rust_dylib( Symbol::intern(&unlib(&sess.target, filestem)), - parent.unwrap_or(Path::new("")), + parent.unwrap_or_else(|| Path::new("")), ); } } diff --git a/compiler/rustc_lint/src/non_fmt_panic.rs b/compiler/rustc_lint/src/non_fmt_panic.rs index e98297b692c92..1a6d968cdb558 100644 --- a/compiler/rustc_lint/src/non_fmt_panic.rs +++ b/compiler/rustc_lint/src/non_fmt_panic.rs @@ -159,7 +159,7 @@ fn check_panic_str<'tcx>( Some(v) if v.len() == 1 => "panic message contains a brace", _ => "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); l.note("this message is not used as a format string, but will be in Rust 2021"); if span.contains(arg.span) { diff --git a/compiler/rustc_macros/src/query.rs b/compiler/rustc_macros/src/query.rs index 3e67525567f97..763f13d17eff8 100644 --- a/compiler/rustc_macros/src/query.rs +++ b/compiler/rustc_macros/src/query.rs @@ -378,14 +378,14 @@ fn add_query_description_impl( let t = &(t.0).0; quote! { #t } }) - .unwrap_or(quote! { _ }); + .unwrap_or_else(|| quote! { _ }); let value = args .as_ref() .map(|t| { let t = &(t.1).0; quote! { #t } }) - .unwrap_or(quote! { _ }); + .unwrap_or_else(|| quote! { _ }); // expr is a `Block`, meaning that `{ #expr }` gets expanded // to `{ { stmts... } }`, which triggers the `unused_braces` lint. quote! { diff --git a/compiler/rustc_macros/src/session_diagnostic.rs b/compiler/rustc_macros/src/session_diagnostic.rs index 5c061a9d3c794..8a0fce209b7fb 100644 --- a/compiler/rustc_macros/src/session_diagnostic.rs +++ b/compiler/rustc_macros/src/session_diagnostic.rs @@ -473,9 +473,9 @@ impl<'a> SessionDiagnosticDeriveBuilder<'a> { .map( |applicability_idx| quote!(#binding.#applicability_idx), ) - .unwrap_or(quote!( - rustc_errors::Applicability::Unspecified - )); + .unwrap_or_else(|| { + quote!(rustc_errors::Applicability::Unspecified) + }); return Ok((span, applicability)); } throw_span_err!( diff --git a/compiler/rustc_session/src/filesearch.rs b/compiler/rustc_session/src/filesearch.rs index 47f14fa6b7a74..72daf666c9942 100644 --- a/compiler/rustc_session/src/filesearch.rs +++ b/compiler/rustc_session/src/filesearch.rs @@ -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, // 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. diff --git a/compiler/rustc_typeck/src/check/writeback.rs b/compiler/rustc_typeck/src/check/writeback.rs index 4f78598215077..af82a3bb4f59a 100644 --- a/compiler/rustc_typeck/src/check/writeback.rs +++ b/compiler/rustc_typeck/src/check/writeback.rs @@ -348,9 +348,9 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> { let min_list_wb = min_list .iter() .map(|captured_place| { - let locatable = captured_place.info.path_expr_id.unwrap_or( - self.tcx().hir().local_def_id_to_hir_id(closure_def_id.expect_local()), - ); + 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.resolve(captured_place.clone(), &locatable) }) From c75c4a579bdea69fc9b93697aa2531daf82540fc Mon Sep 17 00:00:00 2001 From: klensy Date: Wed, 24 Feb 2021 01:02:05 +0300 Subject: [PATCH 2/3] replaced some map_or with map_or_else --- .../src/debuginfo/metadata.rs | 2 +- .../rustc_codegen_llvm/src/debuginfo/mod.rs | 7 +- compiler/rustc_codegen_llvm/src/metadata.rs | 4 +- compiler/rustc_macros/src/query.rs | 2 +- .../rustc_mir/src/const_eval/eval_queries.rs | 2 +- .../rustc_parse/src/parser/diagnostics.rs | 2 +- compiler/rustc_resolve/src/late/lifetimes.rs | 111 +++++++++--------- .../src/traits/specialize/mod.rs | 11 +- .../rustc_typeck/src/check/method/probe.rs | 2 +- compiler/rustc_typeck/src/check/mod.rs | 5 +- compiler/rustc_typeck/src/check/pat.rs | 2 +- compiler/rustc_typeck/src/collect.rs | 2 +- 12 files changed, 82 insertions(+), 70 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs b/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs index d935fc83b7212..2f1cc7fccfc61 100644 --- a/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs +++ b/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs @@ -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 { let mut names = generics .parent - .map_or(vec![], |def_id| get_parameter_names(cx, cx.tcx.generics_of(def_id))); + .map_or_else(|| vec![], |def_id| get_parameter_names(cx, cx.tcx.generics_of(def_id))); names.extend(generics.params.iter().map(|param| param.name)); names } diff --git a/compiler/rustc_codegen_llvm/src/debuginfo/mod.rs b/compiler/rustc_codegen_llvm/src/debuginfo/mod.rs index 955e739b2c126..21585bf77a029 100644 --- a/compiler/rustc_codegen_llvm/src/debuginfo/mod.rs +++ b/compiler/rustc_codegen_llvm/src/debuginfo/mod.rs @@ -481,9 +481,10 @@ impl DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> { } fn get_parameter_names(cx: &CodegenCx<'_, '_>, generics: &ty::Generics) -> Vec { - let mut names = generics - .parent - .map_or(vec![], |def_id| get_parameter_names(cx, cx.tcx.generics_of(def_id))); + let mut names = generics.parent.map_or_else( + || vec![], + |def_id| get_parameter_names(cx, cx.tcx.generics_of(def_id)), + ); names.extend(generics.params.iter().map(|param| param.name)); names } diff --git a/compiler/rustc_codegen_llvm/src/metadata.rs b/compiler/rustc_codegen_llvm/src/metadata.rs index 3912d6a3a48b6..ca973222cca8c 100644 --- a/compiler/rustc_codegen_llvm/src/metadata.rs +++ b/compiler/rustc_codegen_llvm/src/metadata.rs @@ -65,8 +65,8 @@ fn search_meta_section<'a>( while llvm::LLVMIsSectionIteratorAtEnd(of.llof, si.llsi) == False { let mut name_buf = None; let name_len = llvm::LLVMRustGetSectionName(si.llsi, &mut name_buf); - let name = name_buf.map_or( - String::new(), // We got a NULL ptr, ignore `name_len`. + let name = name_buf.map_or_else( + || String::new(), // We got a NULL ptr, ignore `name_len`. |buf| { String::from_utf8( slice::from_raw_parts(buf.as_ptr() as *const u8, name_len as usize) diff --git a/compiler/rustc_macros/src/query.rs b/compiler/rustc_macros/src/query.rs index 763f13d17eff8..291e7ef045e4f 100644 --- a/compiler/rustc_macros/src/query.rs +++ b/compiler/rustc_macros/src/query.rs @@ -409,7 +409,7 @@ fn add_query_description_impl( }; 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! { #[allow(unused_variables)] diff --git a/compiler/rustc_mir/src/const_eval/eval_queries.rs b/compiler/rustc_mir/src/const_eval/eval_queries.rs index de898460368b8..62c314d90f591 100644 --- a/compiler/rustc_mir/src/const_eval/eval_queries.rs +++ b/compiler/rustc_mir/src/const_eval/eval_queries.rs @@ -50,7 +50,7 @@ fn eval_body_using_ecx<'mir, 'tcx>( let name = 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); ecx.push_stack_frame( diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs index 0f49386dec07b..b5b5278b5ce6d 100644 --- a/compiler/rustc_parse/src/parser/diagnostics.rs +++ b/compiler/rustc_parse/src/parser/diagnostics.rs @@ -223,7 +223,7 @@ impl<'a> Parser<'a> { fn tokens_to_string(tokens: &[TokenType]) -> String { let mut i = tokens.iter(); // 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)| { if tokens.len() > 2 && i == tokens.len() - 2 { b.push_str(", or "); diff --git a/compiler/rustc_resolve/src/late/lifetimes.rs b/compiler/rustc_resolve/src/late/lifetimes.rs index c0e4a1bdf07a4..d7c643d0f6fed 100644 --- a/compiler/rustc_resolve/src/late/lifetimes.rs +++ b/compiler/rustc_resolve/src/late/lifetimes.rs @@ -1971,65 +1971,68 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { // Therefore, we would compute `object_lifetime_defaults` to a // vector like `['x, 'static]`. Note that the vector only // includes type parameters. - let object_lifetime_defaults = type_def_id.map_or(vec![], |def_id| { - let in_body = { - let mut scope = self.scope; - loop { - match *scope { - Scope::Root => break false, - - Scope::Body { .. } => break true, - - Scope::Binder { s, .. } - | Scope::Elision { s, .. } - | Scope::ObjectLifetimeDefault { s, .. } => { - scope = s; + let object_lifetime_defaults = type_def_id.map_or_else( + || vec![], + |def_id| { + let in_body = { + let mut scope = self.scope; + loop { + match *scope { + Scope::Root => break false, + + Scope::Body { .. } => break true, + + Scope::Binder { s, .. } + | Scope::Elision { s, .. } + | Scope::ObjectLifetimeDefault { s, .. } => { + scope = s; + } } } - } - }; + }; - let map = &self.map; - let unsubst = if let Some(def_id) = def_id.as_local() { - let id = self.tcx.hir().local_def_id_to_hir_id(def_id); - &map.object_lifetime_defaults[&id] - } else { - let tcx = self.tcx; - self.xcrate_object_lifetime_defaults.entry(def_id).or_insert_with(|| { - tcx.generics_of(def_id) - .params - .iter() - .filter_map(|param| match param.kind { - GenericParamDefKind::Type { object_lifetime_default, .. } => { - Some(object_lifetime_default) + let map = &self.map; + let unsubst = if let Some(def_id) = def_id.as_local() { + let id = self.tcx.hir().local_def_id_to_hir_id(def_id); + &map.object_lifetime_defaults[&id] + } else { + let tcx = self.tcx; + self.xcrate_object_lifetime_defaults.entry(def_id).or_insert_with(|| { + tcx.generics_of(def_id) + .params + .iter() + .filter_map(|param| match param.kind { + GenericParamDefKind::Type { object_lifetime_default, .. } => { + Some(object_lifetime_default) + } + GenericParamDefKind::Lifetime | GenericParamDefKind::Const => None, + }) + .collect() + }) + }; + debug!("visit_segment_args: unsubst={:?}", unsubst); + unsubst + .iter() + .map(|set| match *set { + Set1::Empty => { + if in_body { + None + } else { + Some(Region::Static) } - GenericParamDefKind::Lifetime | GenericParamDefKind::Const => None, - }) - .collect() - }) - }; - debug!("visit_segment_args: unsubst={:?}", unsubst); - unsubst - .iter() - .map(|set| match *set { - Set1::Empty => { - if in_body { - None - } else { - Some(Region::Static) } - } - Set1::One(r) => { - let lifetimes = generic_args.args.iter().filter_map(|arg| match arg { - GenericArg::Lifetime(lt) => Some(lt), - _ => None, - }); - r.subst(lifetimes, map) - } - Set1::Many => None, - }) - .collect() - }); + Set1::One(r) => { + let lifetimes = generic_args.args.iter().filter_map(|arg| match arg { + GenericArg::Lifetime(lt) => Some(lt), + _ => None, + }); + r.subst(lifetimes, map) + } + Set1::Many => None, + }) + .collect() + }, + ); debug!("visit_segment_args: object_lifetime_defaults={:?}", object_lifetime_defaults); diff --git a/compiler/rustc_trait_selection/src/traits/specialize/mod.rs b/compiler/rustc_trait_selection/src/traits/specialize/mod.rs index 0133a961c11a7..9759ab8a8a6a4 100644 --- a/compiler/rustc_trait_selection/src/traits/specialize/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/specialize/mod.rs @@ -349,7 +349,7 @@ fn report_negative_positive_conflict( E0751, "found both positive and negative implementation of trait `{}`{}:", 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) { @@ -397,7 +397,10 @@ fn report_conflicting_impls( let msg = format!( "conflicting implementations of trait `{}`{}:{}", 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 { Some(FutureCompatOverlapErrorKind::Issue33140) => " (E0119)", _ => "", @@ -415,7 +418,9 @@ fn report_conflicting_impls( impl_span, format!( "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)) ), ); } diff --git a/compiler/rustc_typeck/src/check/method/probe.rs b/compiler/rustc_typeck/src/check/method/probe.rs index 158c214759db2..54d3f11566da9 100644 --- a/compiler/rustc_typeck/src/check/method/probe.rs +++ b/compiler/rustc_typeck/src/check/method/probe.rs @@ -1695,7 +1695,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> { } else { self.fcx .associated_item(def_id, name, Namespace::ValueNS) - .map_or(Vec::new(), |x| vec![x]) + .map_or_else(|| Vec::new(), |x| vec![x]) } } else { self.tcx.associated_items(def_id).in_definition_order().copied().collect() diff --git a/compiler/rustc_typeck/src/check/mod.rs b/compiler/rustc_typeck/src/check/mod.rs index fce7ae8119e17..bb85336d7fb2d 100644 --- a/compiler/rustc_typeck/src/check/mod.rs +++ b/compiler/rustc_typeck/src/check/mod.rs @@ -1062,7 +1062,10 @@ fn report_unexpected_variant_res(tcx: TyCtxt<'_>, res: Res, span: Span) { E0533, "expected unit struct, unit variant or constant, found {}{}", 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(); } diff --git a/compiler/rustc_typeck/src/check/pat.rs b/compiler/rustc_typeck/src/check/pat.rs index 897b1f01569f4..f8ca916caf127 100644 --- a/compiler/rustc_typeck/src/check/pat.rs +++ b/compiler/rustc_typeck/src/check/pat.rs @@ -879,7 +879,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let sm = tcx.sess.source_map(); let path_str = sm .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!( "expected tuple struct or tuple variant, found {}{}", res.descr(), diff --git a/compiler/rustc_typeck/src/collect.rs b/compiler/rustc_typeck/src/collect.rs index fc8e50b4b65ba..9fbc56f051beb 100644 --- a/compiler/rustc_typeck/src/collect.rs +++ b/compiler/rustc_typeck/src/collect.rs @@ -2387,7 +2387,7 @@ fn compute_sig_of_foreign_fn_decl<'tcx>( .sess .source_map() .span_to_snippet(ast_ty.span) - .map_or(String::new(), |s| format!(" `{}`", s)); + .map_or_else(|_| String::new(), |s| format!(" `{}`", s)); tcx.sess .struct_span_err( ast_ty.span, From 08b1e8004b06112fb8334155b2cea314bd3e8161 Mon Sep 17 00:00:00 2001 From: klensy Date: Thu, 25 Feb 2021 04:13:42 +0300 Subject: [PATCH 3/3] fix review --- .../src/debuginfo/metadata.rs | 2 +- .../rustc_codegen_llvm/src/debuginfo/mod.rs | 7 +- compiler/rustc_codegen_llvm/src/metadata.rs | 2 +- .../rustc_mir/src/const_eval/eval_queries.rs | 2 +- .../rustc_parse/src/parser/diagnostics.rs | 2 +- compiler/rustc_resolve/src/late/lifetimes.rs | 111 +++++++++--------- compiler/rustc_session/src/filesearch.rs | 2 +- .../src/traits/specialize/mod.rs | 8 +- .../rustc_typeck/src/check/method/probe.rs | 2 +- 9 files changed, 66 insertions(+), 72 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs b/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs index 2f1cc7fccfc61..1464784ae288c 100644 --- a/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs +++ b/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs @@ -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 { let mut names = generics .parent - .map_or_else(|| 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 } diff --git a/compiler/rustc_codegen_llvm/src/debuginfo/mod.rs b/compiler/rustc_codegen_llvm/src/debuginfo/mod.rs index 21585bf77a029..c28b0d64651e3 100644 --- a/compiler/rustc_codegen_llvm/src/debuginfo/mod.rs +++ b/compiler/rustc_codegen_llvm/src/debuginfo/mod.rs @@ -481,10 +481,9 @@ impl DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> { } fn get_parameter_names(cx: &CodegenCx<'_, '_>, generics: &ty::Generics) -> Vec { - let mut names = generics.parent.map_or_else( - || vec![], - |def_id| get_parameter_names(cx, cx.tcx.generics_of(def_id)), - ); + let mut names = generics.parent.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 } diff --git a/compiler/rustc_codegen_llvm/src/metadata.rs b/compiler/rustc_codegen_llvm/src/metadata.rs index ca973222cca8c..b007df5730621 100644 --- a/compiler/rustc_codegen_llvm/src/metadata.rs +++ b/compiler/rustc_codegen_llvm/src/metadata.rs @@ -66,7 +66,7 @@ fn search_meta_section<'a>( let mut name_buf = None; let name_len = llvm::LLVMRustGetSectionName(si.llsi, &mut name_buf); 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| { String::from_utf8( slice::from_raw_parts(buf.as_ptr() as *const u8, name_len as usize) diff --git a/compiler/rustc_mir/src/const_eval/eval_queries.rs b/compiler/rustc_mir/src/const_eval/eval_queries.rs index 62c314d90f591..fa234ff5feb64 100644 --- a/compiler/rustc_mir/src/const_eval/eval_queries.rs +++ b/compiler/rustc_mir/src/const_eval/eval_queries.rs @@ -50,7 +50,7 @@ fn eval_body_using_ecx<'mir, 'tcx>( let name = with_no_trimmed_paths(|| ty::tls::with(|tcx| tcx.def_path_str(cid.instance.def_id()))); - let prom = cid.promoted.map_or_else(|| 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); ecx.push_stack_frame( diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs index b5b5278b5ce6d..5b4939b740726 100644 --- a/compiler/rustc_parse/src/parser/diagnostics.rs +++ b/compiler/rustc_parse/src/parser/diagnostics.rs @@ -223,7 +223,7 @@ impl<'a> Parser<'a> { fn tokens_to_string(tokens: &[TokenType]) -> String { let mut i = tokens.iter(); // This might be a sign we need a connect method on `Iterator`. - let b = i.next().map_or_else(|| 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)| { if tokens.len() > 2 && i == tokens.len() - 2 { b.push_str(", or "); diff --git a/compiler/rustc_resolve/src/late/lifetimes.rs b/compiler/rustc_resolve/src/late/lifetimes.rs index d7c643d0f6fed..493f25f4992d1 100644 --- a/compiler/rustc_resolve/src/late/lifetimes.rs +++ b/compiler/rustc_resolve/src/late/lifetimes.rs @@ -1971,68 +1971,65 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { // Therefore, we would compute `object_lifetime_defaults` to a // vector like `['x, 'static]`. Note that the vector only // includes type parameters. - let object_lifetime_defaults = type_def_id.map_or_else( - || vec![], - |def_id| { - let in_body = { - let mut scope = self.scope; - loop { - match *scope { - Scope::Root => break false, - - Scope::Body { .. } => break true, - - Scope::Binder { s, .. } - | Scope::Elision { s, .. } - | Scope::ObjectLifetimeDefault { s, .. } => { - scope = s; - } + let object_lifetime_defaults = type_def_id.map_or_else(Vec::new, |def_id| { + let in_body = { + let mut scope = self.scope; + loop { + match *scope { + Scope::Root => break false, + + Scope::Body { .. } => break true, + + Scope::Binder { s, .. } + | Scope::Elision { s, .. } + | Scope::ObjectLifetimeDefault { s, .. } => { + scope = s; } } - }; + } + }; - let map = &self.map; - let unsubst = if let Some(def_id) = def_id.as_local() { - let id = self.tcx.hir().local_def_id_to_hir_id(def_id); - &map.object_lifetime_defaults[&id] - } else { - let tcx = self.tcx; - self.xcrate_object_lifetime_defaults.entry(def_id).or_insert_with(|| { - tcx.generics_of(def_id) - .params - .iter() - .filter_map(|param| match param.kind { - GenericParamDefKind::Type { object_lifetime_default, .. } => { - Some(object_lifetime_default) - } - GenericParamDefKind::Lifetime | GenericParamDefKind::Const => None, - }) - .collect() - }) - }; - debug!("visit_segment_args: unsubst={:?}", unsubst); - unsubst - .iter() - .map(|set| match *set { - Set1::Empty => { - if in_body { - None - } else { - Some(Region::Static) + let map = &self.map; + let unsubst = if let Some(def_id) = def_id.as_local() { + let id = self.tcx.hir().local_def_id_to_hir_id(def_id); + &map.object_lifetime_defaults[&id] + } else { + let tcx = self.tcx; + self.xcrate_object_lifetime_defaults.entry(def_id).or_insert_with(|| { + tcx.generics_of(def_id) + .params + .iter() + .filter_map(|param| match param.kind { + GenericParamDefKind::Type { object_lifetime_default, .. } => { + Some(object_lifetime_default) } + GenericParamDefKind::Lifetime | GenericParamDefKind::Const => None, + }) + .collect() + }) + }; + debug!("visit_segment_args: unsubst={:?}", unsubst); + unsubst + .iter() + .map(|set| match *set { + Set1::Empty => { + if in_body { + None + } else { + Some(Region::Static) } - Set1::One(r) => { - let lifetimes = generic_args.args.iter().filter_map(|arg| match arg { - GenericArg::Lifetime(lt) => Some(lt), - _ => None, - }); - r.subst(lifetimes, map) - } - Set1::Many => None, - }) - .collect() - }, - ); + } + Set1::One(r) => { + let lifetimes = generic_args.args.iter().filter_map(|arg| match arg { + GenericArg::Lifetime(lt) => Some(lt), + _ => None, + }); + r.subst(lifetimes, map) + } + Set1::Many => None, + }) + .collect() + }); debug!("visit_segment_args: object_lifetime_defaults={:?}", object_lifetime_defaults); diff --git a/compiler/rustc_session/src/filesearch.rs b/compiler/rustc_session/src/filesearch.rs index 72daf666c9942..2df326628e787 100644 --- a/compiler/rustc_session/src/filesearch.rs +++ b/compiler/rustc_session/src/filesearch.rs @@ -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, // use env::current_exe() to imply sysroot. - from_env_args_next().unwrap_or_else(|| from_current_exe()) + from_env_args_next().unwrap_or_else(from_current_exe) } // The name of the directory rustc expects libraries to be located. diff --git a/compiler/rustc_trait_selection/src/traits/specialize/mod.rs b/compiler/rustc_trait_selection/src/traits/specialize/mod.rs index 9759ab8a8a6a4..264cc4f248cec 100644 --- a/compiler/rustc_trait_selection/src/traits/specialize/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/specialize/mod.rs @@ -349,7 +349,7 @@ fn report_negative_positive_conflict( E0751, "found both positive and negative implementation of trait `{}`{}:", overlap.trait_desc, - overlap.self_desc.clone().map_or_else(|| 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) { @@ -400,7 +400,7 @@ fn report_conflicting_impls( overlap .self_desc .clone() - .map_or_else(|| String::new(), |ty| { format!(" for type `{}`", ty) }), + .map_or_else(String::new, |ty| { format!(" for type `{}`", ty) }), match used_to_be_allowed { Some(FutureCompatOverlapErrorKind::Issue33140) => " (E0119)", _ => "", @@ -418,9 +418,7 @@ fn report_conflicting_impls( impl_span, format!( "conflicting implementation{}", - overlap - .self_desc - .map_or_else(|| String::new(), |ty| format!(" for `{}`", ty)) + overlap.self_desc.map_or_else(String::new, |ty| format!(" for `{}`", ty)) ), ); } diff --git a/compiler/rustc_typeck/src/check/method/probe.rs b/compiler/rustc_typeck/src/check/method/probe.rs index 54d3f11566da9..fcb89e1234e0b 100644 --- a/compiler/rustc_typeck/src/check/method/probe.rs +++ b/compiler/rustc_typeck/src/check/method/probe.rs @@ -1695,7 +1695,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> { } else { self.fcx .associated_item(def_id, name, Namespace::ValueNS) - .map_or_else(|| Vec::new(), |x| vec![x]) + .map_or_else(Vec::new, |x| vec![x]) } } else { self.tcx.associated_items(def_id).in_definition_order().copied().collect()