Skip to content

Commit e3a8ea4

Browse files
committed
Use to_option in various places
1 parent 51901ee commit e3a8ea4

File tree

51 files changed

+81
-236
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+81
-236
lines changed

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.to_option(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.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
@@ -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().to_option(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).to_option(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.to_option_with(|| 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.to_option(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).to_option(None))
307+
.is_some()
313308
}
314309

315310
// Deterministically pick an query from a list

src/librustc_codegen_llvm/attributes.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -375,11 +375,7 @@ pub fn provide_extern(providers: &mut Providers<'_>) {
375375
let native_libs = tcx.native_libraries(cnum);
376376

377377
let def_id_to_native_lib = native_libs.iter().filter_map(|lib|
378-
if let Some(id) = lib.foreign_module {
379-
Some((id, lib))
380-
} else {
381-
None
382-
}
378+
lib.foreign_module.map(|id| (id, lib))
383379
).collect::<FxHashMap<_, _>>();
384380

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

src/librustc_codegen_llvm/common.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -245,11 +245,7 @@ impl ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
245245
let (mut lo, mut hi) = (0u64, 0u64);
246246
let success = llvm::LLVMRustConstInt128Get(v, sign_ext,
247247
&mut hi, &mut lo);
248-
if success {
249-
Some(hi_lo_to_u128(lo, hi))
250-
} else {
251-
None
252-
}
248+
success.to_option(hi_lo_to_u128(lo, hi))
253249
})
254250
}
255251

src/librustc_codegen_llvm/lib.rs

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

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

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

Lines changed: 6 additions & 16 deletions
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,11 @@ 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.to_option(outputs.temp_path(OutputType::Object, Some(&self.name)));
73+
let bytecode = emit_bc.to_option(outputs.temp_path(OutputType::Bitcode, Some(&self.name)));
74+
let bytecode_compressed = emit_bc_compressed.to_option(
75+
outputs.temp_path(OutputType::Bitcode, Some(&self.name))
76+
.with_extension(RLIB_BYTECODE_EXTENSION));
8777

8878
CompiledModule {
8979
name: self.name.clone(),

src/librustc_interface/lib.rs

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -547,13 +547,7 @@ fn output_contains_path(output_paths: &[PathBuf], input_path: &PathBuf) -> bool
547547
}
548548

549549
fn output_conflicts_with_dir(output_paths: &[PathBuf]) -> Option<PathBuf> {
550-
let check = |output_path: &PathBuf| {
551-
if output_path.is_dir() {
552-
Some(output_path.clone())
553-
} else {
554-
None
555-
}
556-
};
550+
let check = |output_path: &PathBuf| output_path.is_dir().to_option(output_path.clone());
557551
check_output(output_paths, check)
558552
}
559553

src/librustc_interface/queries.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,9 @@ impl<'tcx> Queries<'tcx> {
117117

118118
pub fn dep_graph_future(&self) -> Result<&Query<Option<DepGraphFuture>>> {
119119
self.dep_graph_future.compute(|| {
120-
Ok(if self.session().opts.build_dep_graph() {
121-
Some(rustc_incremental::load_dep_graph(self.session()))
122-
} else {
123-
None
124-
})
120+
Ok(self.session().opts.build_dep_graph().to_option_with(|| {
121+
rustc_incremental::load_dep_graph(self.session())
122+
}))
125123
})
126124
}
127125

src/librustc_interface/util.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,7 @@ const STACK_SIZE: usize = 16 * 1024 * 1024;
107107
fn get_stack_size() -> Option<usize> {
108108
// FIXME: Hacks on hacks. If the env is trying to override the stack size
109109
// then *don't* set it explicitly.
110-
if env::var_os("RUST_MIN_STACK").is_none() {
111-
Some(STACK_SIZE)
112-
} else {
113-
None
114-
}
110+
env::var_os("RUST_MIN_STACK").is_none().to_option(STACK_SIZE)
115111
}
116112

117113
struct Sink(Arc<Mutex<Vec<u8>>>);
@@ -285,11 +281,7 @@ fn get_rustc_path_inner(bin_path: &str) -> Option<PathBuf> {
285281
} else {
286282
"rustc"
287283
});
288-
if candidate.exists() {
289-
Some(candidate)
290-
} else {
291-
None
292-
}
284+
candidate.exists().to_option(candidate)
293285
})
294286
.next()
295287
}

