From 261ca04c925e1844bbd3525168d129055637e60e Mon Sep 17 00:00:00 2001 From: Nicholas-Baron Date: Fri, 6 Nov 2020 13:24:55 -0800 Subject: [PATCH] Changed unwrap_or to unwrap_or_else in some places. The discussion seems to have resolved that this lint is a bit "noisy" in that applying it in all places would result in a reduction in readability. A few of the trivial functions (like `Path::new`) are fine to leave outside of closures. The general rule seems to be that anything that is obviously an allocation (`Box`, `Vec`, `vec![]`) should be in a closure, even if it is a 0-sized allocation. --- compiler/rustc_ast/src/attr/mod.rs | 6 +++--- .../rustc_builtin_macros/src/test_harness.rs | 2 +- .../rustc_codegen_cranelift/src/bin/cg_clif.rs | 4 ++-- compiler/rustc_codegen_llvm/src/intrinsic.rs | 14 ++++++++------ compiler/rustc_hir/src/definitions.rs | 2 +- compiler/rustc_metadata/src/rmeta/decoder.rs | 16 ++++++++-------- .../src/borrow_check/region_infer/mod.rs | 2 +- compiler/rustc_session/src/session.rs | 2 +- .../src/traits/select/mod.rs | 2 +- compiler/rustc_typeck/src/check/fn_ctxt/_impl.rs | 2 +- compiler/rustc_typeck/src/check/method/probe.rs | 2 +- library/std/src/time.rs | 2 +- src/librustdoc/clean/mod.rs | 3 +-- src/librustdoc/html/render/cache.rs | 2 +- 14 files changed, 31 insertions(+), 30 deletions(-) diff --git a/compiler/rustc_ast/src/attr/mod.rs b/compiler/rustc_ast/src/attr/mod.rs index ec87a88f4ab..5fc625bf9ef 100644 --- a/compiler/rustc_ast/src/attr/mod.rs +++ b/compiler/rustc_ast/src/attr/mod.rs @@ -66,7 +66,7 @@ impl NestedMetaItem { self.meta_item().and_then(|meta_item| meta_item.ident()) } pub fn name_or_empty(&self) -> Symbol { - self.ident().unwrap_or(Ident::invalid()).name + self.ident().unwrap_or_else(Ident::invalid).name } /// Gets the string value if `self` is a `MetaItem` and the `MetaItem` is a @@ -139,7 +139,7 @@ impl Attribute { } } pub fn name_or_empty(&self) -> Symbol { - self.ident().unwrap_or(Ident::invalid()).name + self.ident().unwrap_or_else(Ident::invalid).name } pub fn value_str(&self) -> Option { @@ -183,7 +183,7 @@ impl MetaItem { if self.path.segments.len() == 1 { Some(self.path.segments[0].ident) } else { None } } pub fn name_or_empty(&self) -> Symbol { - self.ident().unwrap_or(Ident::invalid()).name + self.ident().unwrap_or_else(Ident::invalid).name } // Example: diff --git a/compiler/rustc_builtin_macros/src/test_harness.rs b/compiler/rustc_builtin_macros/src/test_harness.rs index cd4c982b192..9976140d6bd 100644 --- a/compiler/rustc_builtin_macros/src/test_harness.rs +++ b/compiler/rustc_builtin_macros/src/test_harness.rs @@ -282,7 +282,7 @@ fn mk_main(cx: &mut TestCtxt<'_>) -> P { let mut test_runner = cx .test_runner .clone() - .unwrap_or(ecx.path(sp, vec![test_id, Ident::from_str_and_span(runner_name, sp)])); + .unwrap_or_else(|| ecx.path(sp, vec![test_id, Ident::from_str_and_span(runner_name, sp)])); test_runner.span = sp; diff --git a/compiler/rustc_codegen_cranelift/src/bin/cg_clif.rs b/compiler/rustc_codegen_cranelift/src/bin/cg_clif.rs index 71ef4d22673..cd01acc9a83 100644 --- a/compiler/rustc_codegen_cranelift/src/bin/cg_clif.rs +++ b/compiler/rustc_codegen_cranelift/src/bin/cg_clif.rs @@ -27,8 +27,8 @@ impl rustc_driver::Callbacks for CraneliftPassesCallbacks { config.opts.cg.panic = Some(PanicStrategy::Abort); config.opts.debugging_opts.panic_abort_tests = true; config.opts.maybe_sysroot = Some( - config.opts.maybe_sysroot.clone().unwrap_or( - std::env::current_exe() + config.opts.maybe_sysroot.clone().unwrap_or_else( + || std::env::current_exe() .unwrap() .parent() .unwrap() diff --git a/compiler/rustc_codegen_llvm/src/intrinsic.rs b/compiler/rustc_codegen_llvm/src/intrinsic.rs index 4f999f8b560..d52b3be8cd3 100644 --- a/compiler/rustc_codegen_llvm/src/intrinsic.rs +++ b/compiler/rustc_codegen_llvm/src/intrinsic.rs @@ -979,12 +979,14 @@ fn generic_simd_intrinsic( // Integer vector : let (i_xn, in_elem_bitwidth) = match in_elem.kind() { - ty::Int(i) => { - (args[0].immediate(), i.bit_width().unwrap_or(bx.data_layout().pointer_size.bits())) - } - ty::Uint(i) => { - (args[0].immediate(), i.bit_width().unwrap_or(bx.data_layout().pointer_size.bits())) - } + ty::Int(i) => ( + args[0].immediate(), + i.bit_width().unwrap_or_else(|| bx.data_layout().pointer_size.bits()), + ), + ty::Uint(i) => ( + args[0].immediate(), + i.bit_width().unwrap_or_else(|| bx.data_layout().pointer_size.bits()), + ), _ => return_error!( "vector argument `{}`'s element type `{}`, expected integer element type", in_ty, diff --git a/compiler/rustc_hir/src/definitions.rs b/compiler/rustc_hir/src/definitions.rs index 3f109376a3e..d5ade86593e 100644 --- a/compiler/rustc_hir/src/definitions.rs +++ b/compiler/rustc_hir/src/definitions.rs @@ -409,7 +409,7 @@ impl Definitions { } pub fn expansion_that_defined(&self, id: LocalDefId) -> ExpnId { - self.expansions_that_defined.get(&id).copied().unwrap_or(ExpnId::root()) + self.expansions_that_defined.get(&id).copied().unwrap_or_else(ExpnId::root) } pub fn parent_module_of_macro_def(&self, expn_id: ExpnId) -> DefId { diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index c031e0e2e19..746c3b6af12 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -856,7 +856,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { .tables .children .get(self, index) - .unwrap_or(Lazy::empty()) + .unwrap_or_else(Lazy::empty) .decode(self) .map(|index| ty::FieldDef { did: self.local_def_id(index), @@ -888,7 +888,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { .tables .children .get(self, item_id) - .unwrap_or(Lazy::empty()) + .unwrap_or_else(Lazy::empty) .decode(self) .map(|index| self.get_variant(&self.kind(index), index, did, tcx.sess)) .collect() @@ -1075,7 +1075,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { // Iterate over all children. let macros_only = self.dep_kind.lock().macros_only(); - let children = self.root.tables.children.get(self, id).unwrap_or(Lazy::empty()); + let children = self.root.tables.children.get(self, id).unwrap_or_else(Lazy::empty); for child_index in children.decode((self, sess)) { if macros_only { continue; @@ -1098,7 +1098,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { .tables .children .get(self, child_index) - .unwrap_or(Lazy::empty()); + .unwrap_or_else(Lazy::empty); for child_index in child_children.decode((self, sess)) { let kind = self.def_kind(child_index); callback(Export { @@ -1284,7 +1284,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { } fn get_item_variances(&self, id: DefIndex) -> Vec { - self.root.tables.variances.get(self, id).unwrap_or(Lazy::empty()).decode(self).collect() + self.root.tables.variances.get(self, id).unwrap_or_else(Lazy::empty).decode(self).collect() } fn get_ctor_kind(&self, node_id: DefIndex) -> CtorKind { @@ -1323,7 +1323,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { .tables .attributes .get(self, item_id) - .unwrap_or(Lazy::empty()) + .unwrap_or_else(Lazy::empty) .decode((self, sess)) .collect::>() } @@ -1333,7 +1333,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { .tables .children .get(self, id) - .unwrap_or(Lazy::empty()) + .unwrap_or_else(Lazy::empty) .decode(self) .map(|index| respan(self.get_span(index, sess), self.item_ident(index, sess).name)) .collect() @@ -1349,7 +1349,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { .tables .inherent_impls .get(self, id) - .unwrap_or(Lazy::empty()) + .unwrap_or_else(Lazy::empty) .decode(self) .map(|index| self.local_def_id(index)), ) diff --git a/compiler/rustc_mir/src/borrow_check/region_infer/mod.rs b/compiler/rustc_mir/src/borrow_check/region_infer/mod.rs index ac8ab71a1dc..a5a7012852d 100644 --- a/compiler/rustc_mir/src/borrow_check/region_infer/mod.rs +++ b/compiler/rustc_mir/src/borrow_check/region_infer/mod.rs @@ -582,7 +582,7 @@ impl<'tcx> RegionInferenceContext<'tcx> { self.check_member_constraints(infcx, &mut errors_buffer); } - let outlives_requirements = outlives_requirements.unwrap_or(vec![]); + let outlives_requirements = outlives_requirements.unwrap_or_default(); if outlives_requirements.is_empty() { (None, errors_buffer) diff --git a/compiler/rustc_session/src/session.rs b/compiler/rustc_session/src/session.rs index d0aa2809789..98b7f03df38 100644 --- a/compiler/rustc_session/src/session.rs +++ b/compiler/rustc_session/src/session.rs @@ -1305,7 +1305,7 @@ pub fn build_session( early_error(sopts.error_format, &format!("Error loading host specification: {}", e)) }); - let loader = file_loader.unwrap_or(Box::new(RealFileLoader)); + let loader = file_loader.unwrap_or_else(|| Box::new(RealFileLoader)); let hash_kind = sopts.debugging_opts.src_hash_algorithm.unwrap_or_else(|| { if target_cfg.is_like_msvc { SourceFileHashAlgorithm::Sha1 diff --git a/compiler/rustc_trait_selection/src/traits/select/mod.rs b/compiler/rustc_trait_selection/src/traits/select/mod.rs index 4cc4bc0acda..a91f693f175 100644 --- a/compiler/rustc_trait_selection/src/traits/select/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/select/mod.rs @@ -279,7 +279,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { /// tracking is not enabled, just returns an empty vector. pub fn take_intercrate_ambiguity_causes(&mut self) -> Vec { assert!(self.intercrate); - self.intercrate_ambiguity_causes.take().unwrap_or(vec![]) + self.intercrate_ambiguity_causes.take().unwrap_or_default() } pub fn infcx(&self) -> &'cx InferCtxt<'cx, 'tcx> { diff --git a/compiler/rustc_typeck/src/check/fn_ctxt/_impl.rs b/compiler/rustc_typeck/src/check/fn_ctxt/_impl.rs index f87e6b607d4..0bb7b464f16 100644 --- a/compiler/rustc_typeck/src/check/fn_ctxt/_impl.rs +++ b/compiler/rustc_typeck/src/check/fn_ctxt/_impl.rs @@ -118,7 +118,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { closure_def_id: DefId, ) -> Vec> { let mut deferred_call_resolutions = self.deferred_call_resolutions.borrow_mut(); - deferred_call_resolutions.remove(&closure_def_id).unwrap_or(vec![]) + deferred_call_resolutions.remove(&closure_def_id).unwrap_or_default() } pub fn tag(&self) -> String { diff --git a/compiler/rustc_typeck/src/check/method/probe.rs b/compiler/rustc_typeck/src/check/method/probe.rs index d403e259398..713b24e583a 100644 --- a/compiler/rustc_typeck/src/check/method/probe.rs +++ b/compiler/rustc_typeck/src/check/method/probe.rs @@ -244,7 +244,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { ProbeScope::AllTraits, |probe_cx| Ok(probe_cx.candidate_method_names()), ) - .unwrap_or(vec![]); + .unwrap_or_default(); method_names .iter() .flat_map(|&method_name| { diff --git a/library/std/src/time.rs b/library/std/src/time.rs index 64d7898f030..e433f69a8b0 100644 --- a/library/std/src/time.rs +++ b/library/std/src/time.rs @@ -322,7 +322,7 @@ impl Instant { /// ``` #[stable(feature = "checked_duration_since", since = "1.39.0")] pub fn saturating_duration_since(&self, earlier: Instant) -> Duration { - self.checked_duration_since(earlier).unwrap_or(Duration::new(0, 0)) + self.checked_duration_since(earlier).unwrap_or_default() } /// Returns the amount of time elapsed since this instant was created. diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index ec7932d8bbf..366548d5b5f 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -935,8 +935,7 @@ impl<'a> Clean for (&'a [hir::Ty<'a>], &'a [Ident]) { .iter() .enumerate() .map(|(i, ty)| { - let mut name = - self.1.get(i).map(|ident| ident.to_string()).unwrap_or(String::new()); + let mut name = self.1.get(i).map(|ident| ident.to_string()).unwrap_or_default(); if name.is_empty() { name = "_".to_string(); } diff --git a/src/librustdoc/html/render/cache.rs b/src/librustdoc/html/render/cache.rs index cf785d362cd..add28de17ed 100644 --- a/src/librustdoc/html/render/cache.rs +++ b/src/librustdoc/html/render/cache.rs @@ -128,7 +128,7 @@ pub fn build_index(krate: &clean::Crate, cache: &mut Cache) -> String { .module .as_ref() .map(|module| shorten(plain_text_summary(module.doc_value()))) - .unwrap_or(String::new()); + .unwrap_or_default(); #[derive(Serialize)] struct CrateData<'a> {