Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Use to_option in various places
  • Loading branch information
varkor committed Dec 6, 2019
1 parent 51901ee commit e3a8ea4
Show file tree
Hide file tree
Showing 51 changed files with 81 additions and 236 deletions.
7 changes: 2 additions & 5 deletions src/libfmt_macros/lib.rs
Expand Up @@ -11,6 +11,7 @@
#![feature(nll)]
#![feature(rustc_private)]
#![feature(unicode_internals)]
#![feature(bool_to_option)]

pub use Piece::*;
pub use Position::*;
Expand Down Expand Up @@ -644,11 +645,7 @@ impl<'a> Parser<'a> {
break;
}
}
if found {
Some(cur)
} else {
None
}
found.to_option(cur)
}
}

Expand Down
8 changes: 1 addition & 7 deletions src/librustc/hir/map/blocks.rs
Expand Up @@ -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 {
Expand Down
6 changes: 1 addition & 5 deletions src/librustc/infer/outlives/verify.rs
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/librustc/lib.rs
Expand Up @@ -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)]
Expand Down
6 changes: 1 addition & 5 deletions src/librustc/mir/mod.rs
Expand Up @@ -242,11 +242,7 @@ impl<'tcx> Body<'tcx> {
pub fn vars_iter<'a>(&'a self) -> impl Iterator<Item = Local> + '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().to_option(local)
})
}

Expand Down
6 changes: 1 addition & 5 deletions src/librustc/traits/error_reporting.rs
Expand Up @@ -363,11 +363,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 describe_generator(&self, body_id: hir::BodyId) -> Option<&'static str> {
Expand Down
8 changes: 2 additions & 6 deletions src/librustc/ty/mod.rs
Expand Up @@ -2784,11 +2784,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,
Expand Down Expand Up @@ -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.to_option(def_id),
);

let body_id = tcx.hir().as_local_hir_id(def_id).map_or(hir::DUMMY_HIR_ID, |id| {
Expand Down
9 changes: 2 additions & 7 deletions src/librustc/ty/query/job.rs
Expand Up @@ -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).to_option(None))
.is_some()
}

// Deterministically pick an query from a list
Expand Down
6 changes: 1 addition & 5 deletions src/librustc_codegen_llvm/attributes.rs
Expand Up @@ -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::<FxHashMap<_, _>>();

let mut ret = FxHashMap::default();
Expand Down
6 changes: 1 addition & 5 deletions src/librustc_codegen_llvm/common.rs
Expand Up @@ -245,11 +245,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))
})
}

Expand Down
1 change: 1 addition & 0 deletions src/librustc_codegen_llvm/lib.rs
Expand Up @@ -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)]
Expand Down
6 changes: 1 addition & 5 deletions src/librustc_codegen_ssa/back/rpath.rs
Expand Up @@ -119,11 +119,7 @@ fn path_relative_from(path: &Path, base: &Path) -> Option<PathBuf> {
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(PathBuf::from(path))
} else {
let mut ita = path.components();
let mut itb = base.components();
Expand Down
6 changes: 1 addition & 5 deletions src/librustc_codegen_ssa/back/symbol_export.rs
Expand Up @@ -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.
Expand Down
22 changes: 6 additions & 16 deletions 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)]
Expand Down Expand Up @@ -68,22 +69,11 @@ impl<M> ModuleCodegen<M> {
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(outputs.temp_path(OutputType::Object, Some(&self.name)));
let bytecode = emit_bc.to_option(outputs.temp_path(OutputType::Bitcode, Some(&self.name)));
let bytecode_compressed = emit_bc_compressed.to_option(
outputs.temp_path(OutputType::Bitcode, Some(&self.name))
.with_extension(RLIB_BYTECODE_EXTENSION));

CompiledModule {
name: self.name.clone(),
Expand Down
1 change: 1 addition & 0 deletions src/librustc_interface/lib.rs
@@ -1,3 +1,4 @@
#![feature(bool_to_option)]
#![feature(box_syntax)]
#![feature(set_stdio)]
#![feature(nll)]
Expand Down
8 changes: 1 addition & 7 deletions src/librustc_interface/passes.rs
Expand Up @@ -547,13 +547,7 @@ fn output_contains_path(output_paths: &[PathBuf], input_path: &PathBuf) -> bool
}

fn output_conflicts_with_dir(output_paths: &[PathBuf]) -> Option<PathBuf> {
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(output_path.clone());
check_output(output_paths, check)
}

Expand Down
8 changes: 3 additions & 5 deletions src/librustc_interface/queries.rs
Expand Up @@ -117,11 +117,9 @@ impl<'tcx> Queries<'tcx> {

pub fn dep_graph_future(&self) -> Result<&Query<Option<DepGraphFuture>>> {
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().to_option_with(|| {
rustc_incremental::load_dep_graph(self.session())
}))
})
}

Expand Down
12 changes: 2 additions & 10 deletions src/librustc_interface/util.rs
Expand Up @@ -107,11 +107,7 @@ const STACK_SIZE: usize = 16 * 1024 * 1024;
fn get_stack_size() -> Option<usize> {
// 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<Mutex<Vec<u8>>>);
Expand Down Expand Up @@ -285,11 +281,7 @@ fn get_rustc_path_inner(bin_path: &str) -> Option<PathBuf> {
} else {
"rustc"
});
if candidate.exists() {
Some(candidate)
} else {
None
}
candidate.exists().to_option(candidate)
})
.next()
}
Expand Down
12 changes: 2 additions & 10 deletions src/librustc_lint/builtin.rs
Expand Up @@ -1491,11 +1491,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
}
Expand Down Expand Up @@ -1554,11 +1550,7 @@ impl ExplicitOutlivesRequirements {
}),
_ => false,
};
if is_inferred {
Some((i, bound.span()))
} else {
None
}
is_inferred.to_option((i, bound.span()))
} else {
None
}
Expand Down
1 change: 1 addition & 0 deletions src/librustc_lint/lib.rs
Expand Up @@ -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)]
Expand Down
7 changes: 2 additions & 5 deletions src/librustc_metadata/creader.rs
Expand Up @@ -802,11 +802,8 @@ 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 self.cstore.has_global_allocator {
Some(Symbol::intern("this crate"))
} else {
None
};
let mut global_allocator = self.cstore.has_global_allocator
.to_option_with(|| Symbol::intern("this crate"));
self.cstore.iter_crate_data(|_, data| {
if !data.has_global_allocator() {
return
Expand Down
1 change: 1 addition & 0 deletions 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)]
Expand Down
6 changes: 1 addition & 5 deletions src/librustc_mir/borrow_check/nll/mod.rs
Expand Up @@ -173,11 +173,7 @@ pub(in crate::borrow_check) fn compute_regions<'cx, 'tcx>(
Option<Rc<Output<RegionVid, BorrowIndex, LocationIndex, Local, MovePathIndex>>>,
Option<ClosureRegionRequirements<'tcx>>,
) {
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);

Expand Down
15 changes: 6 additions & 9 deletions src/librustc_mir/borrow_check/nll/region_infer/mod.rs
Expand Up @@ -493,7 +493,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,
Expand Down Expand Up @@ -709,14 +709,11 @@ impl<'tcx> RegionInferenceContext<'tcx> {
let min = |r1: ty::RegionVid, r2: ty::RegionVid| -> Option<ty::RegionVid> {
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];
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_mir/build/matches/test.rs
Expand Up @@ -680,7 +680,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)
Expand All @@ -690,7 +690,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)
Expand Down

0 comments on commit e3a8ea4

Please sign in to comment.