Skip to content

Commit a660db3

Browse files
authored
Unrolled build for #142619
Rollup merge of #142619 - klensy:or_fun_call, r=nnethercote apply clippy::or_fun_call Applies https://rust-lang.github.io/rust-clippy/master/index.html?groups=nursery#or_fun_call to reduce needless allocs.
2 parents f9c15f4 + 8c83935 commit a660db3

File tree

13 files changed

+32
-28
lines changed

13 files changed

+32
-28
lines changed

compiler/rustc_codegen_ssa/src/mir/debuginfo.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
317317
let name = if bx.sess().fewer_names() {
318318
None
319319
} else {
320-
Some(match whole_local_var.or(fallback_var.clone()) {
320+
Some(match whole_local_var.or_else(|| fallback_var.clone()) {
321321
Some(var) if var.name != sym::empty => var.name.to_string(),
322322
_ => format!("{local:?}"),
323323
})

compiler/rustc_codegen_ssa/src/mir/naked_asm.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ fn prefix_and_suffix<'tcx>(
205205
let mut end = String::new();
206206
match asm_binary_format {
207207
BinaryFormat::Elf => {
208-
let section = link_section.unwrap_or(format!(".text.{asm_name}"));
208+
let section = link_section.unwrap_or_else(|| format!(".text.{asm_name}"));
209209

210210
let progbits = match is_arm {
211211
true => "%progbits",
@@ -239,7 +239,7 @@ fn prefix_and_suffix<'tcx>(
239239
}
240240
}
241241
BinaryFormat::MachO => {
242-
let section = link_section.unwrap_or("__TEXT,__text".to_string());
242+
let section = link_section.unwrap_or_else(|| "__TEXT,__text".to_string());
243243
writeln!(begin, ".pushsection {},regular,pure_instructions", section).unwrap();
244244
writeln!(begin, ".balign {align_bytes}").unwrap();
245245
write_linkage(&mut begin).unwrap();
@@ -256,7 +256,7 @@ fn prefix_and_suffix<'tcx>(
256256
}
257257
}
258258
BinaryFormat::Coff => {
259-
let section = link_section.unwrap_or(format!(".text.{asm_name}"));
259+
let section = link_section.unwrap_or_else(|| format!(".text.{asm_name}"));
260260
writeln!(begin, ".pushsection {},\"xr\"", section).unwrap();
261261
writeln!(begin, ".balign {align_bytes}").unwrap();
262262
write_linkage(&mut begin).unwrap();
@@ -273,7 +273,7 @@ fn prefix_and_suffix<'tcx>(
273273
}
274274
}
275275
BinaryFormat::Wasm => {
276-
let section = link_section.unwrap_or(format!(".text.{asm_name}"));
276+
let section = link_section.unwrap_or_else(|| format!(".text.{asm_name}"));
277277

278278
writeln!(begin, ".section {section},\"\",@").unwrap();
279279
// wasm functions cannot be aligned, so skip

compiler/rustc_hir_typeck/src/op.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
706706
.sess
707707
.source_map()
708708
.span_to_snippet(lhs_expr.span)
709-
.unwrap_or("_".to_string()),
709+
.unwrap_or_else(|_| "_".to_string()),
710710
};
711711

712712
if op.span().can_be_used_for_suggestions() {

compiler/rustc_metadata/src/locator.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,7 +1196,7 @@ impl CrateError {
11961196
.opts
11971197
.crate_name
11981198
.clone()
1199-
.unwrap_or("<unknown>".to_string()),
1199+
.unwrap_or_else(|| "<unknown>".to_string()),
12001200
is_nightly_build: sess.is_nightly_build(),
12011201
profiler_runtime: Symbol::intern(&sess.opts.unstable_opts.profiler_runtime),
12021202
locator_triple: locator.triple,
@@ -1217,7 +1217,11 @@ impl CrateError {
12171217
crate_name,
12181218
add_info: String::new(),
12191219
missing_core,
1220-
current_crate: sess.opts.crate_name.clone().unwrap_or("<unknown>".to_string()),
1220+
current_crate: sess
1221+
.opts
1222+
.crate_name
1223+
.clone()
1224+
.unwrap_or_else(|| "<unknown>".to_string()),
12211225
is_nightly_build: sess.is_nightly_build(),
12221226
profiler_runtime: Symbol::intern(&sess.opts.unstable_opts.profiler_runtime),
12231227
locator_triple: sess.opts.target_triple.clone(),

compiler/rustc_metadata/src/native_libs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ impl<'tcx> Collector<'tcx> {
705705
.map_or(import_name_type, |ord| Some(PeImportNameType::Ordinal(ord)));
706706

707707
DllImport {
708-
name: codegen_fn_attrs.link_name.unwrap_or(self.tcx.item_name(item)),
708+
name: codegen_fn_attrs.link_name.unwrap_or_else(|| self.tcx.item_name(item)),
709709
import_name_type,
710710
calling_convention,
711711
span,

compiler/rustc_middle/src/query/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1576,7 +1576,7 @@ rustc_queries! {
15761576
query vtable_allocation(key: (Ty<'tcx>, Option<ty::ExistentialTraitRef<'tcx>>)) -> mir::interpret::AllocId {
15771577
desc { |tcx| "vtable const allocation for <{} as {}>",
15781578
key.0,
1579-
key.1.map(|trait_ref| format!("{trait_ref}")).unwrap_or("_".to_owned())
1579+
key.1.map(|trait_ref| format!("{trait_ref}")).unwrap_or_else(|| "_".to_owned())
15801580
}
15811581
}
15821582

compiler/rustc_passes/src/check_export.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ impl<'tcx> ExportableItemCollector<'tcx> {
5353
let is_pub = visibilities.is_directly_public(def_id);
5454

5555
if has_attr && !is_pub {
56-
let vis = visibilities.effective_vis(def_id).cloned().unwrap_or(
56+
let vis = visibilities.effective_vis(def_id).cloned().unwrap_or_else(|| {
5757
EffectiveVisibility::from_vis(Visibility::Restricted(
5858
self.tcx.parent_module_from_def_id(def_id).to_local_def_id(),
59-
)),
60-
);
59+
))
60+
});
6161
let vis = vis.at_level(Level::Direct);
6262
let span = self.tcx.def_span(def_id);
6363

compiler/rustc_session/src/filesearch.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,9 @@ pub fn get_or_default_sysroot() -> PathBuf {
209209
//
210210
// use `parent` twice to chop off the file name and then also the
211211
// directory containing the dll
212-
let dir = dll.parent().and_then(|p| p.parent()).ok_or(format!(
213-
"Could not move 2 levels upper using `parent()` on {}",
214-
dll.display()
215-
))?;
212+
let dir = dll.parent().and_then(|p| p.parent()).ok_or_else(|| {
213+
format!("Could not move 2 levels upper using `parent()` on {}", dll.display())
214+
})?;
216215

217216
// if `dir` points to target's dir, move up to the sysroot
218217
let mut sysroot_dir = if dir.ends_with(crate::config::host_tuple()) {
@@ -265,5 +264,6 @@ pub fn get_or_default_sysroot() -> PathBuf {
265264
rustlib_path.exists().then_some(p)
266265
}
267266

268-
from_env_args_next().unwrap_or(default_from_rustc_driver_dll().expect("Failed finding sysroot"))
267+
from_env_args_next()
268+
.unwrap_or_else(|| default_from_rustc_driver_dll().expect("Failed finding sysroot"))
269269
}

compiler/rustc_trait_selection/src/error_reporting/infer/region.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -891,7 +891,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
891891
(b'a'..=b'z')
892892
.map(|c| format!("'{}", c as char))
893893
.find(|candidate| !used_names.iter().any(|e| e.as_str() == candidate))
894-
.unwrap_or("'lt".to_string())
894+
.unwrap_or_else(|| "'lt".to_string())
895895
};
896896

897897
let mut visitor = LifetimeReplaceVisitor {

compiler/rustc_trait_selection/src/traits/normalize.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ impl<'a, 'b, 'tcx> AssocTypeNormalizer<'a, 'b, 'tcx> {
224224
)
225225
.ok()
226226
.flatten()
227-
.unwrap_or(proj.to_term(infcx.tcx));
227+
.unwrap_or_else(|| proj.to_term(infcx.tcx));
228228

229229
PlaceholderReplacer::replace_placeholders(
230230
infcx,

compiler/rustc_ty_utils/src/layout.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -896,10 +896,9 @@ fn variant_info_for_coroutine<'tcx>(
896896
variant_size = variant_size.max(offset + field_layout.size);
897897
FieldInfo {
898898
kind: FieldKind::CoroutineLocal,
899-
name: field_name.unwrap_or(Symbol::intern(&format!(
900-
".coroutine_field{}",
901-
local.as_usize()
902-
))),
899+
name: field_name.unwrap_or_else(|| {
900+
Symbol::intern(&format!(".coroutine_field{}", local.as_usize()))
901+
}),
903902
offset: offset.bytes(),
904903
size: field_layout.size.bytes(),
905904
align: field_layout.align.abi.bytes(),

src/tools/compiletest/src/runtest.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2368,7 +2368,8 @@ impl<'test> TestCx<'test> {
23682368
// Real paths into the libstd/libcore
23692369
let rust_src_dir = &self.config.sysroot_base.join("lib/rustlib/src/rust");
23702370
rust_src_dir.try_exists().expect(&*format!("{} should exists", rust_src_dir));
2371-
let rust_src_dir = rust_src_dir.read_link_utf8().unwrap_or(rust_src_dir.to_path_buf());
2371+
let rust_src_dir =
2372+
rust_src_dir.read_link_utf8().unwrap_or_else(|_| rust_src_dir.to_path_buf());
23722373
normalize_path(&rust_src_dir.join("library"), "$SRC_DIR_REAL");
23732374

23742375
// eg.

src/tools/compiletest/src/runtest/coverage.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -357,9 +357,9 @@ impl<'test> TestCx<'test> {
357357
// Add this line to the current subview.
358358
subviews
359359
.last_mut()
360-
.ok_or(format!(
361-
"unexpected subview line outside of a subview on line {line_num}"
362-
))?
360+
.ok_or_else(|| {
361+
format!("unexpected subview line outside of a subview on line {line_num}")
362+
})?
363363
.push(line);
364364
} else {
365365
// This line is not part of a subview, so sort and print any

0 commit comments

Comments
 (0)