diff --git a/src/libcore/bool.rs b/src/libcore/bool.rs index 617bdd238f4c63d4660628b84cf5e4d5a140af6a..1b3c254a05f98126a3ff6b0a47ed1b407dba3671 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.then_some(0), None); + /// assert_eq!(true.then_some(0), Some(0)); /// ``` #[unstable(feature = "bool_to_option", issue = "64260")] #[inline] - pub fn then(self, t: T) -> Option { + pub fn then_some(self, t: T) -> Option { if self { Some(t) } else { @@ -29,12 +29,12 @@ pub fn then(self, t: T) -> Option { /// ``` /// #![feature(bool_to_option)] /// - /// assert_eq!(false.then_with(|| 0), None); - /// assert_eq!(true.then_with(|| 0), Some(0)); + /// assert_eq!(false.then(|| 0), None); + /// assert_eq!(true.then(|| 0), Some(0)); /// ``` #[unstable(feature = "bool_to_option", issue = "64260")] #[inline] - pub fn then_with T>(self, f: F) -> Option { + pub fn then 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 0f1e6e89451e9b4294a6aad180c3250e623f2122..e89eb2c7f94d5731a04e320de4427c0dc4d2af68 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.then_some(0), None); + assert_eq!(true.then_some(0), Some(0)); + assert_eq!(false.then(|| 0), None); + assert_eq!(true.then(|| 0), Some(0)); } diff --git a/src/libfmt_macros/lib.rs b/src/libfmt_macros/lib.rs index 900c6ed5ff32235d487ec6a187a0f8970ad8ef9f..2ecbe770729b5b278f21213df8902e31104ef224 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::*; @@ -644,11 +645,7 @@ fn integer(&mut self) -> Option { break; } } - if found { - Some(cur) - } else { - None - } + found.then_some(cur) } } diff --git a/src/librustc/hir/map/blocks.rs b/src/librustc/hir/map/blocks.rs index f25f3b5741a0ef2984e1c9287e96722f413da9b5..8f9f3983262372fdcd28e8782d6e6855c4ed933c 100644 --- a/src/librustc/hir/map/blocks.rs +++ b/src/librustc/hir/map/blocks.rs @@ -147,13 +147,7 @@ pub fn from_node(node: Node<'_>) -> Option> { map::Node::Expr(e) => e.is_fn_like(), _ => false }; - if fn_like { - Some(FnLikeNode { - node, - }) - } else { - None - } + fn_like.then_some(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 3110b027c5bbe5f360aea5b5a85a9a2e455b992c..3e28145c0fa0a544b7022a2050fa74a93ea322a6 100644 --- a/src/librustc/infer/outlives/verify.rs +++ b/src/librustc/infer/outlives/verify.rs @@ -211,11 +211,7 @@ fn declared_generic_bounds_from_env_with_compare_fn( (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).then_some(ty::OutlivesPredicate(p_ty, r)) }); param_bounds diff --git a/src/librustc/lib.rs b/src/librustc/lib.rs index e708c5ab6e77d0da21e6e75270a1c4dda3d19bec..24b87ffc80c2a5b60a425a1a17dfa6683080560b 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 df5d4997e089aac8cbd297b8ff5054ab1fb5d41e..814c244ecbc2b6b5dd6ec97107912c8e59b52d84 100644 --- a/src/librustc/mir/mod.rs +++ b/src/librustc/mir/mod.rs @@ -242,11 +242,7 @@ pub fn temps_iter<'a>(&'a self) -> impl Iterator + 'a { 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() { - Some(local) - } else { - None - } + self.local_decls[local].is_user_variable().then_some(local) }) } diff --git a/src/librustc/traits/error_reporting.rs b/src/librustc/traits/error_reporting.rs index ba44c6c3b9a1f27cd993131bd1fb6e6a29ad6276..412300f0b10dff7dd74f814ece31f43308ad9d63 100644 --- a/src/librustc/traits/error_reporting.rs +++ b/src/librustc/traits/error_reporting.rs @@ -363,11 +363,7 @@ fn impl_similar_to(&self, 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).then_some(impl_def_id) } fn describe_generator(&self, body_id: hir::BodyId) -> Option<&'static str> { diff --git a/src/librustc/ty/mod.rs b/src/librustc/ty/mod.rs index c9a934e9ebd8490ff86cbfb73096bf596cf1501e..05a2704cc5dfc67792b24a3ea6cc4ea7e3fee915 100644 --- a/src/librustc/ty/mod.rs +++ b/src/librustc/ty/mod.rs @@ -2784,11 +2784,7 @@ pub fn opt_associated_item(self, def_id: DefId) -> Option { } }; - if is_associated_item { - Some(self.associated_item(def_id)) - } else { - None - } + is_associated_item.then(|| self.associated_item(def_id)) } fn associated_item_from_trait_item_ref(self, @@ -3253,7 +3249,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.then_some(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 e5f4e793132f4b0d74166cfa1f955e5f970f1a90..2f30b797fb1516d11c4ecd31bd1398e9802a3655 100644 --- a/src/librustc/ty/query/job.rs +++ b/src/librustc/ty/query/job.rs @@ -303,13 +303,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).then_some(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 33dc2513de5844892e35e0a94007dab34904bc21..1ea9362dc4260ea16c52a90cac5518903533a1ba 100644 --- a/src/librustc_codegen_llvm/attributes.rs +++ b/src/librustc_codegen_llvm/attributes.rs @@ -375,11 +375,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 f38f9dfecd38705e63e7b0c418da2ee828b0f8e5..419e99d55d72d703d8895fa97422f8df6fb622b9 100644 --- a/src/librustc_codegen_llvm/common.rs +++ b/src/librustc_codegen_llvm/common.rs @@ -245,11 +245,7 @@ fn const_to_opt_u128(&self, v: &'ll Value, sign_ext: bool) -> Option { 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.then_some(hi_lo_to_u128(lo, hi)) }) } diff --git a/src/librustc_codegen_llvm/lib.rs b/src/librustc_codegen_llvm/lib.rs index 2ff5872370fd10ccbe467b1859b38a672abc89b7..00a84f8d80f36467f52fbfd4b1abeb815750e8c0 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 e27cb6d8dda8973ade54bda2621b77dda290bebe..cd3d99951e2b1054330c477b528239edcc636755 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().then(|| 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 f8b3e0ffe5cd9774416126d04a0530aa715789e4..cea5dc18c136296391e6dd89e2b460150123065a 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).then_some(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 9784d870b31ba44ae4aa69d6ea5faf0c9176b429..9919666027a2116c101fac344efc1dbccd44da06 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 @@ pub fn into_compiled_module(self, 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 + .then(|| outputs.temp_path(OutputType::Object, Some(&self.name))); + let bytecode = emit_bc + .then(|| outputs.temp_path(OutputType::Bitcode, Some(&self.name))); + let bytecode_compressed = emit_bc_compressed.then(|| { + 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 76af4342f5c9ef0c59877cdb020394bd6287b98f..9bb18788171b4a51851e054984664a6446dc05f9 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 235184382c5cc18873e5c09f72e86b5cff6bdcc2..2a4bc41f85072192bda1c0c202451dcbf9b83514 100644 --- a/src/librustc_interface/passes.rs +++ b/src/librustc_interface/passes.rs @@ -547,13 +547,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().then(|| output_path.clone()); check_output(output_paths, check) } diff --git a/src/librustc_interface/queries.rs b/src/librustc_interface/queries.rs index d1fbb949fb164326eb4fc93237e2831d0d221d66..e429b4d101a904b524bd5d3d61e88d67c30c2978 100644 --- a/src/librustc_interface/queries.rs +++ b/src/librustc_interface/queries.rs @@ -117,11 +117,9 @@ fn codegen_backend(&self) -> &Lrc> { pub fn dep_graph_future(&self) -> Result<&Query>> { self.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().then(|| { + rustc_incremental::load_dep_graph(self.session()) + })) }) } diff --git a/src/librustc_interface/util.rs b/src/librustc_interface/util.rs index d8e20e17ce835f927239b304185d818771f7e4a8..8c225b83f40e8234b0f0f55506150cb6cdca5411 100644 --- a/src/librustc_interface/util.rs +++ b/src/librustc_interface/util.rs @@ -107,11 +107,7 @@ pub fn create_session( 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().then_some(STACK_SIZE) } struct Sink(Arc>>); @@ -285,11 +281,7 @@ fn get_rustc_path_inner(bin_path: &str) -> Option { } else { "rustc" }); - if candidate.exists() { - Some(candidate) - } else { - None - } + candidate.exists().then_some(candidate) }) .next() } diff --git a/src/librustc_lint/builtin.rs b/src/librustc_lint/builtin.rs index 0fd7145f425d3027cf58e59594035682aec631dc..10b00d35d9bcaa9d0e12bf19ef2eee101d311770 100644 --- a/src/librustc_lint/builtin.rs +++ b/src/librustc_lint/builtin.rs @@ -1491,11 +1491,7 @@ fn lifetimes_outliving_type<'tcx>( 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).then_some(outlives.1) } _ => None } @@ -1554,11 +1550,7 @@ fn collect_outlives_bound_spans<'tcx>( }), _ => false, }; - if is_inferred { - Some((i, bound.span())) - } else { - None - } + is_inferred.then_some((i, bound.span())) } else { None } diff --git a/src/librustc_lint/lib.rs b/src/librustc_lint/lib.rs index e60c025c3ef8b2f5671865f3672c53001e95b415..12aab4b4f84e4549a329d46124e88bdb5e3f5f51 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 dbf2dcf1c0aea461d6be050c33deaf806f453dc5..25bd2c45da5f5b05bacf24355a8a666448ac5ae4 100644 --- a/src/librustc_metadata/creader.rs +++ b/src/librustc_metadata/creader.rs @@ -802,11 +802,8 @@ fn inject_allocator_crate(&mut self, krate: &ast::Crate) { // 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 self.cstore.has_global_allocator { - Some(Symbol::intern("this crate")) - } else { - None - }; + let mut global_allocator = self.cstore.has_global_allocator + .then(|| Symbol::intern("this crate")); self.cstore.iter_crate_data(|_, data| { if !data.has_global_allocator() { return diff --git a/src/librustc_metadata/lib.rs b/src/librustc_metadata/lib.rs index 8c0b7345e1e8aa0a81cea3b9b5acf345f98d08ca..aaaff7e3b0a4a9eef42da430219e455ecdbebeeb 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 b9363200cdf2855dc60ab6c6801895642f8d7ecd..7a5bbb4d892980d3c6850c427c14b54ea17b0f18 100644 --- a/src/librustc_mir/borrow_check/nll/mod.rs +++ b/src/librustc_mir/borrow_check/nll/mod.rs @@ -173,11 +173,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).then_some(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 85031d6210a4d2202bbc4dab2c2d66affd5753f6..0b3cb29e39ef4e38d183aceebeae3ef2a9e87bc0 100644 --- a/src/librustc_mir/borrow_check/nll/region_infer/mod.rs +++ b/src/librustc_mir/borrow_check/nll/region_infer/mod.rs @@ -493,7 +493,7 @@ pub(super) fn solve( // 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).then(|| vec![]); self.check_type_tests( infcx, @@ -709,14 +709,11 @@ fn apply_member_constraint( 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/matches/test.rs b/src/librustc_mir/build/matches/test.rs index c893d6f485630da75b81915f7f78c26781f27836..e320811ca0556e10ef1519c9997c23b32c238f5d 100644 --- a/src/librustc_mir/build/matches/test.rs +++ b/src/librustc_mir/build/matches/test.rs @@ -680,7 +680,7 @@ pub fn sort_candidate<'pat>( } })(); - 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) @@ -690,7 +690,7 @@ pub fn sort_candidate<'pat>( } (&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 92c7178da53e0b163d546a59b14787427e3acb7f..0086c3b0e103ff999f55fe787879902ac0401f41 100644 --- a/src/librustc_mir/hair/pattern/mod.rs +++ b/src/librustc_mir/hair/pattern/mod.rs @@ -1154,13 +1154,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.then_some(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 ee7fb18fd05a544fb5133f32c1094ff04b68f8be..8f177ad1225806c3e5858822eb0077cc97e508f9 100644 --- a/src/librustc_mir/interpret/memory.rs +++ b/src/librustc_mir/interpret/memory.rs @@ -324,7 +324,7 @@ pub fn check_ptr_access( size: Size, align: Align, ) -> InterpResult<'tcx, Option>> { - let align = if M::CHECK_ALIGN { Some(align) } else { None }; + let align = M::CHECK_ALIGN.then_some(align); self.check_ptr_access_align(sptr, size, align, CheckInAllocMsg::MemoryAccessTest) } diff --git a/src/librustc_mir/interpret/step.rs b/src/librustc_mir/interpret/step.rs index d7493338a5352f083a9db4a3733366d44baf7715..55b9427a75ba303d0c5fe3e9877c893eeb4f3672 100644 --- a/src/librustc_mir/interpret/step.rs +++ b/src/librustc_mir/interpret/step.rs @@ -157,9 +157,9 @@ pub fn eval_rvalue_into_place( } 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).then_some(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).then_some(left.layout); let right = self.read_immediate(self.eval_operand(right, layout)?)?; self.binop_ignore_overflow( bin_op, @@ -172,7 +172,7 @@ pub fn eval_rvalue_into_place( 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).then_some(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 f4fb9a5e4f2a2ad20a2438ca5d9835fcd272f0ad..42b72b64f8a3053ae209ae62294eb59ece03b520 100644 --- a/src/librustc_mir/lib.rs +++ b/src/librustc_mir/lib.rs @@ -8,6 +8,7 @@ #![feature(in_band_lifetimes)] #![feature(inner_deref)] #![feature(slice_patterns)] +#![feature(bool_to_option)] #![feature(box_patterns)] #![feature(box_syntax)] #![feature(crate_visibility_modifier)] @@ -27,7 +28,6 @@ #![feature(associated_type_bounds)] #![feature(range_is_empty)] #![feature(stmt_expr_attributes)] -#![feature(bool_to_option)] #![feature(trait_alias)] #![feature(matches_macro)] diff --git a/src/librustc_mir/monomorphize/partitioning.rs b/src/librustc_mir/monomorphize/partitioning.rs index 42f08771f866d57b32b1f4242ec8b3ddc05017f1..591f220549deaf1c7f21547b312636c5d56b6285 100644 --- a/src/librustc_mir/monomorphize/partitioning.rs +++ b/src/librustc_mir/monomorphize/partitioning.rs @@ -761,11 +761,7 @@ fn compute_codegen_unit_name( .iter() .map(|part| part.data.as_symbol()); - let volatile_suffix = if volatile { - Some("volatile") - } else { - None - }; + let volatile_suffix = volatile.then_some("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 2c45dcfbe2665c077c70617c29ed0e290170e02a..284285c327c34428d26f7eabfb83fb00a4599633 100644 --- a/src/librustc_mir/transform/check_unsafety.rs +++ b/src/librustc_mir/transform/check_unsafety.rs @@ -658,7 +658,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.then_some(id)) .collect(); for &(block_id, is_used) in unsafe_blocks { if !is_used { diff --git a/src/librustc_mir/transform/uninhabited_enum_branching.rs b/src/librustc_mir/transform/uninhabited_enum_branching.rs index de070d75ad8e2a88d9f1e4260728e3890c7051b4..26081d93946640f9d39c118a6b97ab75a705eb80 100644 --- a/src/librustc_mir/transform/uninhabited_enum_branching.rs +++ b/src/librustc_mir/transform/uninhabited_enum_branching.rs @@ -29,7 +29,7 @@ fn get_switched_on_type<'tcx>( // Only bother checking blocks which terminate by switching on a local. if let Some(local) = get_discriminant_local(&terminator.kind) { let stmt_before_term = (block_data.statements.len() > 0) - .then_with(|| &block_data.statements[block_data.statements.len() - 1].kind); + .then(|| &block_data.statements[block_data.statements.len() - 1].kind); if let Some(StatementKind::Assign(box (l, Rvalue::Discriminant(place)))) = stmt_before_term { @@ -59,7 +59,7 @@ fn variant_discriminants<'tcx>( .iter_enumerated() .filter_map(|(idx, layout)| { (layout.abi != Abi::Uninhabited) - .then_with(|| ty.discriminant_for_variant(tcx, idx).unwrap().val) + .then(|| ty.discriminant_for_variant(tcx, idx).unwrap().val) }) .collect(), } diff --git a/src/librustc_parse/config.rs b/src/librustc_parse/config.rs index 30e056e52d25acd58b770a1d27145e2cb81fc5fd..7dbfd98de7535d041ccd08a5777b5bf0869182fc 100644 --- a/src/librustc_parse/config.rs +++ b/src/librustc_parse/config.rs @@ -75,7 +75,7 @@ pub fn features(mut krate: ast::Crate, sess: &ParseSess, edition: Edition, 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()).then_some(node) } /// Parse and expand all `cfg_attr` attributes into a list of attributes diff --git a/src/librustc_parse/lib.rs b/src/librustc_parse/lib.rs index a222f3f00c4633fe4a0a47f2ad8d7be6b15d6944..69050534ee60e8b47dad94163c0c07504ee7c8a6 100644 --- a/src/librustc_parse/lib.rs +++ b/src/librustc_parse/lib.rs @@ -1,5 +1,6 @@ //! The main parser interface. +#![feature(bool_to_option)] #![feature(crate_visibility_modifier)] use syntax::ast; diff --git a/src/librustc_resolve/diagnostics.rs b/src/librustc_resolve/diagnostics.rs index 4dcafb6d2798ccdbf9d2d7281b3040f2c48a7e4b..8dd45f5df4cabc08e106448b4ee7e5d5149f19e7 100644 --- a/src/librustc_resolve/diagnostics.rs +++ b/src/librustc_resolve/diagnostics.rs @@ -422,11 +422,7 @@ fn early_lookup_typo_candidate( 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).then_some(TypoSuggestion::from_res(*name, res)) })); } Scope::BuiltinAttrs => { @@ -440,11 +436,7 @@ fn early_lookup_typo_candidate( 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).then_some(TypoSuggestion::from_res(ident.name, res)) })); } Scope::ToolPrelude => { @@ -467,11 +459,7 @@ fn early_lookup_typo_candidate( 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).then_some(TypoSuggestion::from_res(*name, res)) }) ) } diff --git a/src/librustc_resolve/late.rs b/src/librustc_resolve/late.rs index 666c482c680462bb6ff213a0b0658004c8919656..4f95d6fe70f1712f38d089e4d687b909d623a619 100644 --- a/src/librustc_resolve/late.rs +++ b/src/librustc_resolve/late.rs @@ -496,11 +496,7 @@ fn visit_generics(&mut self, generics: &'tcx Generics) { 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.then_some((Ident::with_dummy_span(param.ident.name), Res::Err)) } })); diff --git a/src/librustc_resolve/lib.rs b/src/librustc_resolve/lib.rs index be36e02f5b5b1e17fc8659c2d5092a98890f3b91..31f59e47d0aceebbf67e954aa54e6f6c44c57436 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_session/session.rs b/src/librustc_session/session.rs index 9369c1771a35ed10c6c90caa5e5db0e3c7043ed8..150d207e5bfe9cb6fa46b24b212eda63ad788962 100644 --- a/src/librustc_session/session.rs +++ b/src/librustc_session/session.rs @@ -752,11 +752,7 @@ pub fn incr_comp_session_dir(&self) -> cell::Ref<'_, PathBuf> { } 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) { @@ -1079,8 +1075,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_target/abi/call/aarch64.rs b/src/librustc_target/abi/call/aarch64.rs index 45fe4751a3dae9dc4e07eae26513e3157e97e09f..06c001e577b870c95bf1f2fb7159e0cbf88eb2cc 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 ArgAbi<'a, Ty>) RegKind::Vector => size.bits() == 64 || size.bits() == 128 }; - if valid_unit { - Some(Uniform { - unit, - total: size - }) - } else { - None - } + valid_unit.then_some(Uniform { unit, total: size }) }) } diff --git a/src/librustc_target/abi/call/arm.rs b/src/librustc_target/abi/call/arm.rs index ff929f33d8bc94a0ecab52cdc8faf40b266498e0..36971c1c5013cc981c91921d802e6ade01970c9a 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 ArgAbi<'a, Ty>) RegKind::Vector => size.bits() == 64 || size.bits() == 128 }; - if valid_unit { - Some(Uniform { - unit, - total: size - }) - } else { - None - } + valid_unit.then_some(Uniform { unit, total: size }) }) } diff --git a/src/librustc_target/abi/call/mod.rs b/src/librustc_target/abi/call/mod.rs index 6f53577b450550ca064cae8aa7c192c6ada7da65..5119464b1cc5fbc6323f78d0bb8d748b8a6334f5 100644 --- a/src/librustc_target/abi/call/mod.rs +++ b/src/librustc_target/abi/call/mod.rs @@ -416,11 +416,7 @@ pub fn make_indirect(&mut self) { // 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().then_some(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 f967a83d5f9b603ba41d7dea50f0df8f0b365357..fe4594802f66abe8278d9f98f5ad37efc71101ef 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 ArgAbi<'a, Ty>, abi: AB RegKind::Vector => arg.layout.size.bits() == 128 }; - if valid_unit { - Some(Uniform { - unit, - total: arg.layout.size - }) - } else { - None - } + valid_unit.then_some(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 fe2c427f703100ba19445da7414015ab9fd31a1e..32be7b89cb064c300b60f4ef830e799555eda740 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 ArgAbi<'a, Ty>) RegKind::Vector => arg.layout.size.bits() == 128 }; - if valid_unit { - Some(Uniform { - unit, - total: arg.layout.size - }) - } else { - None - } + valid_unit.then_some(Uniform { unit, total: arg.layout.size }) }) } diff --git a/src/librustc_target/lib.rs b/src/librustc_target/lib.rs index 5582eaf47c426f09b3409bd78b68dc7edc2e0c3e..6a1498e98e816017eed9f21bb5ceea802c48556d 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 9717190045afb38bf73663781b321b6f3f0853e1..992308183b42f951fd9dde5645722fb13c8af6f2 100644 --- a/src/librustc_typeck/check/method/probe.rs +++ b/src/librustc_typeck/check/method/probe.rs @@ -1105,11 +1105,7 @@ fn pick_autorefd_method( 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.then_some(self_ty); pick }) }) diff --git a/src/librustc_typeck/check/upvar.rs b/src/librustc_typeck/check/upvar.rs index 3002459d56f3c392cb7f81fcada250b8f7563cf1..c5a6c072979182c5ff457536a26f168ef54cb2f5 100644 --- a/src/librustc_typeck/check/upvar.rs +++ b/src/librustc_typeck/check/upvar.rs @@ -114,11 +114,7 @@ fn analyze_closure( }; 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().then_some(closure_substs) } else { None }; diff --git a/src/librustc_typeck/lib.rs b/src/librustc_typeck/lib.rs index c606feab087279ca5f7bc1c04a6cfa13ebac9e17..6a6294b6f87982635f2da36ffc706cab3443e0d8 100644 --- a/src/librustc_typeck/lib.rs +++ b/src/librustc_typeck/lib.rs @@ -59,6 +59,7 @@ #![allow(non_camel_case_types)] +#![feature(bool_to_option)] #![feature(box_patterns)] #![feature(box_syntax)] #![feature(crate_visibility_modifier)] diff --git a/src/libsyntax/lib.rs b/src/libsyntax/lib.rs index a94742634cf17919e7f3378f391284896ef0f17f..36173801eae3d31dc760fcc5f869185ef0c2b938 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 4821bbd9ec6e2a63eb548f4936d05a1db3fee2c4..b2e8d8526fd2e9ec26511f074734763d7f6922bb 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -317,7 +317,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.then_some(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 4127a8c7fce2502650e8d52868b0b8320632fab8..efb3c2396c32d54b5deead0a04bc0a3b851c7edb 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 3d4f82764413a0021fd93b3dfb194b259ad18264..0d1d2926c85d92d3ca02b5d24d77f868bd30dda0 100644 --- a/src/libsyntax_ext/format_foreign.rs +++ b/src/libsyntax_ext/format_foreign.rs @@ -95,12 +95,12 @@ pub fn translate(&self) -> Option { }; // Has a special form in Rust for numbers. - let fill = if c_zero { Some("0") } else { None }; + let fill = c_zero.then_some("0"); - let align = if c_left { Some("<") } else { None }; + let align = c_left.then_some("<"); // Rust doesn't have an equivalent to the `' '` flag. - let sign = if c_plus { Some("+") } else { None }; + let sign = c_plus.then_some("+"); // 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 b9287d2fe7062ed1f3cd742225b748b84bd00340..55c7f3fa5749ae10988e3d9acced10f2856f427f 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 7647978b3d975dfd0f69667ecf4a95bc6953caf9..0097558eae6c1488ff6922173ccf361f8deea21a 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)] @@ -562,11 +563,7 @@ fn run_test_in_process( None }; - let start = if report_time { - Some(Instant::now()) - } else { - None - }; + let start = report_time.then(Instant::now); let result = catch_unwind(AssertUnwindSafe(testfn)); let exec_time = start.map(|start| { let duration = start.elapsed(); @@ -597,11 +594,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.then(Instant::now); let output = match Command::new(current_exe) .env(SECONDARY_TEST_INVOKER_VAR, desc.name.as_slice()) .output() {