Skip to content

Commit 9f1269f

Browse files
committed
Rename to then_some and then
1 parent 8579fe6 commit 9f1269f

File tree

37 files changed

+57
-57
lines changed

37 files changed

+57
-57
lines changed

src/libcore/bool.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ impl bool {
99
/// ```
1010
/// #![feature(bool_to_option)]
1111
///
12-
/// assert_eq!(false.to_option(0), None);
13-
/// assert_eq!(true.to_option(0), Some(0));
12+
/// assert_eq!(false.then_some(0), None);
13+
/// assert_eq!(true.then_some(0), Some(0));
1414
/// ```
1515
#[unstable(feature = "bool_to_option", issue = "64260")]
1616
#[inline]
17-
pub fn to_option<T>(self, t: T) -> Option<T> {
17+
pub fn then_some<T>(self, t: T) -> Option<T> {
1818
if self {
1919
Some(t)
2020
} else {
@@ -29,12 +29,12 @@ impl bool {
2929
/// ```
3030
/// #![feature(bool_to_option)]
3131
///
32-
/// assert_eq!(false.to_option_with(|| 0), None);
33-
/// assert_eq!(true.to_option_with(|| 0), Some(0));
32+
/// assert_eq!(false.then(|| 0), None);
33+
/// assert_eq!(true.then(|| 0), Some(0));
3434
/// ```
3535
#[unstable(feature = "bool_to_option", issue = "64260")]
3636
#[inline]
37-
pub fn to_option_with<T, F: FnOnce() -> T>(self, f: F) -> Option<T> {
37+
pub fn then<T, F: FnOnce() -> T>(self, f: F) -> Option<T> {
3838
if self {
3939
Some(f())
4040
} else {

src/libcore/tests/bool.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#[test]
22
fn test_bool_to_option() {
3-
assert_eq!(false.to_option(0), None);
4-
assert_eq!(true.to_option(0), Some(0));
5-
assert_eq!(false.to_option_with(|| 0), None);
6-
assert_eq!(true.to_option_with(|| 0), Some(0));
3+
assert_eq!(false.then_some(0), None);
4+
assert_eq!(true.then_some(0), Some(0));
5+
assert_eq!(false.then(|| 0), None);
6+
assert_eq!(true.then(|| 0), Some(0));
77
}

src/libfmt_macros/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,7 @@ impl<'a> Parser<'a> {
645645
break;
646646
}
647647
}
648-
found.to_option(cur)
648+
found.then_some(cur)
649649
}
650650
}
651651

src/librustc/hir/map/blocks.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ impl<'a> FnLikeNode<'a> {
147147
map::Node::Expr(e) => e.is_fn_like(),
148148
_ => false
149149
};
150-
fn_like.to_option(FnLikeNode { node })
150+
fn_like.then_some(FnLikeNode { node })
151151
}
152152

153153
pub fn body(self) -> ast::BodyId {

src/librustc/infer/outlives/verify.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ impl<'cx, 'tcx> VerifyBoundCx<'cx, 'tcx> {
211211
(r, p)
212212
);
213213
let p_ty = p.to_ty(tcx);
214-
compare_ty(p_ty).to_option(ty::OutlivesPredicate(p_ty, r))
214+
compare_ty(p_ty).then_some(ty::OutlivesPredicate(p_ty, r))
215215
});
216216

217217
param_bounds

src/librustc/traits/error_reporting.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
363363
return None
364364
};
365365

366-
tcx.has_attr(impl_def_id, sym::rustc_on_unimplemented).to_option(impl_def_id)
366+
tcx.has_attr(impl_def_id, sym::rustc_on_unimplemented).then_some(impl_def_id)
367367
}
368368

369369
fn describe_generator(&self, body_id: hir::BodyId) -> Option<&'static str> {

src/librustc/ty/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2784,7 +2784,7 @@ impl<'tcx> TyCtxt<'tcx> {
27842784
}
27852785
};
27862786

2787-
is_associated_item.to_option_with(|| self.associated_item(def_id))
2787+
is_associated_item.then(|| self.associated_item(def_id))
27882788
}
27892789

27902790
fn associated_item_from_trait_item_ref(self,
@@ -3249,7 +3249,7 @@ fn param_env(tcx: TyCtxt<'_>, def_id: DefId) -> ParamEnv<'_> {
32493249
let unnormalized_env = ty::ParamEnv::new(
32503250
tcx.intern_predicates(&predicates),
32513251
traits::Reveal::UserFacing,
3252-
tcx.sess.opts.debugging_opts.chalk.to_option(def_id),
3252+
tcx.sess.opts.debugging_opts.chalk.then_some(def_id),
32533253
);
32543254

32553255
let body_id = tcx.hir().as_local_hir_id(def_id).map_or(hir::DUMMY_HIR_ID, |id| {

src/librustc/ty/query/job.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ fn connected_to_root<'tcx>(
303303
return true;
304304
}
305305

306-
visit_waiters(query, |_, successor| connected_to_root(successor, visited).to_option(None))
306+
visit_waiters(query, |_, successor| connected_to_root(successor, visited).then_some(None))
307307
.is_some()
308308
}
309309

src/librustc_codegen_llvm/common.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +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-
success.to_option(hi_lo_to_u128(lo, hi))
248+
success.then_some(hi_lo_to_u128(lo, hi))
249249
})
250250
}
251251

