Skip to content

Commit ae1b871

Browse files
committed
Auto merge of #65195 - varkor:to_option, r=Centril
Rename `bool::then_*` to `bool::to_option_*` and use where appropriate Name change following rust-lang/rfcs#2757. Also try it out throughout the compiler in places I think makes the code more readable.
2 parents 9630dbb + f1db60c commit ae1b871

File tree

55 files changed

+97
-249
lines changed

Some content is hidden

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

55 files changed

+97
-249
lines changed

src/libcore/bool.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ impl bool {
99
/// ```
1010
/// #![feature(bool_to_option)]
1111
///
12-
/// assert_eq!(false.then(0), None);
13-
/// assert_eq!(true.then(0), Some(0));
12+
/// assert_eq!(false.then_some(0), None);
13+
/// assert_eq!(true.then_some(0), Some(0));
1414
/// ```
1515
#[unstable(feature = "bool_to_option", issue = "64260")]
1616
#[inline]
17-
pub fn then<T>(self, t: T) -> Option<T> {
17+
pub fn then_some<T>(self, t: T) -> Option<T> {
1818
if self {
1919
Some(t)
2020
} else {
@@ -29,12 +29,12 @@ impl bool {
2929
/// ```
3030
/// #![feature(bool_to_option)]
3131
///
32-
/// assert_eq!(false.then_with(|| 0), None);
33-
/// assert_eq!(true.then_with(|| 0), Some(0));
32+
/// assert_eq!(false.then(|| 0), None);
33+
/// assert_eq!(true.then(|| 0), Some(0));
3434
/// ```
3535
#[unstable(feature = "bool_to_option", issue = "64260")]
3636
#[inline]
37-
pub fn then_with<T, F: FnOnce() -> T>(self, f: F) -> Option<T> {
37+
pub fn then<T, F: FnOnce() -> T>(self, f: F) -> Option<T> {
3838
if self {
3939
Some(f())
4040
} else {

src/libcore/tests/bool.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#[test]
22
fn test_bool_to_option() {
3-
assert_eq!(false.then(0), None);
4-
assert_eq!(true.then(0), Some(0));
5-
assert_eq!(false.then_with(|| 0), None);
6-
assert_eq!(true.then_with(|| 0), Some(0));
3+
assert_eq!(false.then_some(0), None);
4+
assert_eq!(true.then_some(0), Some(0));
5+
assert_eq!(false.then(|| 0), None);
6+
assert_eq!(true.then(|| 0), Some(0));
77
}

src/libfmt_macros/lib.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#![feature(nll)]
1212
#![feature(rustc_private)]
1313
#![feature(unicode_internals)]
14+
#![feature(bool_to_option)]
1415

1516
pub use Piece::*;
1617
pub use Position::*;
@@ -644,11 +645,7 @@ impl<'a> Parser<'a> {
644645
break;
645646
}
646647
}
647-
if found {
648-
Some(cur)
649-
} else {
650-
None
651-
}
648+
found.then_some(cur)
652649
}
653650
}
654651

src/librustc/hir/map/blocks.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,7 @@ impl<'a> FnLikeNode<'a> {
147147
map::Node::Expr(e) => e.is_fn_like(),
148148
_ => false
149149
};
150-
if fn_like {
151-
Some(FnLikeNode {
152-
node,
153-
})
154-
} else {
155-
None
156-
}
150+
fn_like.then_some(FnLikeNode { node })
157151
}
158152

159153
pub fn body(self) -> ast::BodyId {

src/librustc/infer/outlives/verify.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -211,11 +211,7 @@ impl<'cx, 'tcx> VerifyBoundCx<'cx, 'tcx> {
211211
(r, p)
212212
);
213213
let p_ty = p.to_ty(tcx);
214-
if compare_ty(p_ty) {
215-
Some(ty::OutlivesPredicate(p_ty, r))
216-
} else {
217-
None
218-
}
214+
compare_ty(p_ty).then_some(ty::OutlivesPredicate(p_ty, r))
219215
});
220216

221217
param_bounds

src/librustc/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/")]
3030

3131
#![feature(arbitrary_self_types)]
32+
#![feature(bool_to_option)]
3233
#![feature(box_patterns)]
3334
#![feature(box_syntax)]
3435
#![feature(const_fn)]

src/librustc/mir/mod.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -242,11 +242,7 @@ impl<'tcx> Body<'tcx> {
242242
pub fn vars_iter<'a>(&'a self) -> impl Iterator<Item = Local> + 'a {
243243
(self.arg_count + 1..self.local_decls.len()).filter_map(move |index| {
244244
let local = Local::new(index);
245-
if self.local_decls[local].is_user_variable() {
246-
Some(local)
247-
} else {
248-
None
249-
}
245+
self.local_decls[local].is_user_variable().then_some(local)
250246
})
251247
}
252248

src/librustc/traits/error_reporting.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -363,11 +363,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
363363
return None
364364
};
365365

366-
if tcx.has_attr(impl_def_id, sym::rustc_on_unimplemented) {
367-
Some(impl_def_id)
368-
} else {
369-
None
370-
}
366+
tcx.has_attr(impl_def_id, sym::rustc_on_unimplemented).then_some(impl_def_id)
371367
}
372368