src/librustc_lint/builtin.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1491,11 +1491,7 @@ impl ExplicitOutlivesRequirements {
14911491
match pred {
14921492
ty::Predicate::TypeOutlives(outlives) => {
14931493
let outlives = outlives.skip_binder();
1494-
if outlives.0.is_param(index) {
1495-
Some(outlives.1)
1496-
} else {
1497-
None
1498-
}
1494+
outlives.0.is_param(index).to_option(outlives.1)
14991495
}
15001496
_ => None
15011497
}
@@ -1554,11 +1550,7 @@ impl ExplicitOutlivesRequirements {
15541550
}),
15551551
_ => false,
15561552
};
1557-
if is_inferred {
1558-
Some((i, bound.span()))
1559-
} else {
1560-
None
1561-
}
1553+
is_inferred.to_option((i, bound.span()))
15621554
} else {
15631555
None
15641556
}

src/librustc_lint/lib.rs

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

src/librustc_metadata/creader.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -802,11 +802,8 @@ impl<'a> CrateLoader<'a> {
802802
// First up we check for global allocators. Look at the crate graph here
803803
// and see what's a global allocator, including if we ourselves are a
804804
// global allocator.
805-
let mut global_allocator = if self.cstore.has_global_allocator {
806-
Some(Symbol::intern("this crate"))
807-
} else {
808-
None
809-
};
805+
let mut global_allocator = self.cstore.has_global_allocator
806+
.to_option_with(|| Symbol::intern("this crate"));
810807
self.cstore.iter_crate_data(|_, data| {
811808
if !data.has_global_allocator() {
812809
return

src/librustc_metadata/lib.rs

Lines changed: 1 addition & 0 deletions
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(core_intrinsics)]
56
#![feature(crate_visibility_modifier)]

src/librustc_mir/borrow_check/nll/mod.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,7 @@ pub(in crate::borrow_check) fn compute_regions<'cx, 'tcx>(
173173
Option<Rc<Output<RegionVid, BorrowIndex, LocationIndex, Local, MovePathIndex>>>,
174174
Option<ClosureRegionRequirements<'tcx>>,
175175
) {
176-
let mut all_facts = if AllFacts::enabled(infcx.tcx) {
177-
Some(AllFacts::default())
178-
} else {
179-
None
180-
};
176+
let mut all_facts = AllFacts::enabled(infcx.tcx).to_option(AllFacts::default());
181177

182178
let universal_regions = Rc::new(universal_regions);
183179

src/librustc_mir/borrow_check/nll/region_infer/mod.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
493493
// functions below, which will trigger them to report errors
494494
// eagerly.
495495
let mut outlives_requirements =
496-
if infcx.tcx.is_closure(mir_def_id) { Some(vec![]) } else { None };
496+
infcx.tcx.is_closure(mir_def_id).to_option_with(|| vec![]);
497497

498498
self.check_type_tests(
499499
infcx,
@@ -709,14 +709,11 @@ impl<'tcx> RegionInferenceContext<'tcx> {
709709
let min = |r1: ty::RegionVid, r2: ty::RegionVid| -> Option<ty::RegionVid> {
710710
let r1_outlives_r2 = self.universal_region_relations.outlives(r1, r2);
711711
let r2_outlives_r1 = self.universal_region_relations.outlives(r2, r1);
712-
if r1_outlives_r2 && r2_outlives_r1 {
713-
Some(r1.min(r2))
714-
} else if r1_outlives_r2 {
715-
Some(r2)
716-
} else if r2_outlives_r1 {
717-
Some(r1)
718-
} else {
719-
None
712+
match (r1_outlives_r2, r2_outlives_r1) {
713+
(true, true) => Some(r1.min(r2)),
714+
(true, false) => Some(r2),
715+
(false, true) => Some(r1),
716+
(false, false) => None,
720717
}
721718
};
722719
let mut min_choice = choice_regions[0];

src/librustc_mir/build/matches/test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
680680
}
681681
})();
682682

683-
if no_overlap == Some(true) {
683+
if let Some(true) = no_overlap {
684684
// Testing range does not overlap with pattern range,
685685
// so the pattern can be matched only if this test fails.
686686
Some(1)
@@ -690,7 +690,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
690690
}
691691

692692
(&TestKind::Range(range), &PatKind::Constant { value }) => {
693-
if self.const_range_contains(range, value) == Some(false) {
693+
if let Some(false) = self.const_range_contains(range, value) {
694694
// `value` is not contained in the testing range,
695695
// so `value` can be matched only if this test fails.
696696
Some(1)

0 commit comments

Comments
 (0)