Skip to content

apply clippy::or_fun_call #142619

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_ssa/src/mir/debuginfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
let name = if bx.sess().fewer_names() {
None
} else {
Some(match whole_local_var.or(fallback_var.clone()) {
Some(match whole_local_var.or_else(|| fallback_var.clone()) {
Some(var) if var.name != sym::empty => var.name.to_string(),
_ => format!("{local:?}"),
})
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_codegen_ssa/src/mir/naked_asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ fn prefix_and_suffix<'tcx>(
let mut end = String::new();
match asm_binary_format {
BinaryFormat::Elf => {
let section = link_section.unwrap_or(format!(".text.{asm_name}"));
let section = link_section.unwrap_or_else(|| format!(".text.{asm_name}"));

let progbits = match is_arm {
true => "%progbits",
Expand Down Expand Up @@ -239,7 +239,7 @@ fn prefix_and_suffix<'tcx>(
}
}
BinaryFormat::MachO => {
let section = link_section.unwrap_or("__TEXT,__text".to_string());
let section = link_section.unwrap_or_else(|| "__TEXT,__text".to_string());
writeln!(begin, ".pushsection {},regular,pure_instructions", section).unwrap();
writeln!(begin, ".balign {align_bytes}").unwrap();
write_linkage(&mut begin).unwrap();
Expand All @@ -256,7 +256,7 @@ fn prefix_and_suffix<'tcx>(
}
}
BinaryFormat::Coff => {
let section = link_section.unwrap_or(format!(".text.{asm_name}"));
let section = link_section.unwrap_or_else(|| format!(".text.{asm_name}"));
writeln!(begin, ".pushsection {},\"xr\"", section).unwrap();
writeln!(begin, ".balign {align_bytes}").unwrap();
write_linkage(&mut begin).unwrap();
Expand All @@ -273,7 +273,7 @@ fn prefix_and_suffix<'tcx>(
}
}
BinaryFormat::Wasm => {
let section = link_section.unwrap_or(format!(".text.{asm_name}"));
let section = link_section.unwrap_or_else(|| format!(".text.{asm_name}"));

writeln!(begin, ".section {section},\"\",@").unwrap();
// wasm functions cannot be aligned, so skip
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_typeck/src/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
.sess
.source_map()
.span_to_snippet(lhs_expr.span)
.unwrap_or("_".to_string()),
.unwrap_or_else(|_| "_".to_string()),
};

if op.span().can_be_used_for_suggestions() {
Expand Down
8 changes: 6 additions & 2 deletions compiler/rustc_metadata/src/locator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1196,7 +1196,7 @@ impl CrateError {
.opts
.crate_name
.clone()
.unwrap_or("<unknown>".to_string()),
.unwrap_or_else(|| "<unknown>".to_string()),
is_nightly_build: sess.is_nightly_build(),
profiler_runtime: Symbol::intern(&sess.opts.unstable_opts.profiler_runtime),
locator_triple: locator.triple,
Expand All @@ -1217,7 +1217,11 @@ impl CrateError {
crate_name,
add_info: String::new(),
missing_core,
current_crate: sess.opts.crate_name.clone().unwrap_or("<unknown>".to_string()),
current_crate: sess
.opts
.crate_name
.clone()
.unwrap_or_else(|| "<unknown>".to_string()),
is_nightly_build: sess.is_nightly_build(),
profiler_runtime: Symbol::intern(&sess.opts.unstable_opts.profiler_runtime),
locator_triple: sess.opts.target_triple.clone(),
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_metadata/src/native_libs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ impl<'tcx> Collector<'tcx> {
.map_or(import_name_type, |ord| Some(PeImportNameType::Ordinal(ord)));

DllImport {
name: codegen_fn_attrs.link_name.unwrap_or(self.tcx.item_name(item)),
name: codegen_fn_attrs.link_name.unwrap_or_else(|| self.tcx.item_name(item)),
import_name_type,
calling_convention,
span,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1575,7 +1575,7 @@ rustc_queries! {
query vtable_allocation(key: (Ty<'tcx>, Option<ty::ExistentialTraitRef<'tcx>>)) -> mir::interpret::AllocId {
desc { |tcx| "vtable const allocation for <{} as {}>",
key.0,
key.1.map(|trait_ref| format!("{trait_ref}")).unwrap_or("_".to_owned())
key.1.map(|trait_ref| format!("{trait_ref}")).unwrap_or_else(|| "_".to_owned())
}
}

Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_passes/src/check_export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ impl<'tcx> ExportableItemCollector<'tcx> {
let is_pub = visibilities.is_directly_public(def_id);

if has_attr && !is_pub {
let vis = visibilities.effective_vis(def_id).cloned().unwrap_or(
let vis = visibilities.effective_vis(def_id).cloned().unwrap_or_else(|| {
EffectiveVisibility::from_vis(Visibility::Restricted(
self.tcx.parent_module_from_def_id(def_id).to_local_def_id(),
)),
);
))
});
let vis = vis.at_level(Level::Direct);
let span = self.tcx.def_span(def_id);

Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_session/src/filesearch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,9 @@ pub fn get_or_default_sysroot() -> PathBuf {
//
// use `parent` twice to chop off the file name and then also the
// directory containing the dll
let dir = dll.parent().and_then(|p| p.parent()).ok_or(format!(
"Could not move 2 levels upper using `parent()` on {}",
dll.display()
))?;
let dir = dll.parent().and_then(|p| p.parent()).ok_or_else(|| {
format!("Could not move 2 levels upper using `parent()` on {}", dll.display())
})?;

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

from_env_args_next().unwrap_or(default_from_rustc_driver_dll().expect("Failed finding sysroot"))
from_env_args_next()
.unwrap_or_else(|| default_from_rustc_driver_dll().expect("Failed finding sysroot"))
}
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
(b'a'..=b'z')
.map(|c| format!("'{}", c as char))
.find(|candidate| !used_names.iter().any(|e| e.as_str() == candidate))
.unwrap_or("'lt".to_string())
.unwrap_or_else(|| "'lt".to_string())
};

let mut visitor = LifetimeReplaceVisitor {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_trait_selection/src/traits/normalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ impl<'a, 'b, 'tcx> AssocTypeNormalizer<'a, 'b, 'tcx> {
)
.ok()
.flatten()
.unwrap_or(proj.to_term(infcx.tcx));
.unwrap_or_else(|| proj.to_term(infcx.tcx));

PlaceholderReplacer::replace_placeholders(
infcx,
Expand Down
7 changes: 3 additions & 4 deletions compiler/rustc_ty_utils/src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -896,10 +896,9 @@ fn variant_info_for_coroutine<'tcx>(
variant_size = variant_size.max(offset + field_layout.size);
FieldInfo {
kind: FieldKind::CoroutineLocal,
name: field_name.unwrap_or(Symbol::intern(&format!(
".coroutine_field{}",
local.as_usize()
))),
name: field_name.unwrap_or_else(|| {
Symbol::intern(&format!(".coroutine_field{}", local.as_usize()))
}),
offset: offset.bytes(),
size: field_layout.size.bytes(),
align: field_layout.align.abi.bytes(),
Expand Down
3 changes: 2 additions & 1 deletion src/tools/compiletest/src/runtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2368,7 +2368,8 @@ impl<'test> TestCx<'test> {
// Real paths into the libstd/libcore
let rust_src_dir = &self.config.sysroot_base.join("lib/rustlib/src/rust");
rust_src_dir.try_exists().expect(&*format!("{} should exists", rust_src_dir));
let rust_src_dir = rust_src_dir.read_link_utf8().unwrap_or(rust_src_dir.to_path_buf());
let rust_src_dir =
rust_src_dir.read_link_utf8().unwrap_or_else(|_| rust_src_dir.to_path_buf());
normalize_path(&rust_src_dir.join("library"), "$SRC_DIR_REAL");

// eg.
Expand Down
6 changes: 3 additions & 3 deletions src/tools/compiletest/src/runtest/coverage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,9 +357,9 @@ impl<'test> TestCx<'test> {
// Add this line to the current subview.
subviews
.last_mut()
.ok_or(format!(
"unexpected subview line outside of a subview on line {line_num}"
))?
.ok_or_else(|| {
format!("unexpected subview line outside of a subview on line {line_num}")
})?
.push(line);
} else {
// This line is not part of a subview, so sort and print any
Expand Down
Loading