373369
fn describe_generator(&self, body_id: hir::BodyId) -> Option<&'static str> {

src/librustc/ty/mod.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -2784,11 +2784,7 @@ impl<'tcx> TyCtxt<'tcx> {
27842784
}
27852785
};
27862786

2787-
if is_associated_item {
2788-
Some(self.associated_item(def_id))
2789-
} else {
2790-
None
2791-
}
2787+
is_associated_item.then(|| self.associated_item(def_id))
27922788
}
27932789

27942790
fn associated_item_from_trait_item_ref(self,
@@ -3253,7 +3249,7 @@ fn param_env(tcx: TyCtxt<'_>, def_id: DefId) -> ParamEnv<'_> {
32533249
let unnormalized_env = ty::ParamEnv::new(
32543250
tcx.intern_predicates(&predicates),
32553251
traits::Reveal::UserFacing,
3256-
if tcx.sess.opts.debugging_opts.chalk { Some(def_id) } else { None }
3252+
tcx.sess.opts.debugging_opts.chalk.then_some(def_id),
32573253
);
32583254

32593255
let body_id = tcx.hir().as_local_hir_id(def_id).map_or(hir::DUMMY_HIR_ID, |id| {

src/librustc/ty/query/job.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -303,13 +303,8 @@ fn connected_to_root<'tcx>(
303303
return true;
304304
}
305305

306-
visit_waiters(query, |_, successor| {
307-
if connected_to_root(successor, visited) {
308-
Some(None)
309-
} else {
310-
None
311-
}
312-
}).is_some()
306+
visit_waiters(query, |_, successor| connected_to_root(successor, visited).then_some(None))
307+
.is_some()
313308
}
314309

315310
// Deterministically pick an query from a list

src/librustc_codegen_llvm/attributes.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -375,11 +375,7 @@ pub fn provide_extern(providers: &mut Providers<'_>) {
375375
let native_libs = tcx.native_libraries(cnum);
376376

377377
let def_id_to_native_lib = native_libs.iter().filter_map(|lib|
378-
if let Some(id) = lib.foreign_module {
379-
Some((id, lib))
380-
} else {
381-
None
382-
}
378+
lib.foreign_module.map(|id| (id, lib))
383379
).collect::<FxHashMap<_, _>>();
384380

385381
let mut ret = FxHashMap::default();

src/librustc_codegen_llvm/common.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -245,11 +245,7 @@ impl ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
245245
let (mut lo, mut hi) = (0u64, 0u64);
246246
let success = llvm::LLVMRustConstInt128Get(v, sign_ext,
247247
&mut hi, &mut lo);
248-
if success {
249-
Some(hi_lo_to_u128(lo, hi))
250-
} else {
251-
None
252-
}
248+
success.then_some(hi_lo_to_u128(lo, hi))
253249
})
254250
}
255251

src/librustc_codegen_llvm/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
77
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/")]
88

9+
#![feature(bool_to_option)]
910
#![feature(box_patterns)]
1011
#![feature(box_syntax)]
1112
#![feature(const_cstr_unchecked)]

src/librustc_codegen_ssa/back/rpath.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,7 @@ fn path_relative_from(path: &Path, base: &Path) -> Option<PathBuf> {
119119
use std::path::Component;
120120

121121
if path.is_absolute() != base.is_absolute() {
122-
if path.is_absolute() {
123-
Some(PathBuf::from(path))
124-
} else {
125-
None
126-
}
122+
path.is_absolute().then(|| PathBuf::from(path))
127123
} else {
128124
let mut ita = path.components();
129125
let mut itb = base.components();

src/librustc_codegen_ssa/back/symbol_export.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,7 @@ fn reachable_non_generics_provider(
8585
match tcx.hir().get(hir_id) {
8686
Node::ForeignItem(..) => {
8787
let def_id = tcx.hir().local_def_id(hir_id);
88-
if tcx.is_statically_included_foreign_item(def_id) {
89-
Some(def_id)
90-
} else {
91-
None
92-
}
88+
tcx.is_statically_included_foreign_item(def_id).then_some(def_id)
9389
}
9490

9591
// Only consider nodes that actually have exported symbols.

src/librustc_codegen_ssa/lib.rs

+9-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/")]
22

3+
#![feature(bool_to_option)]
34
#![feature(box_patterns)]
45
#![feature(box_syntax)]
56
#![feature(core_intrinsics)]
@@ -68,22 +69,14 @@ impl<M> ModuleCodegen<M> {
6869
emit_bc: bool,
6970
emit_bc_compressed: bool,
7071
outputs: &OutputFilenames) -> CompiledModule {
71-
let object = if emit_obj {
72-
Some(outputs.temp_path(OutputType::Object, Some(&self.name)))
73-
} else {
74-
None
75-
};
76-
let bytecode = if emit_bc {
77-
Some(outputs.temp_path(OutputType::Bitcode, Some(&self.name)))
78-
} else {
79-
None
80-
};
81-
let bytecode_compressed = if emit_bc_compressed {
82-
Some(outputs.temp_path(OutputType::Bitcode, Some(&self.name))
83-
.with_extension(RLIB_BYTECODE_EXTENSION))
84-
} else {
85-
None
86-
};
72+
let object = emit_obj
73+
.then(|| outputs.temp_path(OutputType::Object, Some(&self.name)));
74+
let bytecode = emit_bc
75+
.then(|| outputs.temp_path(OutputType::Bitcode, Some(&self.name)));
76+
let bytecode_compressed = emit_bc_compressed.then(|| {
77+
outputs.temp_path(OutputType::Bitcode, Some(&self.name))
78+
.with_extension(RLIB_BYTECODE_EXTENSION)
79+
});
8780

8881
CompiledModule {
8982
name: self.name.clone(),

src/librustc_interface/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![feature(bool_to_option)]
12
#![feature(box_syntax)]
23
#![feature(set_stdio)]
34
#![feature(nll)]

src/librustc_interface/passes.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -547,13 +547,7 @@ fn output_contains_path(output_paths: &[PathBuf], input_path: &PathBuf) -> bool
547547
}
548548

549549
fn output_conflicts_with_dir(output_paths: &[PathBuf]) -> Option<PathBuf> {
550-
let check = |output_path: &PathBuf| {
551-
if output_path.is_dir() {
552-
Some(output_path.clone())
553-
} else {
554-
None
555-
}
556-
};
550+
let check = |output_path: &PathBuf| output_path.is_dir().then(|| output_path.clone());
557551
check_output(output_paths, check)
558552
}
559553

src/librustc_interface/queries.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,9 @@ impl<'tcx> Queries<'tcx> {
117117

118118
pub fn dep_graph_future(&self) -> Result<&Query<Option<DepGraphFuture>>> {
119119
self.dep_graph_future.compute(|| {
120-
Ok(if self.session().opts.build_dep_graph() {
121-
Some(rustc_incremental::load_dep_graph(self.session()))
122-
} else {
123-
None
124-
})
120+
Ok(self.session().opts.build_dep_graph().then(|| {
121+
rustc_incremental::load_dep_graph(self.session())
122+
}))
125123
})
126124
}
127125

src/librustc_interface/util.rs

+2-10
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,7 @@ const STACK_SIZE: usize = 16 * 1024 * 1024;
107107
fn get_stack_size() -> Option<usize> {
108108
// FIXME: Hacks on hacks. If the env is trying to override the stack size
109109
// then *don't* set it explicitly.
110-
if env::var_os("RUST_MIN_STACK").is_none() {
111-
Some(STACK_SIZE)
112-
} else {
113-
None
114-
}
110+
env::var_os("RUST_MIN_STACK").is_none().then_some(STACK_SIZE)
115111
}
116112

117113
struct Sink(Arc<Mutex<Vec<u8>>>);
@@ -285,11 +281,7 @@ fn get_rustc_path_inner(bin_path: &str) -> Option<PathBuf> {
285281
} else {
286282
"rustc"
287283
});
288-
if candidate.exists() {
289-
Some(candidate)
290-
} else {
291-
None
292-
}
284+
candidate.exists().then_some(candidate)
293285
})
294286
.next()
295287
}

src/librustc_lint/builtin.rs

+2-10
Original file line numberDiff line numberDiff line change
@@ -1491,11 +1491,7 @@ impl ExplicitOutlivesRequirements {
14911491
match pred {
14921492
ty::Predicate::TypeOutlives(outlives) => {
14931493
let outlives = outlives.skip_binder();
1494-
if outlives.0.is_param(index) {
1495-
Some(outlives.1)
1496-
} else {
1497-
None
1498-
}
1494+
outlives.0.is_param(index).then_some(outlives.1)
14991495
}
15001496
_ => None
15011497
}
@@ -1554,11 +1550,7 @@ impl ExplicitOutlivesRequirements {
15541550
}),
15551551
_ => false,
15561552
};
1557-
if is_inferred {
1558-
Some((i, bound.span()))
1559-
} else {
1560-
None
1561-
}
1553+
is_inferred.then_some((i, bound.span()))
15621554
} else {
15631555
None
15641556
}

src/librustc_lint/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/")]
1313

1414
#![cfg_attr(test, feature(test))]
15+
#![feature(bool_to_option)]
1516
#![feature(box_patterns)]
1617
#![feature(box_syntax)]
1718
#![feature(nll)]

src/librustc_metadata/creader.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -802,11 +802,8 @@ impl<'a> CrateLoader<'a> {
802802
// First up we check for global allocators. Look at the crate graph here
803803
// and see what's a global allocator, including if we ourselves are a
804804
// global allocator.
805-
let mut global_allocator = if self.cstore.has_global_allocator {
806-
Some(Symbol::intern("this crate"))
807-
} else {
808-
None
809-
};
805+
let mut global_allocator = self.cstore.has_global_allocator
806+
.then(|| Symbol::intern("this crate"));
810807
self.cstore.iter_crate_data(|_, data| {
811808
if !data.has_global_allocator() {
812809
return

src/librustc_metadata/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/")]
22

3+
#![feature(bool_to_option)]
34
#![feature(box_patterns)]
45
#![feature(core_intrinsics)]
56
#![feature(crate_visibility_modifier)]

src/librustc_mir/borrow_check/nll/mod.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,7 @@ pub(in crate::borrow_check) fn compute_regions<'cx, 'tcx>(
173173
Option<Rc<Output<RegionVid, BorrowIndex, LocationIndex, Local, MovePathIndex>>>,
174174
Option<ClosureRegionRequirements<'tcx>>,
175175
) {
176-
let mut all_facts = if AllFacts::enabled(infcx.tcx) {
177-
Some(AllFacts::default())
178-
} else {
179-
None
180-
};
176+
let mut all_facts = AllFacts::enabled(infcx.tcx).then_some(AllFacts::default());
181177

182178
let universal_regions = Rc::new(universal_regions);
183179

0 commit comments

Comments
 (0)