src/librustc_codegen_ssa/back/rpath.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +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-
path.is_absolute().to_option_with(|| PathBuf::from(path))
122+
path.is_absolute().then(|| PathBuf::from(path))
123123
} else {
124124
let mut ita = path.components();
125125
let mut itb = base.components();

src/librustc_codegen_ssa/back/symbol_export.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +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-
tcx.is_statically_included_foreign_item(def_id).to_option(def_id)
88+
tcx.is_statically_included_foreign_item(def_id).then_some(def_id)
8989
}
9090

9191
// Only consider nodes that actually have exported symbols.

src/librustc_codegen_ssa/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ impl<M> ModuleCodegen<M> {
7070
emit_bc_compressed: bool,
7171
outputs: &OutputFilenames) -> CompiledModule {
7272
let object = emit_obj
73-
.to_option_with(|| outputs.temp_path(OutputType::Object, Some(&self.name)));
73+
.then(|| outputs.temp_path(OutputType::Object, Some(&self.name)));
7474
let bytecode = emit_bc
75-
.to_option_with(|| outputs.temp_path(OutputType::Bitcode, Some(&self.name)));
76-
let bytecode_compressed = emit_bc_compressed.to_option_with(|| {
75+
.then(|| outputs.temp_path(OutputType::Bitcode, Some(&self.name)));
76+
let bytecode_compressed = emit_bc_compressed.then(|| {
7777
outputs.temp_path(OutputType::Bitcode, Some(&self.name))
7878
.with_extension(RLIB_BYTECODE_EXTENSION)
7979
});

src/librustc_interface/passes.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -547,7 +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| output_path.is_dir().to_option_with(|| output_path.clone());
550+
let check = |output_path: &PathBuf| output_path.is_dir().then(|| output_path.clone());
551551
check_output(output_paths, check)
552552
}
553553

src/librustc_interface/queries.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ impl<'tcx> Queries<'tcx> {
117117

118118
pub fn dep_graph_future(&self) -> Result<&Query<Option<DepGraphFuture>>> {
119119
self.dep_graph_future.compute(|| {
120-
Ok(self.session().opts.build_dep_graph().to_option_with(|| {
120+
Ok(self.session().opts.build_dep_graph().then(|| {
121121
rustc_incremental::load_dep_graph(self.session())
122122
}))
123123
})

src/librustc_interface/util.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -107,7 +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-
env::var_os("RUST_MIN_STACK").is_none().to_option(STACK_SIZE)
110+
env::var_os("RUST_MIN_STACK").is_none().then_some(STACK_SIZE)
111111
}
112112

113113
struct Sink(Arc<Mutex<Vec<u8>>>);
@@ -281,7 +281,7 @@ fn get_rustc_path_inner(bin_path: &str) -> Option<PathBuf> {
281281
} else {
282282
"rustc"
283283
});
284-
candidate.exists().to_option(candidate)
284+
candidate.exists().then_some(candidate)
285285
})
286286
.next()
287287
}

src/librustc_lint/builtin.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1491,7 +1491,7 @@ impl ExplicitOutlivesRequirements {
14911491
match pred {
14921492
ty::Predicate::TypeOutlives(outlives) => {
14931493
let outlives = outlives.skip_binder();
1494-
outlives.0.is_param(index).to_option(outlives.1)
1494+
outlives.0.is_param(index).then_some(outlives.1)
14951495
}
14961496
_ => None
14971497
}
@@ -1550,7 +1550,7 @@ impl ExplicitOutlivesRequirements {
15501550
}),
15511551
_ => false,
15521552
};
1553-
is_inferred.to_option((i, bound.span()))
1553+
is_inferred.then_some((i, bound.span()))
15541554
} else {
15551555
None
15561556
}

