Skip to content

Commit d998a73

Browse files
committed
Auto merge of #65195 - varkor:to_option, r=<try>
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 857a55b + 429435c commit d998a73

File tree

55 files changed

+95
-256
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

+95
-256
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.to_option(0), None);
13+
/// assert_eq!(true.to_option(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 to_option<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.to_option_with(|| 0), None);
33+
/// assert_eq!(true.to_option_with(|| 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 to_option_with<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.to_option(0), None);
4+
assert_eq!(true.to_option(0), Some(0));
5+
assert_eq!(false.to_option_with(|| 0), None);
6+
assert_eq!(true.to_option_with(|| 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::*;
@@ -633,11 +634,7 @@ impl<'a> Parser<'a> {
633634
break;
634635
}
635636
}
636-
if found {
637-
Some(cur)
638-
} else {
639-
None
640-
}
637+
found.to_option(cur)
641638
}
642639
}
643640

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.to_option(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).to_option(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
@@ -305,11 +305,7 @@ impl<'tcx> Body<'tcx> {
305305
pub fn vars_iter<'a>(&'a self) -> impl Iterator<Item = Local> + 'a {
306306
(self.arg_count + 1..self.local_decls.len()).filter_map(move |index| {
307307
let local = Local::new(index);
308-
if self.local_decls[local].is_user_variable.is_some() {
309-
Some(local)
310-
} else {
311-
None
312-
}
308+
self.local_decls[local].is_user_variable.as_ref().map(|_| local)
313309
})
314310
}
315311

src/librustc/session/mod.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -825,11 +825,7 @@ impl Session {
825825
}
826826

827827
pub fn incr_comp_session_dir_opt(&self) -> Option<cell::Ref<'_, PathBuf>> {
828-
if self.opts.incremental.is_some() {
829-
Some(self.incr_comp_session_dir())
830-
} else {
831-
None
832-
}
828+
self.opts.incremental.as_ref().map(|_| self.incr_comp_session_dir())
833829
}
834830

835831
pub fn print_perf_stats(&self) {
@@ -1148,8 +1144,9 @@ fn build_session_(
11481144
None
11491145
}
11501146
}
1151-
}
1152-
else { None };
1147+
} else {
1148+
None
1149+
};
11531150

11541151
let host_triple = TargetTriple::from_triple(config::host_triple());
11551152
let host = Target::search(&host_triple).unwrap_or_else(|e|

src/librustc/traits/error_reporting.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -340,11 +340,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
340340
return None
341341
};
342342

343-
if tcx.has_attr(impl_def_id, sym::rustc_on_unimplemented) {
344-
Some(impl_def_id)
345-
} else {
346-
None
347-
}
343+
tcx.has_attr(impl_def_id, sym::rustc_on_unimplemented).to_option(impl_def_id)
348344
}
349345

350346
fn on_unimplemented_note(

src/librustc/ty/context.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1572,11 +1572,7 @@ impl<'tcx> TyCtxt<'tcx> {
15721572
ty::FnDef(_, _) => {
15731573
let sig = ret_ty.fn_sig(*self);
15741574
let output = self.erase_late_bound_regions(&sig.output());
1575-
if output.is_impl_trait() {
1576-
Some(output)
1577-
} else {
1578-
None
1579-
}
1575+
output.is_impl_trait().to_option(output)
15801576
}
15811577
_ => None
15821578
}

0 commit comments

Comments
 (0)