Skip to content

Commit ae1b871

Browse files
committed
Auto merge of rust-lang#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

Lines changed: 6 additions & 6 deletions
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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 2 additions & 5 deletions
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

Lines changed: 1 addition & 7 deletions
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

Lines changed: 1 addition & 5 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 5 deletions
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

Lines changed: 1 addition & 5 deletions
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

Lines changed: 2 additions & 6 deletions
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

Lines changed: 2 additions & 7 deletions
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

0 commit comments

Comments
 (0)