src/librustc_metadata/creader.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,7 @@ impl<'a> CrateLoader<'a> {
803803
// and see what's a global allocator, including if we ourselves are a
804804
// global allocator.
805805
let mut global_allocator = self.cstore.has_global_allocator
806-
.to_option_with(|| Symbol::intern("this crate"));
806+
.then(|| Symbol::intern("this crate"));
807807
self.cstore.iter_crate_data(|_, data| {
808808
if !data.has_global_allocator() {
809809
return

src/librustc_mir/borrow_check/nll/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +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 = AllFacts::enabled(infcx.tcx).to_option(AllFacts::default());
176+
let mut all_facts = AllFacts::enabled(infcx.tcx).then_some(AllFacts::default());
177177

178178
let universal_regions = Rc::new(universal_regions);
179179

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

+1-1
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-
infcx.tcx.is_closure(mir_def_id).to_option_with(|| vec![]);
496+
infcx.tcx.is_closure(mir_def_id).then(|| vec![]);
497497

498498
self.check_type_tests(
499499
infcx,

src/librustc_mir/hair/pattern/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1154,7 +1154,7 @@ pub fn compare_const_vals<'tcx>(
11541154
) -> Option<Ordering> {
11551155
trace!("compare_const_vals: {:?}, {:?}", a, b);
11561156

1157-
let from_bool = |v: bool| v.to_option(Ordering::Equal);
1157+
let from_bool = |v: bool| v.then_some(Ordering::Equal);
11581158

11591159
let fallback = || from_bool(a == b);
11601160

src/librustc_mir/interpret/memory.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
324324
size: Size,
325325
align: Align,
326326
) -> InterpResult<'tcx, Option<Pointer<M::PointerTag>>> {
327-
let align = M::CHECK_ALIGN.to_option(align);
327+
let align = M::CHECK_ALIGN.then_some(align);
328328
self.check_ptr_access_align(sptr, size, align, CheckInAllocMsg::MemoryAccessTest)
329329
}
330330

src/librustc_mir/interpret/step.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,9 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
157157
}
158158

159159
BinaryOp(bin_op, ref left, ref right) => {
160-
let layout = binop_left_homogeneous(bin_op).to_option(dest.layout);
160+
let layout = binop_left_homogeneous(bin_op).then_some(dest.layout);
161161
let left = self.read_immediate(self.eval_operand(left, layout)?)?;
162-
let layout = binop_right_homogeneous(bin_op).to_option(left.layout);
162+
let layout = binop_right_homogeneous(bin_op).then_some(left.layout);
163163
let right = self.read_immediate(self.eval_operand(right, layout)?)?;
164164
self.binop_ignore_overflow(
165165
bin_op,
@@ -172,7 +172,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
172172
CheckedBinaryOp(bin_op, ref left, ref right) => {
173173
// Due to the extra boolean in the result, we can never reuse the `dest.layout`.
174174
let left = self.read_immediate(self.eval_operand(left, None)?)?;
175-
let layout = binop_right_homogeneous(bin_op).to_option(left.layout);
175+
let layout = binop_right_homogeneous(bin_op).then_some(left.layout);
176176
let right = self.read_immediate(self.eval_operand(right, layout)?)?;
177177
self.binop_with_overflow(
178178
bin_op,

src/librustc_mir/monomorphize/partitioning.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,7 @@ fn compute_codegen_unit_name(
761761
.iter()
762762
.map(|part| part.data.as_symbol());
763763

764-
let volatile_suffix = volatile.to_option("volatile");
764+
let volatile_suffix = volatile.then_some("volatile");
765765

766766
name_builder.build_cgu_name(def_path.krate, components, volatile_suffix)
767767
}).clone()

src/librustc_mir/transform/check_unsafety.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@ pub fn check_unsafety(tcx: TyCtxt<'_>, def_id: DefId) {
658658
let mut unsafe_blocks: Vec<_> = unsafe_blocks.into_iter().collect();
659659
unsafe_blocks.sort_by_cached_key(|(hir_id, _)| tcx.hir().hir_to_node_id(*hir_id));
660660
let used_unsafe: FxHashSet<_> = unsafe_blocks.iter()
661-
.flat_map(|&&(id, used)| used.to_option(id))
661+
.flat_map(|&&(id, used)| used.then_some(id))
662662
.collect();
663663
for &(block_id, is_used) in unsafe_blocks {
664664
if !is_used {

src/librustc_parse/config.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ macro_rules! configure {
7575
impl<'a> StripUnconfigured<'a> {
7676
pub fn configure<T: HasAttrs>(&mut self, mut node: T) -> Option<T> {
7777
self.process_cfg_attrs(&mut node);
78-
self.in_cfg(node.attrs()).to_option(node)
78+
self.in_cfg(node.attrs()).then_some(node)
7979
}
8080

8181
/// Parse and expand all `cfg_attr` attributes into a list of attributes

src/librustc_resolve/diagnostics.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ impl<'a> Resolver<'a> {
422422
Scope::MacroUsePrelude => {
423423
suggestions.extend(this.macro_use_prelude.iter().filter_map(|(name, binding)| {
424424
let res = binding.res();
425-
filter_fn(res).to_option(TypoSuggestion::from_res(*name, res))
425+
filter_fn(res).then_some(TypoSuggestion::from_res(*name, res))
426426
}));
427427
}
428428
Scope::BuiltinAttrs => {
@@ -436,7 +436,7 @@ impl<'a> Resolver<'a> {
436436
Scope::ExternPrelude => {
437437
suggestions.extend(this.extern_prelude.iter().filter_map(|(ident, _)| {
438438
let res = Res::Def(DefKind::Mod, DefId::local(CRATE_DEF_INDEX));
439-
filter_fn(res).to_option(TypoSuggestion::from_res(ident.name, res))
439+
filter_fn(res).then_some(TypoSuggestion::from_res(ident.name, res))
440440
}));
441441
}
442442
Scope::ToolPrelude => {
@@ -459,7 +459,7 @@ impl<'a> Resolver<'a> {
459459
suggestions.extend(
460460
primitive_types.iter().flat_map(|(name, prim_ty)| {
461461
let res = Res::PrimTy(*prim_ty);
462-
filter_fn(res).to_option(TypoSuggestion::from_res(*name, res))
462+
filter_fn(res).then_some(TypoSuggestion::from_res(*name, res))
463463
})
464464
)
465465
}

src/librustc_resolve/late.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LateResolutionVisitor<'a, '_> {
496496
GenericParamKind::Lifetime { .. } => None,
497497
GenericParamKind::Type { ref default, .. } => {
498498
found_default |= default.is_some();
499-
found_default.to_option((Ident::with_dummy_span(param.ident.name), Res::Err))
499+
found_default.then_some((Ident::with_dummy_span(param.ident.name), Res::Err))
500500
}
501501
}));
502502

src/librustc_target/abi/call/aarch64.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ fn is_homogeneous_aggregate<'a, Ty, C>(cx: &C, arg: &mut ArgAbi<'a, Ty>)
2020
RegKind::Vector => size.bits() == 64 || size.bits() == 128
2121
};
2222

23-
valid_unit.to_option(Uniform { unit, total: size })
23+
valid_unit.then_some(Uniform { unit, total: size })
2424
})
2525
}
2626

src/librustc_target/abi/call/arm.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ fn is_homogeneous_aggregate<'a, Ty, C>(cx: &C, arg: &mut ArgAbi<'a, Ty>)
2121
RegKind::Vector => size.bits() == 64 || size.bits() == 128
2222
};
2323

24-
valid_unit.to_option(Uniform { unit, total: size })
24+
valid_unit.then_some(Uniform { unit, total: size })
2525
})
2626
}
2727

src/librustc_target/abi/call/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ impl<'a, Ty> ArgAbi<'a, Ty> {
416416
// i686-pc-windows-msvc, it results in wrong stack offsets.
417417
// attrs.pointee_align = Some(self.layout.align.abi);
418418

419-
let extra_attrs = self.layout.is_unsized().to_option(ArgAttributes::new());
419+
let extra_attrs = self.layout.is_unsized().then_some(ArgAttributes::new());
420420

421421
self.mode = PassMode::Indirect(attrs, extra_attrs);
422422
}

src/librustc_target/abi/call/powerpc64.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ fn is_homogeneous_aggregate<'a, Ty, C>(cx: &C, arg: &mut ArgAbi<'a, Ty>, abi: AB
3232
RegKind::Vector => arg.layout.size.bits() == 128
3333
};
3434

35-
valid_unit.to_option(Uniform { unit, total: arg.layout.size })
35+
valid_unit.then_some(Uniform { unit, total: arg.layout.size })
3636
})
3737
}
3838

