diff --git a/src/libcore/bool.rs b/src/libcore/bool.rs index 617bdd238f4c6..aa1ba7affc167 100644 --- a/src/libcore/bool.rs +++ b/src/libcore/bool.rs @@ -9,12 +9,12 @@ impl bool { /// ``` /// #![feature(bool_to_option)] /// - /// assert_eq!(false.then(0), None); - /// assert_eq!(true.then(0), Some(0)); + /// assert_eq!(false.to_option(0), None); + /// assert_eq!(true.to_option(0), Some(0)); /// ``` #[unstable(feature = "bool_to_option", issue = "64260")] #[inline] - pub fn then(self, t: T) -> Option { + pub fn to_option(self, t: T) -> Option { if self { Some(t) } else { @@ -29,12 +29,12 @@ impl bool { /// ``` /// #![feature(bool_to_option)] /// - /// assert_eq!(false.then_with(|| 0), None); - /// assert_eq!(true.then_with(|| 0), Some(0)); + /// assert_eq!(false.to_option_with(|| 0), None); + /// assert_eq!(true.to_option_with(|| 0), Some(0)); /// ``` #[unstable(feature = "bool_to_option", issue = "64260")] #[inline] - pub fn then_with T>(self, f: F) -> Option { + pub fn to_option_with T>(self, f: F) -> Option { if self { Some(f()) } else { diff --git a/src/libcore/tests/bool.rs b/src/libcore/tests/bool.rs index 0f1e6e89451e9..8ec151b63ffaa 100644 --- a/src/libcore/tests/bool.rs +++ b/src/libcore/tests/bool.rs @@ -1,7 +1,7 @@ #[test] fn test_bool_to_option() { - assert_eq!(false.then(0), None); - assert_eq!(true.then(0), Some(0)); - assert_eq!(false.then_with(|| 0), None); - assert_eq!(true.then_with(|| 0), Some(0)); + assert_eq!(false.to_option(0), None); + assert_eq!(true.to_option(0), Some(0)); + assert_eq!(false.to_option_with(|| 0), None); + assert_eq!(true.to_option_with(|| 0), Some(0)); } diff --git a/src/libfmt_macros/lib.rs b/src/libfmt_macros/lib.rs index d22420e76dcd4..685b6fdb34b1f 100644 --- a/src/libfmt_macros/lib.rs +++ b/src/libfmt_macros/lib.rs @@ -11,6 +11,7 @@ #![feature(nll)] #![feature(rustc_private)] #![feature(unicode_internals)] +#![feature(bool_to_option)] pub use Piece::*; pub use Position::*; @@ -633,11 +634,7 @@ impl<'a> Parser<'a> { break; } } - if found { - Some(cur) - } else { - None - } + found.to_option(cur) } } diff --git a/src/librustc/hir/map/blocks.rs b/src/librustc/hir/map/blocks.rs index f670d5abe85e4..87e6afb499ce7 100644 --- a/src/librustc/hir/map/blocks.rs +++ b/src/librustc/hir/map/blocks.rs @@ -147,13 +147,7 @@ impl<'a> FnLikeNode<'a> { map::Node::Expr(e) => e.is_fn_like(), _ => false }; - if fn_like { - Some(FnLikeNode { - node, - }) - } else { - None - } + fn_like.to_option(FnLikeNode { node }) } pub fn body(self) -> ast::BodyId { diff --git a/src/librustc/infer/outlives/verify.rs b/src/librustc/infer/outlives/verify.rs index 3110b027c5bbe..aebf93d85c5f1 100644 --- a/src/librustc/infer/outlives/verify.rs +++ b/src/librustc/infer/outlives/verify.rs @@ -211,11 +211,7 @@ impl<'cx, 'tcx> VerifyBoundCx<'cx, 'tcx> { (r, p) ); let p_ty = p.to_ty(tcx); - if compare_ty(p_ty) { - Some(ty::OutlivesPredicate(p_ty, r)) - } else { - None - } + compare_ty(p_ty).to_option(ty::OutlivesPredicate(p_ty, r)) }); param_bounds diff --git a/src/librustc/lib.rs b/src/librustc/lib.rs index 8943fc342c023..9f56d84ecde9a 100644 --- a/src/librustc/lib.rs +++ b/src/librustc/lib.rs @@ -29,6 +29,7 @@ #![doc(html_root_url = "https://doc.rust-lang.org/nightly/")] #![feature(arbitrary_self_types)] +#![feature(bool_to_option)] #![feature(box_patterns)] #![feature(box_syntax)] #![feature(const_fn)] diff --git a/src/librustc/mir/mod.rs b/src/librustc/mir/mod.rs index 9ac1465cb0ba9..b54043cf70ad0 100644 --- a/src/librustc/mir/mod.rs +++ b/src/librustc/mir/mod.rs @@ -305,11 +305,7 @@ impl<'tcx> Body<'tcx> { pub fn vars_iter<'a>(&'a self) -> impl Iterator + 'a { (self.arg_count + 1..self.local_decls.len()).filter_map(move |index| { let local = Local::new(index); - if self.local_decls[local].is_user_variable.is_some() { - Some(local) - } else { - None - } + self.local_decls[local].is_user_variable.as_ref().map(|_| local) }) } diff --git a/src/librustc/session/mod.rs b/src/librustc/session/mod.rs index b65bf2230b39d..17100cccb0a05 100644 --- a/src/librustc/session/mod.rs +++ b/src/librustc/session/mod.rs @@ -825,11 +825,7 @@ impl Session { } pub fn incr_comp_session_dir_opt(&self) -> Option> { - if self.opts.incremental.is_some() { - Some(self.incr_comp_session_dir()) - } else { - None - } + self.opts.incremental.as_ref().map(|_| self.incr_comp_session_dir()) } pub fn print_perf_stats(&self) { @@ -1148,8 +1144,9 @@ fn build_session_( None } } - } - else { None }; + } else { + None + }; let host_triple = TargetTriple::from_triple(config::host_triple()); let host = Target::search(&host_triple).unwrap_or_else(|e| diff --git a/src/librustc/traits/error_reporting.rs b/src/librustc/traits/error_reporting.rs index daa4a215a238a..4c718eccb2d30 100644 --- a/src/librustc/traits/error_reporting.rs +++ b/src/librustc/traits/error_reporting.rs @@ -340,11 +340,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> { return None }; - if tcx.has_attr(impl_def_id, sym::rustc_on_unimplemented) { - Some(impl_def_id) - } else { - None - } + tcx.has_attr(impl_def_id, sym::rustc_on_unimplemented).to_option(impl_def_id) } fn on_unimplemented_note( diff --git a/src/librustc/ty/context.rs b/src/librustc/ty/context.rs index 665d4c2d0696a..651d6b9216363 100644 --- a/src/librustc/ty/context.rs +++ b/src/librustc/ty/context.rs @@ -1572,11 +1572,7 @@ impl<'tcx> TyCtxt<'tcx> { ty::FnDef(_, _) => { let sig = ret_ty.fn_sig(*self); let output = self.erase_late_bound_regions(&sig.output()); - if output.is_impl_trait() { - Some(output) - } else { - None - } + output.is_impl_trait().to_option(output) } _ => None } diff --git a/src/librustc/ty/mod.rs b/src/librustc/ty/mod.rs index d46ab3769ad55..77662346e0e30 100644 --- a/src/librustc/ty/mod.rs +++ b/src/librustc/ty/mod.rs @@ -2824,11 +2824,7 @@ impl<'tcx> TyCtxt<'tcx> { } }; - if is_associated_item { - Some(self.associated_item(def_id)) - } else { - None - } + is_associated_item.to_option_with(|| self.associated_item(def_id)) } fn associated_item_from_trait_item_ref(self, @@ -3292,7 +3288,7 @@ fn param_env(tcx: TyCtxt<'_>, def_id: DefId) -> ParamEnv<'_> { let unnormalized_env = ty::ParamEnv::new( tcx.intern_predicates(&predicates), traits::Reveal::UserFacing, - if tcx.sess.opts.debugging_opts.chalk { Some(def_id) } else { None } + tcx.sess.opts.debugging_opts.chalk.to_option(def_id), ); let body_id = tcx.hir().as_local_hir_id(def_id).map_or(hir::DUMMY_HIR_ID, |id| { diff --git a/src/librustc/ty/query/job.rs b/src/librustc/ty/query/job.rs index 391ea762a083b..8edbc13971baf 100644 --- a/src/librustc/ty/query/job.rs +++ b/src/librustc/ty/query/job.rs @@ -313,13 +313,8 @@ fn connected_to_root<'tcx>( return true; } - visit_waiters(query, |_, successor| { - if connected_to_root(successor, visited) { - Some(None) - } else { - None - } - }).is_some() + visit_waiters(query, |_, successor| connected_to_root(successor, visited).to_option(None)) + .is_some() } // Deterministically pick an query from a list diff --git a/src/librustc_codegen_llvm/attributes.rs b/src/librustc_codegen_llvm/attributes.rs index 6a36a4a50cbf3..1025207d2cae8 100644 --- a/src/librustc_codegen_llvm/attributes.rs +++ b/src/librustc_codegen_llvm/attributes.rs @@ -373,11 +373,7 @@ pub fn provide_extern(providers: &mut Providers<'_>) { let native_libs = tcx.native_libraries(cnum); let def_id_to_native_lib = native_libs.iter().filter_map(|lib| - if let Some(id) = lib.foreign_module { - Some((id, lib)) - } else { - None - } + lib.foreign_module.map(|id| (id, lib)) ).collect::>(); let mut ret = FxHashMap::default(); diff --git a/src/librustc_codegen_llvm/common.rs b/src/librustc_codegen_llvm/common.rs index a1a5232d58832..bae76f935c5ff 100644 --- a/src/librustc_codegen_llvm/common.rs +++ b/src/librustc_codegen_llvm/common.rs @@ -256,11 +256,7 @@ impl ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> { let (mut lo, mut hi) = (0u64, 0u64); let success = llvm::LLVMRustConstInt128Get(v, sign_ext, &mut hi, &mut lo); - if success { - Some(hi_lo_to_u128(lo, hi)) - } else { - None - } + success.to_option(hi_lo_to_u128(lo, hi)) }) } diff --git a/src/librustc_codegen_llvm/lib.rs b/src/librustc_codegen_llvm/lib.rs index 9b55bef0c514d..3f38ec701e870 100644 --- a/src/librustc_codegen_llvm/lib.rs +++ b/src/librustc_codegen_llvm/lib.rs @@ -6,6 +6,7 @@ #![doc(html_root_url = "https://doc.rust-lang.org/nightly/")] +#![feature(bool_to_option)] #![feature(box_patterns)] #![feature(box_syntax)] #![feature(const_cstr_unchecked)] diff --git a/src/librustc_codegen_ssa/back/rpath.rs b/src/librustc_codegen_ssa/back/rpath.rs index e27cb6d8dda89..c61f16da26414 100644 --- a/src/librustc_codegen_ssa/back/rpath.rs +++ b/src/librustc_codegen_ssa/back/rpath.rs @@ -119,11 +119,7 @@ fn path_relative_from(path: &Path, base: &Path) -> Option { use std::path::Component; if path.is_absolute() != base.is_absolute() { - if path.is_absolute() { - Some(PathBuf::from(path)) - } else { - None - } + path.is_absolute().to_option_with(|| PathBuf::from(path)) } else { let mut ita = path.components(); let mut itb = base.components(); diff --git a/src/librustc_codegen_ssa/back/symbol_export.rs b/src/librustc_codegen_ssa/back/symbol_export.rs index d866a10f06935..f9c68d18c3527 100644 --- a/src/librustc_codegen_ssa/back/symbol_export.rs +++ b/src/librustc_codegen_ssa/back/symbol_export.rs @@ -85,11 +85,7 @@ fn reachable_non_generics_provider( match tcx.hir().get(hir_id) { Node::ForeignItem(..) => { let def_id = tcx.hir().local_def_id(hir_id); - if tcx.is_statically_included_foreign_item(def_id) { - Some(def_id) - } else { - None - } + tcx.is_statically_included_foreign_item(def_id).to_option(def_id) } // Only consider nodes that actually have exported symbols. diff --git a/src/librustc_codegen_ssa/lib.rs b/src/librustc_codegen_ssa/lib.rs index 0221a04b04518..e1b8cabdff8ec 100644 --- a/src/librustc_codegen_ssa/lib.rs +++ b/src/librustc_codegen_ssa/lib.rs @@ -1,5 +1,6 @@ #![doc(html_root_url = "https://doc.rust-lang.org/nightly/")] +#![feature(bool_to_option)] #![feature(box_patterns)] #![feature(box_syntax)] #![feature(core_intrinsics)] @@ -68,22 +69,14 @@ impl ModuleCodegen { emit_bc: bool, emit_bc_compressed: bool, outputs: &OutputFilenames) -> CompiledModule { - let object = if emit_obj { - Some(outputs.temp_path(OutputType::Object, Some(&self.name))) - } else { - None - }; - let bytecode = if emit_bc { - Some(outputs.temp_path(OutputType::Bitcode, Some(&self.name))) - } else { - None - }; - let bytecode_compressed = if emit_bc_compressed { - Some(outputs.temp_path(OutputType::Bitcode, Some(&self.name)) - .with_extension(RLIB_BYTECODE_EXTENSION)) - } else { - None - }; + let object = emit_obj + .to_option_with(|| outputs.temp_path(OutputType::Object, Some(&self.name))); + let bytecode = emit_bc + .to_option_with(|| outputs.temp_path(OutputType::Bitcode, Some(&self.name))); + let bytecode_compressed = emit_bc_compressed.to_option_with(|| { + outputs.temp_path(OutputType::Bitcode, Some(&self.name)) + .with_extension(RLIB_BYTECODE_EXTENSION) + }); CompiledModule { name: self.name.clone(), diff --git a/src/librustc_interface/lib.rs b/src/librustc_interface/lib.rs index 6be36e9b9001b..df150f3262a85 100644 --- a/src/librustc_interface/lib.rs +++ b/src/librustc_interface/lib.rs @@ -1,3 +1,4 @@ +#![feature(bool_to_option)] #![feature(box_syntax)] #![feature(set_stdio)] #![feature(nll)] diff --git a/src/librustc_interface/passes.rs b/src/librustc_interface/passes.rs index 89de5714695de..8b090ffe720f7 100644 --- a/src/librustc_interface/passes.rs +++ b/src/librustc_interface/passes.rs @@ -628,13 +628,7 @@ fn output_contains_path(output_paths: &[PathBuf], input_path: &PathBuf) -> bool } fn output_conflicts_with_dir(output_paths: &[PathBuf]) -> Option { - let check = |output_path: &PathBuf| { - if output_path.is_dir() { - Some(output_path.clone()) - } else { - None - } - }; + let check = |output_path: &PathBuf| output_path.is_dir().to_option_with(|| output_path.clone()); check_output(output_paths, check) } diff --git a/src/librustc_interface/queries.rs b/src/librustc_interface/queries.rs index cd72dc9453c7e..683d6c67f7bfa 100644 --- a/src/librustc_interface/queries.rs +++ b/src/librustc_interface/queries.rs @@ -87,11 +87,9 @@ pub(crate) struct Queries { impl Compiler { pub fn dep_graph_future(&self) -> Result<&Query>> { self.queries.dep_graph_future.compute(|| { - Ok(if self.session().opts.build_dep_graph() { - Some(rustc_incremental::load_dep_graph(self.session())) - } else { - None - }) + Ok(self.session().opts.build_dep_graph().to_option_with(|| { + rustc_incremental::load_dep_graph(self.session()) + })) }) } diff --git a/src/librustc_interface/util.rs b/src/librustc_interface/util.rs index 0c272f0c4563b..deae60ca63002 100644 --- a/src/librustc_interface/util.rs +++ b/src/librustc_interface/util.rs @@ -132,11 +132,7 @@ const STACK_SIZE: usize = 16 * 1024 * 1024; fn get_stack_size() -> Option { // FIXME: Hacks on hacks. If the env is trying to override the stack size // then *don't* set it explicitly. - if env::var_os("RUST_MIN_STACK").is_none() { - Some(STACK_SIZE) - } else { - None - } + env::var_os("RUST_MIN_STACK").is_none().to_option(STACK_SIZE) } struct Sink(Arc>>); @@ -310,11 +306,7 @@ fn get_rustc_path_inner(bin_path: &str) -> Option { } else { "rustc" }); - if candidate.exists() { - Some(candidate) - } else { - None - } + candidate.exists().to_option(candidate) }) .next() } diff --git a/src/librustc_lint/builtin.rs b/src/librustc_lint/builtin.rs index 08f6f43ab0cff..5fef07dfeb53b 100644 --- a/src/librustc_lint/builtin.rs +++ b/src/librustc_lint/builtin.rs @@ -1545,11 +1545,7 @@ impl ExplicitOutlivesRequirements { match pred { ty::Predicate::TypeOutlives(outlives) => { let outlives = outlives.skip_binder(); - if outlives.0.is_param(index) { - Some(outlives.1) - } else { - None - } + outlives.0.is_param(index).to_option(outlives.1) } _ => None } @@ -1608,11 +1604,7 @@ impl ExplicitOutlivesRequirements { }), _ => false, }; - if is_inferred { - Some((i, bound.span())) - } else { - None - } + is_inferred.to_option((i, bound.span())) } else { None } diff --git a/src/librustc_lint/lib.rs b/src/librustc_lint/lib.rs index e3860e229d6b5..aa7d59f5006ac 100644 --- a/src/librustc_lint/lib.rs +++ b/src/librustc_lint/lib.rs @@ -12,6 +12,7 @@ #![doc(html_root_url = "https://doc.rust-lang.org/nightly/")] #![cfg_attr(test, feature(test))] +#![feature(bool_to_option)] #![feature(box_patterns)] #![feature(box_syntax)] #![feature(nll)] diff --git a/src/librustc_metadata/creader.rs b/src/librustc_metadata/creader.rs index 7412e8a2cb9bd..9e6b04e2d67c2 100644 --- a/src/librustc_metadata/creader.rs +++ b/src/librustc_metadata/creader.rs @@ -868,11 +868,7 @@ impl<'a> CrateLoader<'a> { // First up we check for global allocators. Look at the crate graph here // and see what's a global allocator, including if we ourselves are a // global allocator. - let mut global_allocator = if has_global_allocator { - Some(None) - } else { - None - }; + let mut global_allocator = has_global_allocator.to_option(None); self.cstore.iter_crate_data(|_, data| { if !data.root.has_global_allocator { return diff --git a/src/librustc_metadata/lib.rs b/src/librustc_metadata/lib.rs index 291ee23ff7262..55e102abcbf86 100644 --- a/src/librustc_metadata/lib.rs +++ b/src/librustc_metadata/lib.rs @@ -1,5 +1,6 @@ #![doc(html_root_url = "https://doc.rust-lang.org/nightly/")] +#![feature(bool_to_option)] #![feature(box_patterns)] #![feature(core_intrinsics)] #![feature(crate_visibility_modifier)] diff --git a/src/librustc_mir/borrow_check/nll/mod.rs b/src/librustc_mir/borrow_check/nll/mod.rs index b2e5751b902e7..1aaec296ca756 100644 --- a/src/librustc_mir/borrow_check/nll/mod.rs +++ b/src/librustc_mir/borrow_check/nll/mod.rs @@ -170,11 +170,7 @@ pub(in crate::borrow_check) fn compute_regions<'cx, 'tcx>( Option>>, Option>, ) { - let mut all_facts = if AllFacts::enabled(infcx.tcx) { - Some(AllFacts::default()) - } else { - None - }; + let mut all_facts = AllFacts::enabled(infcx.tcx).to_option(AllFacts::default()); let universal_regions = Rc::new(universal_regions); diff --git a/src/librustc_mir/borrow_check/nll/region_infer/mod.rs b/src/librustc_mir/borrow_check/nll/region_infer/mod.rs index dbb810db555b4..02d8ec2d2a33e 100644 --- a/src/librustc_mir/borrow_check/nll/region_infer/mod.rs +++ b/src/librustc_mir/borrow_check/nll/region_infer/mod.rs @@ -482,7 +482,7 @@ impl<'tcx> RegionInferenceContext<'tcx> { // functions below, which will trigger them to report errors // eagerly. let mut outlives_requirements = - if infcx.tcx.is_closure(mir_def_id) { Some(vec![]) } else { None }; + infcx.tcx.is_closure(mir_def_id).to_option_with(|| vec![]); self.check_type_tests( infcx, @@ -697,14 +697,11 @@ impl<'tcx> RegionInferenceContext<'tcx> { let min = |r1: ty::RegionVid, r2: ty::RegionVid| -> Option { let r1_outlives_r2 = self.universal_region_relations.outlives(r1, r2); let r2_outlives_r1 = self.universal_region_relations.outlives(r2, r1); - if r1_outlives_r2 && r2_outlives_r1 { - Some(r1.min(r2)) - } else if r1_outlives_r2 { - Some(r2) - } else if r2_outlives_r1 { - Some(r1) - } else { - None + match (r1_outlives_r2, r2_outlives_r1) { + (true, true) => Some(r1.min(r2)), + (true, false) => Some(r2), + (false, true) => Some(r1), + (false, false) => None, } }; let mut min_choice = choice_regions[0]; diff --git a/src/librustc_mir/build/expr/as_rvalue.rs b/src/librustc_mir/build/expr/as_rvalue.rs index 87d95a751534d..2fdd599f6ca48 100644 --- a/src/librustc_mir/build/expr/as_rvalue.rs +++ b/src/librustc_mir/build/expr/as_rvalue.rs @@ -266,11 +266,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { } => { // see (*) above let is_union = adt_def.is_union(); - let active_field_index = if is_union { - Some(fields[0].name.index()) - } else { - None - }; + let active_field_index = is_union.to_option_with(|| fields[0].name.index()); // first process the set of fields that were provided // (evaluating them in order given by user) diff --git a/src/librustc_mir/build/matches/test.rs b/src/librustc_mir/build/matches/test.rs index a04c989e6e0de..9395b68152ff6 100644 --- a/src/librustc_mir/build/matches/test.rs +++ b/src/librustc_mir/build/matches/test.rs @@ -674,7 +674,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { } })(); - if no_overlap == Some(true) { + if let Some(true) = no_overlap { // Testing range does not overlap with pattern range, // so the pattern can be matched only if this test fails. Some(1) @@ -684,7 +684,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { } (&TestKind::Range(range), &PatKind::Constant { value }) => { - if self.const_range_contains(range, value) == Some(false) { + if let Some(false) = self.const_range_contains(range, value) { // `value` is not contained in the testing range, // so `value` can be matched only if this test fails. Some(1) diff --git a/src/librustc_mir/hair/pattern/mod.rs b/src/librustc_mir/hair/pattern/mod.rs index 7e17162dfb3ef..a04641bc8b304 100644 --- a/src/librustc_mir/hair/pattern/mod.rs +++ b/src/librustc_mir/hair/pattern/mod.rs @@ -1478,13 +1478,7 @@ pub fn compare_const_vals<'tcx>( ) -> Option { trace!("compare_const_vals: {:?}, {:?}", a, b); - let from_bool = |v: bool| { - if v { - Some(Ordering::Equal) - } else { - None - } - }; + let from_bool = |v: bool| v.to_option(Ordering::Equal); let fallback = || from_bool(a == b); diff --git a/src/librustc_mir/interpret/memory.rs b/src/librustc_mir/interpret/memory.rs index 924474c53175c..762872014040c 100644 --- a/src/librustc_mir/interpret/memory.rs +++ b/src/librustc_mir/interpret/memory.rs @@ -313,7 +313,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> { size: Size, align: Align, ) -> InterpResult<'tcx, Option>> { - let align = if M::CHECK_ALIGN { Some(align) } else { None }; + let align = M::CHECK_ALIGN.to_option(align); self.check_ptr_access_align(sptr, size, align) } diff --git a/src/librustc_mir/interpret/step.rs b/src/librustc_mir/interpret/step.rs index daca7a25787ca..68cd91c845de9 100644 --- a/src/librustc_mir/interpret/step.rs +++ b/src/librustc_mir/interpret/step.rs @@ -148,9 +148,9 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { } BinaryOp(bin_op, ref left, ref right) => { - let layout = if binop_left_homogeneous(bin_op) { Some(dest.layout) } else { None }; + let layout = binop_left_homogeneous(bin_op).to_option(dest.layout); let left = self.read_immediate(self.eval_operand(left, layout)?)?; - let layout = if binop_right_homogeneous(bin_op) { Some(left.layout) } else { None }; + let layout = binop_right_homogeneous(bin_op).to_option(left.layout); let right = self.read_immediate(self.eval_operand(right, layout)?)?; self.binop_ignore_overflow( bin_op, @@ -163,7 +163,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { CheckedBinaryOp(bin_op, ref left, ref right) => { // Due to the extra boolean in the result, we can never reuse the `dest.layout`. let left = self.read_immediate(self.eval_operand(left, None)?)?; - let layout = if binop_right_homogeneous(bin_op) { Some(left.layout) } else { None }; + let layout = binop_right_homogeneous(bin_op).to_option(left.layout); let right = self.read_immediate(self.eval_operand(right, layout)?)?; self.binop_with_overflow( bin_op, diff --git a/src/librustc_mir/lib.rs b/src/librustc_mir/lib.rs index 98d5487870a4d..9c38c76cc21b6 100644 --- a/src/librustc_mir/lib.rs +++ b/src/librustc_mir/lib.rs @@ -8,6 +8,7 @@ Rust MIR: a lowered representation of Rust. Also: an experiment! #![feature(in_band_lifetimes)] #![feature(inner_deref)] #![feature(slice_patterns)] +#![feature(bool_to_option)] #![feature(box_patterns)] #![feature(box_syntax)] #![feature(crate_visibility_modifier)] diff --git a/src/librustc_mir/monomorphize/partitioning.rs b/src/librustc_mir/monomorphize/partitioning.rs index b9d38028b72a8..b5eed74995133 100644 --- a/src/librustc_mir/monomorphize/partitioning.rs +++ b/src/librustc_mir/monomorphize/partitioning.rs @@ -764,11 +764,7 @@ fn compute_codegen_unit_name( .iter() .map(|part| part.data.as_interned_str()); - let volatile_suffix = if volatile { - Some("volatile") - } else { - None - }; + let volatile_suffix = volatile.to_option("volatile"); name_builder.build_cgu_name(def_path.krate, components, volatile_suffix) }).clone() diff --git a/src/librustc_mir/transform/check_unsafety.rs b/src/librustc_mir/transform/check_unsafety.rs index 70855d70228b3..00b14470ed44e 100644 --- a/src/librustc_mir/transform/check_unsafety.rs +++ b/src/librustc_mir/transform/check_unsafety.rs @@ -673,7 +673,7 @@ pub fn check_unsafety(tcx: TyCtxt<'_>, def_id: DefId) { let mut unsafe_blocks: Vec<_> = unsafe_blocks.into_iter().collect(); unsafe_blocks.sort_by_cached_key(|(hir_id, _)| tcx.hir().hir_to_node_id(*hir_id)); let used_unsafe: FxHashSet<_> = unsafe_blocks.iter() - .flat_map(|&&(id, used)| if used { Some(id) } else { None }) + .flat_map(|&&(id, used)| used.to_option(id)) .collect(); for &(block_id, is_used) in unsafe_blocks { if !is_used { diff --git a/src/librustc_resolve/diagnostics.rs b/src/librustc_resolve/diagnostics.rs index 5647d5b2794af..c62a00100b5ba 100644 --- a/src/librustc_resolve/diagnostics.rs +++ b/src/librustc_resolve/diagnostics.rs @@ -428,11 +428,7 @@ impl<'a> Resolver<'a> { Scope::MacroUsePrelude => { suggestions.extend(this.macro_use_prelude.iter().filter_map(|(name, binding)| { let res = binding.res(); - if filter_fn(res) { - Some(TypoSuggestion::from_res(*name, res)) - } else { - None - } + filter_fn(res).to_option(TypoSuggestion::from_res(*name, res)) })); } Scope::BuiltinAttrs => { @@ -455,11 +451,7 @@ impl<'a> Resolver<'a> { Scope::ExternPrelude => { suggestions.extend(this.extern_prelude.iter().filter_map(|(ident, _)| { let res = Res::Def(DefKind::Mod, DefId::local(CRATE_DEF_INDEX)); - if filter_fn(res) { - Some(TypoSuggestion::from_res(ident.name, res)) - } else { - None - } + filter_fn(res).to_option(TypoSuggestion::from_res(ident.name, res)) })); } Scope::ToolPrelude => { @@ -482,11 +474,7 @@ impl<'a> Resolver<'a> { suggestions.extend( primitive_types.iter().flat_map(|(name, prim_ty)| { let res = Res::PrimTy(*prim_ty); - if filter_fn(res) { - Some(TypoSuggestion::from_res(*name, res)) - } else { - None - } + filter_fn(res).to_option(TypoSuggestion::from_res(*name, res)) }) ) } diff --git a/src/librustc_resolve/late.rs b/src/librustc_resolve/late.rs index 73a282b1a0ec1..737170a330085 100644 --- a/src/librustc_resolve/late.rs +++ b/src/librustc_resolve/late.rs @@ -462,11 +462,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LateResolutionVisitor<'a, '_> { GenericParamKind::Lifetime { .. } => None, GenericParamKind::Type { ref default, .. } => { found_default |= default.is_some(); - if found_default { - Some((Ident::with_dummy_span(param.ident.name), Res::Err)) - } else { - None - } + found_default.to_option((Ident::with_dummy_span(param.ident.name), Res::Err)) } })); diff --git a/src/librustc_resolve/lib.rs b/src/librustc_resolve/lib.rs index 17d8f0f211a92..88b276fb665ec 100644 --- a/src/librustc_resolve/lib.rs +++ b/src/librustc_resolve/lib.rs @@ -9,6 +9,7 @@ #![doc(html_root_url = "https://doc.rust-lang.org/nightly/")] +#![feature(bool_to_option)] #![feature(crate_visibility_modifier)] #![feature(label_break_value)] #![feature(nll)] diff --git a/src/librustc_target/abi/call/aarch64.rs b/src/librustc_target/abi/call/aarch64.rs index f50ec6c2e7e3a..5147b6a017d9a 100644 --- a/src/librustc_target/abi/call/aarch64.rs +++ b/src/librustc_target/abi/call/aarch64.rs @@ -20,14 +20,7 @@ fn is_homogeneous_aggregate<'a, Ty, C>(cx: &C, arg: &mut ArgType<'a, Ty>) RegKind::Vector => size.bits() == 64 || size.bits() == 128 }; - if valid_unit { - Some(Uniform { - unit, - total: size - }) - } else { - None - } + valid_unit.to_option(Uniform { unit, total: size }) }) } diff --git a/src/librustc_target/abi/call/arm.rs b/src/librustc_target/abi/call/arm.rs index e3fee8e5700c1..88992a574e1ff 100644 --- a/src/librustc_target/abi/call/arm.rs +++ b/src/librustc_target/abi/call/arm.rs @@ -21,14 +21,7 @@ fn is_homogeneous_aggregate<'a, Ty, C>(cx: &C, arg: &mut ArgType<'a, Ty>) RegKind::Vector => size.bits() == 64 || size.bits() == 128 }; - if valid_unit { - Some(Uniform { - unit, - total: size - }) - } else { - None - } + valid_unit.to_option(Uniform { unit, total: size }) }) } diff --git a/src/librustc_target/abi/call/mod.rs b/src/librustc_target/abi/call/mod.rs index 17bad189bcfda..62fc83e660f80 100644 --- a/src/librustc_target/abi/call/mod.rs +++ b/src/librustc_target/abi/call/mod.rs @@ -414,11 +414,7 @@ impl<'a, Ty> ArgType<'a, Ty> { // i686-pc-windows-msvc, it results in wrong stack offsets. // attrs.pointee_align = Some(self.layout.align.abi); - let extra_attrs = if self.layout.is_unsized() { - Some(ArgAttributes::new()) - } else { - None - }; + let extra_attrs = self.layout.is_unsized().to_option(ArgAttributes::new()); self.mode = PassMode::Indirect(attrs, extra_attrs); } diff --git a/src/librustc_target/abi/call/powerpc64.rs b/src/librustc_target/abi/call/powerpc64.rs index a9683104d164e..5ea1c88d62f0f 100644 --- a/src/librustc_target/abi/call/powerpc64.rs +++ b/src/librustc_target/abi/call/powerpc64.rs @@ -32,14 +32,7 @@ fn is_homogeneous_aggregate<'a, Ty, C>(cx: &C, arg: &mut ArgType<'a, Ty>, abi: A RegKind::Vector => arg.layout.size.bits() == 128 }; - if valid_unit { - Some(Uniform { - unit, - total: arg.layout.size - }) - } else { - None - } + valid_unit.to_option(Uniform { unit, total: arg.layout.size }) }) } diff --git a/src/librustc_target/abi/call/sparc64.rs b/src/librustc_target/abi/call/sparc64.rs index d8930a875efbc..0f7cb75bddbae 100644 --- a/src/librustc_target/abi/call/sparc64.rs +++ b/src/librustc_target/abi/call/sparc64.rs @@ -20,14 +20,7 @@ fn is_homogeneous_aggregate<'a, Ty, C>(cx: &C, arg: &mut ArgType<'a, Ty>) RegKind::Vector => arg.layout.size.bits() == 128 }; - if valid_unit { - Some(Uniform { - unit, - total: arg.layout.size - }) - } else { - None - } + valid_unit.to_option(Uniform { unit, total: arg.layout.size }) }) } diff --git a/src/librustc_target/lib.rs b/src/librustc_target/lib.rs index a349dc26e834c..77b856b655f77 100644 --- a/src/librustc_target/lib.rs +++ b/src/librustc_target/lib.rs @@ -10,6 +10,7 @@ #![doc(html_root_url = "https://doc.rust-lang.org/nightly/")] #![feature(box_syntax)] +#![feature(bool_to_option)] #![feature(nll)] #![feature(slice_patterns)] diff --git a/src/librustc_typeck/check/method/probe.rs b/src/librustc_typeck/check/method/probe.rs index 593cf77a4a6c7..100fc8590469a 100644 --- a/src/librustc_typeck/check/method/probe.rs +++ b/src/librustc_typeck/check/method/probe.rs @@ -1103,11 +1103,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> { r.map(|mut pick| { pick.autoderefs = step.autoderefs; pick.autoref = Some(mutbl); - pick.unsize = if step.unsize { - Some(self_ty) - } else { - None - }; + pick.unsize = step.unsize.to_option(self_ty); pick }) }) diff --git a/src/librustc_typeck/check/upvar.rs b/src/librustc_typeck/check/upvar.rs index 97e59664df0c0..e106387898c9b 100644 --- a/src/librustc_typeck/check/upvar.rs +++ b/src/librustc_typeck/check/upvar.rs @@ -116,11 +116,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { }; let infer_kind = if let UpvarSubsts::Closure(closure_substs) = substs { - if self.closure_kind(closure_def_id, closure_substs).is_none() { - Some(closure_substs) - } else { - None - } + self.closure_kind(closure_def_id, closure_substs).is_none().to_option(closure_substs) } else { None }; diff --git a/src/librustc_typeck/lib.rs b/src/librustc_typeck/lib.rs index 9374113e1c950..6dd88be1d2068 100644 --- a/src/librustc_typeck/lib.rs +++ b/src/librustc_typeck/lib.rs @@ -59,6 +59,7 @@ This API is completely unstable and subject to change. #![allow(non_camel_case_types)] +#![feature(bool_to_option)] #![feature(box_patterns)] #![feature(box_syntax)] #![feature(crate_visibility_modifier)] diff --git a/src/libsyntax/config.rs b/src/libsyntax/config.rs index 54dc95291d67f..591bdcebec794 100644 --- a/src/libsyntax/config.rs +++ b/src/libsyntax/config.rs @@ -69,7 +69,7 @@ macro_rules! configure { impl<'a> StripUnconfigured<'a> { pub fn configure(&mut self, mut node: T) -> Option { self.process_cfg_attrs(&mut node); - if self.in_cfg(node.attrs()) { Some(node) } else { None } + self.in_cfg(node.attrs()).to_option(node) } /// Parse and expand all `cfg_attr` attributes into a list of attributes diff --git a/src/libsyntax/lib.rs b/src/libsyntax/lib.rs index a68b7fdf931a4..789b48af51241 100644 --- a/src/libsyntax/lib.rs +++ b/src/libsyntax/lib.rs @@ -7,6 +7,7 @@ #![doc(html_root_url = "https://doc.rust-lang.org/nightly/", test(attr(deny(warnings))))] +#![feature(bool_to_option)] #![feature(box_syntax)] #![feature(const_fn)] #![feature(const_transmute)] diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 136fc355f89d4..944c3e02879e6 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -318,7 +318,7 @@ pub fn token_to_string(token: &Token) -> String { } fn token_to_string_ext(token: &Token, convert_dollar_crate: bool) -> String { - let convert_dollar_crate = if convert_dollar_crate { Some(token.span) } else { None }; + let convert_dollar_crate = convert_dollar_crate.to_option(token.span); token_kind_to_string_ext(&token.kind, convert_dollar_crate) } diff --git a/src/libsyntax/util/lev_distance.rs b/src/libsyntax/util/lev_distance.rs index 4127a8c7fce25..efb3c2396c32d 100644 --- a/src/libsyntax/util/lev_distance.rs +++ b/src/libsyntax/util/lev_distance.rs @@ -77,6 +77,6 @@ pub fn find_best_match_for_name<'a, T>(iter_names: T, if let Some(candidate) = case_insensitive_match { Some(candidate) // exact case insensitive match has a higher priority } else { - if let Some((candidate, _)) = levenstein_match { Some(candidate) } else { None } + levenstein_match.map(|(candidate, _)| candidate) } } diff --git a/src/libsyntax_ext/format_foreign.rs b/src/libsyntax_ext/format_foreign.rs index 3d4f82764413a..f6e8b08653267 100644 --- a/src/libsyntax_ext/format_foreign.rs +++ b/src/libsyntax_ext/format_foreign.rs @@ -95,12 +95,12 @@ pub mod printf { }; // Has a special form in Rust for numbers. - let fill = if c_zero { Some("0") } else { None }; + let fill = c_zero.to_option("0"); - let align = if c_left { Some("<") } else { None }; + let align = c_left.to_option("<"); // Rust doesn't have an equivalent to the `' '` flag. - let sign = if c_plus { Some("+") } else { None }; + let sign = c_plus.to_option("+"); // Not *quite* the same, depending on the type... let alt = c_alt; diff --git a/src/libsyntax_ext/lib.rs b/src/libsyntax_ext/lib.rs index 5516f276422e9..9f9ea108378f7 100644 --- a/src/libsyntax_ext/lib.rs +++ b/src/libsyntax_ext/lib.rs @@ -3,6 +3,7 @@ #![doc(html_root_url = "https://doc.rust-lang.org/nightly/")] +#![feature(bool_to_option)] #![feature(crate_visibility_modifier)] #![feature(decl_macro)] #![feature(nll)] diff --git a/src/libtest/lib.rs b/src/libtest/lib.rs index 4c3cbeb4acced..9dbb7912d94af 100644 --- a/src/libtest/lib.rs +++ b/src/libtest/lib.rs @@ -24,6 +24,7 @@ #![cfg_attr(any(unix, target_os = "cloudabi"), feature(libc))] #![feature(rustc_private)] #![feature(nll)] +#![feature(bool_to_option)] #![feature(set_stdio)] #![feature(panic_unwind)] #![feature(staged_api)] @@ -1928,11 +1929,7 @@ fn run_test_in_process( None }; - let start = if report_time { - Some(Instant::now()) - } else { - None - }; + let start = report_time.to_option(Instant::now()); let result = catch_unwind(AssertUnwindSafe(testfn)); let exec_time = start.map(|start| { let duration = start.elapsed(); @@ -1962,11 +1959,7 @@ fn spawn_test_subprocess( let args = env::args().collect::>(); let current_exe = &args[0]; - let start = if report_time { - Some(Instant::now()) - } else { - None - }; + let start = report_time.to_option(Instant::now()); let output = match Command::new(current_exe) .env(SECONDARY_TEST_INVOKER_VAR, desc.name.as_slice()) .output() {