Skip to content

Commit d28aed6

Browse files
committed
Prefer unwrap_or_else to unwrap_or in case of function calls/allocations
1 parent cb5e1b9 commit d28aed6

File tree

35 files changed

+57
-54
lines changed

35 files changed

+57
-54
lines changed

src/bootstrap/config.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ impl Config {
430430
}
431431
}).unwrap_or_else(|| TomlConfig::default());
432432

433-
let build = toml.build.clone().unwrap_or(Build::default());
433+
let build = toml.build.clone().unwrap_or_default();
434434
// set by bootstrap.py
435435
config.hosts.push(config.build.clone());
436436
for host in build.host.iter() {
@@ -524,7 +524,7 @@ impl Config {
524524
set(&mut config.llvm_link_shared, llvm.link_shared);
525525
config.llvm_targets = llvm.targets.clone();
526526
config.llvm_experimental_targets = llvm.experimental_targets.clone()
527-
.unwrap_or("WebAssembly;RISCV".to_string());
527+
.unwrap_or_else(|| "WebAssembly;RISCV".to_string());
528528
config.llvm_link_jobs = llvm.link_jobs;
529529
config.llvm_version_suffix = llvm.version_suffix.clone();
530530
config.llvm_clang_cl = llvm.clang_cl.clone();

src/build_helper/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,8 @@ pub fn native_lib_boilerplate(
233233
let src_dir = current_dir.join("..").join(src_name);
234234
rerun_if_changed_anything_in_dir(&src_dir);
235235

236-
let out_dir = env::var_os("RUSTBUILD_NATIVE_DIR").unwrap_or(env::var_os("OUT_DIR").unwrap());
236+
let out_dir = env::var_os("RUSTBUILD_NATIVE_DIR").unwrap_or_else(||
237+
env::var_os("OUT_DIR").unwrap());
237238
let out_dir = PathBuf::from(out_dir).join(out_name);
238239
t!(fs::create_dir_all(&out_dir));
239240
if link_name.contains('=') {

src/librustc/hir/lowering.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1589,7 +1589,7 @@ impl<'a> LoweringContext<'a> {
15891589

15901590
let resolution = self.resolver
15911591
.get_resolution(id)
1592-
.unwrap_or(PathResolution::new(Def::Err));
1592+
.unwrap_or_else(|| PathResolution::new(Def::Err));
15931593

15941594
let proj_start = p.segments.len() - resolution.unresolved_segments();
15951595
let path = P(hir::Path {

src/librustc/lint/levels.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ impl LintLevelSets {
9797

9898
// If `level` is none then we actually assume the default level for this
9999
// lint.
100-
let mut level = level.unwrap_or(lint.default_level(sess));
100+
let mut level = level.unwrap_or_else(|| lint.default_level(sess));
101101

102102
// If we're about to issue a warning, check at the last minute for any
103103
// directives against the warnings "lint". If, for example, there's an

src/librustc/lint/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ impl LintBuffer {
519519
}
520520

521521
pub fn take(&mut self, id: ast::NodeId) -> Vec<BufferedEarlyLint> {
522-
self.map.remove(&id).unwrap_or(Vec::new())
522+
self.map.remove(&id).unwrap_or_default()
523523
}
524524

525525
pub fn get_any(&self) -> Option<&[BufferedEarlyLint]> {

src/librustc/traits/auto_trait.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -498,8 +498,8 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
498498
panic!("Missing lifetime with name {:?} for {:?}", name, region)
499499
)
500500
)
501-
.unwrap_or(&"'static".to_owned())
502-
.clone()
501+
.cloned()
502+
.unwrap_or_else(|| "'static".to_owned())
503503
}
504504

505505
// This is very similar to handle_lifetimes. However, instead of matching ty::Region's

src/librustc/traits/error_reporting.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
352352
obligation: &PredicateObligation<'tcx>,
353353
) -> OnUnimplementedNote {
354354
let def_id = self.impl_similar_to(trait_ref, obligation)
355-
.unwrap_or(trait_ref.def_id());
355+
.unwrap_or_else(|| trait_ref.def_id());
356356
let trait_ref = *trait_ref.skip_binder();
357357

358358
let mut flags = vec![];
@@ -639,7 +639,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
639639
let (post_message, pre_message) =
640640
self.get_parent_trait_ref(&obligation.cause.code)
641641
.map(|t| (format!(" in `{}`", t), format!("within `{}`, ", t)))
642-
.unwrap_or((String::new(), String::new()));
642+
.unwrap_or_default();
643643

644644
let OnUnimplementedNote { message, label, note }
645645
= self.on_unimplemented_note(trait_ref, obligation);

src/librustc/ty/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ impl<'tcx> TypeckTables<'tcx> {
565565

566566
pub fn node_substs(&self, id: hir::HirId) -> &'tcx Substs<'tcx> {
567567
validate_hir_id_for_typeck_tables(self.local_id_root, id, false);
568-
self.node_substs.get(&id.local_id).cloned().unwrap_or(Substs::empty())
568+
self.node_substs.get(&id.local_id).cloned().unwrap_or_else(|| Substs::empty())
569569
}
570570

571571
pub fn node_substs_opt(&self, id: hir::HirId) -> Option<&'tcx Substs<'tcx>> {

src/librustc/ty/query/on_disk_cache.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ impl<'sess> OnDiskCache<'sess> {
342342
&self.prev_diagnostics_index,
343343
"diagnostics");
344344

345-
diagnostics.unwrap_or(Vec::new())
345+
diagnostics.unwrap_or_default()
346346
}
347347

348348
/// Store a diagnostic emitted during the current compilation session.

src/librustc_codegen_llvm/attributes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ pub fn from_fn_attrs(
154154
id: Option<DefId>,
155155
) {
156156
let codegen_fn_attrs = id.map(|id| cx.tcx.codegen_fn_attrs(id))
157-
.unwrap_or(CodegenFnAttrs::new());
157+
.unwrap_or_else(|| CodegenFnAttrs::new());
158158

159159
inline(cx, llfn, codegen_fn_attrs.inline);
160160

0 commit comments

Comments
 (0)