src/librustc_target/abi/call/sparc64.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ fn is_homogeneous_aggregate<'a, Ty, C>(cx: &C, arg: &mut ArgAbi<'a, Ty>)
2020
RegKind::Vector => arg.layout.size.bits() == 128
2121
};
2222

23-
valid_unit.to_option(Uniform { unit, total: arg.layout.size })
23+
valid_unit.then_some(Uniform { unit, total: arg.layout.size })
2424
})
2525
}
2626

src/librustc_typeck/check/method/probe.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1105,7 +1105,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
11051105
r.map(|mut pick| {
11061106
pick.autoderefs = step.autoderefs;
11071107
pick.autoref = Some(mutbl);
1108-
pick.unsize = step.unsize.to_option(self_ty);
1108+
pick.unsize = step.unsize.then_some(self_ty);
11091109
pick
11101110
})
11111111
})

src/librustc_typeck/check/upvar.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
114114
};
115115

116116
let infer_kind = if let UpvarSubsts::Closure(closure_substs) = substs {
117-
self.closure_kind(closure_def_id, closure_substs).is_none().to_option(closure_substs)
117+
self.closure_kind(closure_def_id, closure_substs).is_none().then_some(closure_substs)
118118
} else {
119119
None
120120
};

0 commit comments

Comments
 (0)