Skip to content

Commit 64a4f70

Browse files
authored
Rollup merge of #108090 - WaffleLapkin:if_not_now_then_when…, r=oli-obk
`if $c:expr { Some($r:expr) } else { None }` =>> `$c.then(|| $r)` Resurrection of #108079
2 parents 0412898 + 5bf6a46 commit 64a4f70

File tree

59 files changed

+165
-286
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+165
-286
lines changed

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ impl<'a> AstValidator<'a> {
271271

272272
self.session.emit_err(InvalidVisibility {
273273
span: vis.span,
274-
implied: if vis.kind.is_pub() { Some(vis.span) } else { None },
274+
implied: vis.kind.is_pub().then_some(vis.span),
275275
note,
276276
});
277277
}

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,11 +1186,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
11861186
return None;
11871187
};
11881188
debug!("checking call args for uses of inner_param: {:?}", args);
1189-
if args.contains(&Operand::Move(inner_param)) {
1190-
Some((loc, term))
1191-
} else {
1192-
None
1193-
}
1189+
args.contains(&Operand::Move(inner_param)).then_some((loc, term))
11941190
}) else {
11951191
debug!("no uses of inner_param found as a by-move call arg");
11961192
return;

compiler/rustc_borrowck/src/diagnostics/region_name.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -280,17 +280,10 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
280280

281281
debug!("give_region_a_name: error_region = {:?}", error_region);
282282
match *error_region {
283-
ty::ReEarlyBound(ebr) => {
284-
if ebr.has_name() {
285-
let span = tcx.hir().span_if_local(ebr.def_id).unwrap_or(DUMMY_SP);
286-
Some(RegionName {
287-
name: ebr.name,
288-
source: RegionNameSource::NamedEarlyBoundRegion(span),
289-
})
290-
} else {
291-
None
292-
}
293-
}
283+
ty::ReEarlyBound(ebr) => ebr.has_name().then(|| {
284+
let span = tcx.hir().span_if_local(ebr.def_id).unwrap_or(DUMMY_SP);
285+
RegionName { name: ebr.name, source: RegionNameSource::NamedEarlyBoundRegion(span) }
286+
}),
294287

295288
ty::ReStatic => {
296289
Some(RegionName { name: kw::StaticLifetime, source: RegionNameSource::Static })

compiler/rustc_borrowck/src/type_check/liveness/mod.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,11 @@ pub(super) fn generate<'mir, 'tcx>(
5050
compute_relevant_live_locals(typeck.tcx(), &free_regions, &body);
5151
let facts_enabled = use_polonius || AllFacts::enabled(typeck.tcx());
5252

53-
let polonius_drop_used = if facts_enabled {
53+
let polonius_drop_used = facts_enabled.then(|| {
5454
let mut drop_used = Vec::new();
5555
polonius::populate_access_facts(typeck, body, location_table, move_data, &mut drop_used);
56-
Some(drop_used)
57-
} else {
58-
None
59-
};
56+
drop_used
57+
});
6058

6159
trace::trace(
6260
typeck,

compiler/rustc_builtin_macros/src/deriving/debug.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,19 +135,17 @@ fn show_substructure(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>
135135
}
136136

137137
// `let names: &'static _ = &["field1", "field2"];`
138-
let names_let = if is_struct {
138+
let names_let = is_struct.then(|| {
139139
let lt_static = Some(cx.lifetime_static(span));
140140
let ty_static_ref = cx.ty_ref(span, cx.ty_infer(span), lt_static, ast::Mutability::Not);
141-
Some(cx.stmt_let_ty(
141+
cx.stmt_let_ty(
142142
span,
143143
false,
144144
Ident::new(sym::names, span),
145145
Some(ty_static_ref),
146146
cx.expr_array_ref(span, name_exprs),
147-
))
148-
} else {
149-
None
150-
};
147+
)
148+
});
151149

152150
// `let values: &[&dyn Debug] = &[&&self.field1, &&self.field2];`
153151
let path_debug = cx.path_global(span, cx.std_path(&[sym::fmt, sym::Debug]));

compiler/rustc_builtin_macros/src/deriving/generic/mod.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -942,13 +942,11 @@ impl<'a> MethodDef<'a> {
942942
let mut nonself_arg_tys = Vec::new();
943943
let span = trait_.span;
944944

945-
let explicit_self = if self.explicit_self {
945+
let explicit_self = self.explicit_self.then(|| {
946946
let (self_expr, explicit_self) = ty::get_explicit_self(cx, span);
947947
selflike_args.push(self_expr);
948-
Some(explicit_self)
949-
} else {
950-
None
951-
};
948+
explicit_self
949+
});
952950

953951
for (ty, name) in self.nonself_args.iter() {
954952
let ast_ty = ty.to_ty(cx, span, type_ident, generics);

compiler/rustc_builtin_macros/src/standard_library_imports.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ pub fn inject(
6262
// the one with the prelude.
6363
let name = names[0];
6464

65-
let root = (edition == Edition2015).then(|| kw::PathRoot);
65+
let root = (edition == Edition2015).then_some(kw::PathRoot);
6666

6767
let import_path = root
6868
.iter()

compiler/rustc_codegen_cranelift/src/driver/aot.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -248,17 +248,13 @@ fn reuse_workproduct_for_cgu(
248248
dwarf_object: None,
249249
bytecode: None,
250250
},
251-
module_global_asm: if has_global_asm {
252-
Some(CompiledModule {
253-
name: cgu.name().to_string(),
254-
kind: ModuleKind::Regular,
255-
object: Some(obj_out_global_asm),
256-
dwarf_object: None,
257-
bytecode: None,
258-
})
259-
} else {
260-
None
261-
},
251+
module_global_asm: has_global_asm.then(|| CompiledModule {
252+
name: cgu.name().to_string(),
253+
kind: ModuleKind::Regular,
254+
object: Some(obj_out_global_asm),
255+
dwarf_object: None,
256+
bytecode: None,
257+
}),
262258
existing_work_product: Some((cgu.work_product_id(), work_product)),
263259
})
264260
}

compiler/rustc_codegen_llvm/src/back/write.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -412,11 +412,7 @@ fn get_pgo_sample_use_path(config: &ModuleConfig) -> Option<CString> {
412412
}
413413

414414
fn get_instr_profile_output_path(config: &ModuleConfig) -> Option<CString> {
415-
if config.instrument_coverage {
416-
Some(CString::new("default_%m_%p.profraw").unwrap())
417-
} else {
418-
None
419-
}
415+
config.instrument_coverage.then(|| CString::new("default_%m_%p.profraw").unwrap())
420416
}
421417

422418
pub(crate) unsafe fn llvm_optimize(
@@ -451,11 +447,10 @@ pub(crate) unsafe fn llvm_optimize(
451447
None
452448
};
453449

454-
let mut llvm_profiler = if cgcx.prof.llvm_recording_enabled() {
455-
Some(LlvmSelfProfiler::new(cgcx.prof.get_self_profiler().unwrap()))
456-
} else {
457-
None
458-
};
450+
let mut llvm_profiler = cgcx
451+
.prof
452+
.llvm_recording_enabled()
453+
.then(|| LlvmSelfProfiler::new(cgcx.prof.get_self_profiler().unwrap()));
459454

460455
let llvm_selfprofiler =
461456
llvm_profiler.as_mut().map(|s| s as *mut _ as *mut c_void).unwrap_or(std::ptr::null_mut());

compiler/rustc_codegen_llvm/src/context.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -402,12 +402,8 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
402402

403403
let (llcx, llmod) = (&*llvm_module.llcx, llvm_module.llmod());
404404

405-
let coverage_cx = if tcx.sess.instrument_coverage() {
406-
let covctx = coverageinfo::CrateCoverageContext::new();
407-
Some(covctx)
408-
} else {
409-
None
410-
};
405+
let coverage_cx =
406+
tcx.sess.instrument_coverage().then(coverageinfo::CrateCoverageContext::new);
411407

412408
let dbg_cx = if tcx.sess.opts.debuginfo != DebugInfo::None {
413409
let dctx = debuginfo::CodegenUnitDebugContext::new(llmod);

0 commit comments

Comments
 (0)