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

+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.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

+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.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

+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::*;
@@ -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

+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.to_option(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).to_option(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
@@ -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

+4-7
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

+1-5
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

+1-5
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
}

src/librustc/ty/mod.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -2824,11 +2824,7 @@ impl<'tcx> TyCtxt<'tcx> {
28242824
}
28252825
};
28262826

2827-
if is_associated_item {
2828-
Some(self.associated_item(def_id))
2829-
} else {
2830-
None
2831-
}
2827+
is_associated_item.to_option_with(|| self.associated_item(def_id))
28322828
}
28332829

28342830
fn associated_item_from_trait_item_ref(self,
@@ -3292,7 +3288,7 @@ fn param_env(tcx: TyCtxt<'_>, def_id: DefId) -> ParamEnv<'_> {
32923288
let unnormalized_env = ty::ParamEnv::new(
32933289
tcx.intern_predicates(&predicates),
32943290
traits::Reveal::UserFacing,
3295-
if tcx.sess.opts.debugging_opts.chalk { Some(def_id) } else { None }
3291+
tcx.sess.opts.debugging_opts.chalk.to_option(def_id),
32963292
);
32973293

32983294
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
@@ -313,13 +313,8 @@ fn connected_to_root<'tcx>(
313313
return true;
314314
}
315315

316-
visit_waiters(query, |_, successor| {
317-
if connected_to_root(successor, visited) {
318-
Some(None)
319-
} else {
320-
None
321-
}
322-
}).is_some()
316+
visit_waiters(query, |_, successor| connected_to_root(successor, visited).to_option(None))
317+
.is_some()
323318
}
324319

325320
// Deterministically pick an query from a list

src/librustc_codegen_llvm/attributes.rs

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

375375
let def_id_to_native_lib = native_libs.iter().filter_map(|lib|
376-
if let Some(id) = lib.foreign_module {
377-
Some((id, lib))
378-
} else {
379-
None
380-
}
376+
lib.foreign_module.map(|id| (id, lib))
381377
).collect::<FxHashMap<_, _>>();
382378

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

src/librustc_codegen_llvm/common.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -256,11 +256,7 @@ impl ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
256256
let (mut lo, mut hi) = (0u64, 0u64);
257257
let success = llvm::LLVMRustConstInt128Get(v, sign_ext,
258258
&mut hi, &mut lo);
259-
if success {
260-
Some(hi_lo_to_u128(lo, hi))
261-
} else {
262-
None
263-
}
259+
success.to_option(hi_lo_to_u128(lo, hi))
264260
})
265261
}
266262

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().to_option_with(|| 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).to_option(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+
.to_option_with(|| outputs.temp_path(OutputType::Object, Some(&self.name)));
74+
let bytecode = emit_bc
75+
.to_option_with(|| outputs.temp_path(OutputType::Bitcode, Some(&self.name)));
76+
let bytecode_compressed = emit_bc_compressed.to_option_with(|| {
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
@@ -628,13 +628,7 @@ fn output_contains_path(output_paths: &[PathBuf], input_path: &PathBuf) -> bool
628628
}
629629

630630
fn output_conflicts_with_dir(output_paths: &[PathBuf]) -> Option<PathBuf> {
631-
let check = |output_path: &PathBuf| {
632-
if output_path.is_dir() {
633-
Some(output_path.clone())
634-
} else {
635-
None
636-
}
637-
};
631+
let check = |output_path: &PathBuf| output_path.is_dir().to_option_with(|| output_path.clone());
638632
check_output(output_paths, check)
639633
}
640634

src/librustc_interface/queries.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,9 @@ pub(crate) struct Queries {
8787
impl Compiler {
8888
pub fn dep_graph_future(&self) -> Result<&Query<Option<DepGraphFuture>>> {
8989
self.queries.dep_graph_future.compute(|| {
90-
Ok(if self.session().opts.build_dep_graph() {
91-
Some(rustc_incremental::load_dep_graph(self.session()))
92-
} else {
93-
None
94-
})
90+
Ok(self.session().opts.build_dep_graph().to_option_with(|| {
91+
rustc_incremental::load_dep_graph(self.session())
92+
}))
9593
})
9694
}
9795

src/librustc_interface/util.rs

+2-10
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,7 @@ const STACK_SIZE: usize = 16 * 1024 * 1024;
132132
fn get_stack_size() -> Option<usize> {
133133
// FIXME: Hacks on hacks. If the env is trying to override the stack size
134134
// then *don't* set it explicitly.
135-
if env::var_os("RUST_MIN_STACK").is_none() {
136-
Some(STACK_SIZE)
137-
} else {
138-
None
139-
}
135+
env::var_os("RUST_MIN_STACK").is_none().to_option(STACK_SIZE)
140136
}
141137

142138
struct Sink(Arc<Mutex<Vec<u8>>>);
@@ -310,11 +306,7 @@ fn get_rustc_path_inner(bin_path: &str) -> Option<PathBuf> {
310306
} else {
311307
"rustc"
312308
});
313-
if candidate.exists() {
314-
Some(candidate)
315-
} else {
316-
None
317-
}
309+
candidate.exists().to_option(candidate)
318310
})
319311
.next()
320312
}

src/librustc_lint/builtin.rs

+2-10
Original file line numberDiff line numberDiff line change
@@ -1545,11 +1545,7 @@ impl ExplicitOutlivesRequirements {
15451545
match pred {
15461546
ty::Predicate::TypeOutlives(outlives) => {
15471547
let outlives = outlives.skip_binder();
1548-
if outlives.0.is_param(index) {
1549-
Some(outlives.1)
1550-
} else {
1551-
None
1552-
}
1548+
outlives.0.is_param(index).to_option(outlives.1)
15531549
}
15541550
_ => None
15551551
}
@@ -1608,11 +1604,7 @@ impl ExplicitOutlivesRequirements {
16081604
}),
16091605
_ => false,
16101606
};
1611-
if is_inferred {
1612-
Some((i, bound.span()))
1613-
} else {
1614-
None
1615-
}
1607+
is_inferred.to_option((i, bound.span()))
16161608
} else {
16171609
None
16181610
}

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)]

0 commit comments

Comments
 (0)