1
Fork 0

if $c:expr { Some($r:expr) } else { None } =>> $c.then(|| $r)

This commit is contained in:
Maybe Waffle 2023-02-15 11:43:41 +00:00
parent af3c8b2726
commit 8751fa1a9a
54 changed files with 159 additions and 281 deletions

View file

@ -319,7 +319,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
// See the notes for `ExprKind::Array` in `as_rvalue` and for
// `ExprKind::Borrow` 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.then(|| fields[0].name.index());
let scope = this.local_scope();

View file

@ -563,14 +563,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
let not_contained =
self.values_not_contained_in_range(&*range, options).unwrap_or(false);
if not_contained {
not_contained.then(|| {
// No switch values are contained in the pattern range,
// so the pattern can be matched only if this test fails.
let otherwise = options.len();
Some(otherwise)
} else {
None
}
options.len()
})
}
(&TestKind::SwitchInt { .. }, _) => None,

View file

@ -172,7 +172,7 @@ impl IntRange {
ty: Ty<'tcx>,
end: &RangeEnd,
) -> Option<IntRange> {
if Self::is_integral(ty) {
Self::is_integral(ty).then(|| {
// Perform a shift if the underlying types are signed,
// which makes the interval arithmetic simpler.
let bias = IntRange::signed_bias(tcx, ty);
@ -182,10 +182,8 @@ impl IntRange {
// This should have been caught earlier by E0030.
bug!("malformed range pattern: {}..={}", lo, (hi - offset));
}
Some(IntRange { range: lo..=(hi - offset), bias })
} else {
None
}
IntRange { range: lo..=(hi - offset), bias }
})
}
// The return value of `signed_bias` should be XORed with an endpoint to encode/decode it.

View file

@ -203,11 +203,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
if !lower_overflow && !higher_overflow {
self.tcx.sess.emit_err(LowerRangeBoundMustBeLessThanOrEqualToUpper {
span,
teach: if self.tcx.sess.teach(&error_code!(E0030)) {
Some(())
} else {
None
},
teach: self.tcx.sess.teach(&error_code!(E0030)).then(|| ()),
});
}
PatKind::Wild