Skip to content

Commit 3a3c7b5

Browse files
authored
Rollup merge of #149795 - estebank:let-else-std, r=workingjubilee
Use `let`...`else` instead of `match foo { ... _ => return };` and `if let ... else return` in std Split off #148837.
2 parents 087ca1f + 0488690 commit 3a3c7b5

File tree

7 files changed

+21
-36
lines changed

7 files changed

+21
-36
lines changed

library/alloc/src/collections/linked_list.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1646,9 +1646,8 @@ impl<'a, T> CursorMut<'a, T> {
16461646
#[unstable(feature = "linked_list_cursors", issue = "58533")]
16471647
pub fn splice_after(&mut self, list: LinkedList<T>) {
16481648
unsafe {
1649-
let (splice_head, splice_tail, splice_len) = match list.detach_all_nodes() {
1650-
Some(parts) => parts,
1651-
_ => return,
1649+
let Some((splice_head, splice_tail, splice_len)) = list.detach_all_nodes() else {
1650+
return;
16521651
};
16531652
let node_next = match self.current {
16541653
None => self.list.head,

library/alloc/src/raw_vec/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -788,9 +788,7 @@ impl<A: Allocator> RawVecInner<A> {
788788
elem_layout: Layout,
789789
) -> Result<(), TryReserveError> {
790790
// SAFETY: Precondition passed to caller
791-
let (ptr, layout) = if let Some(mem) = unsafe { self.current_memory(elem_layout) } {
792-
mem
793-
} else {
791+
let Some((ptr, layout)) = (unsafe { self.current_memory(elem_layout) }) else {
794792
return Ok(());
795793
};
796794

library/alloc/src/string.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -619,16 +619,14 @@ impl String {
619619
pub fn from_utf8_lossy(v: &[u8]) -> Cow<'_, str> {
620620
let mut iter = v.utf8_chunks();
621621

622-
let first_valid = if let Some(chunk) = iter.next() {
623-
let valid = chunk.valid();
624-
if chunk.invalid().is_empty() {
625-
debug_assert_eq!(valid.len(), v.len());
626-
return Cow::Borrowed(valid);
627-
}
628-
valid
629-
} else {
622+
let Some(chunk) = iter.next() else {
630623
return Cow::Borrowed("");
631624
};
625+
let first_valid = chunk.valid();
626+
if chunk.invalid().is_empty() {
627+
debug_assert_eq!(first_valid.len(), v.len());
628+
return Cow::Borrowed(first_valid);
629+
}
632630

633631
const REPLACEMENT: &str = "\u{FFFD}";
634632

@@ -720,11 +718,10 @@ impl String {
720718
// FIXME: the function can be simplified again when #48994 is closed.
721719
let mut ret = String::with_capacity(v.len());
722720
for c in char::decode_utf16(v.iter().cloned()) {
723-
if let Ok(c) = c {
724-
ret.push(c);
725-
} else {
721+
let Ok(c) = c else {
726722
return Err(FromUtf16Error(()));
727-
}
723+
};
724+
ret.push(c);
728725
}
729726
Ok(ret)
730727
}

library/alloc/src/vec/splice.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,11 @@ impl<T, A: Allocator> Drain<'_, T, A> {
112112
};
113113

114114
for place in range_slice {
115-
if let Some(new_item) = replace_with.next() {
116-
unsafe { ptr::write(place, new_item) };
117-
vec.len += 1;
118-
} else {
115+
let Some(new_item) = replace_with.next() else {
119116
return false;
120-
}
117+
};
118+
unsafe { ptr::write(place, new_item) };
119+
vec.len += 1;
121120
}
122121
true
123122
}

library/core/src/num/dec2flt/mod.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -255,11 +255,7 @@ fn biased_fp_to_float<F: RawFloat>(x: BiasedFp) -> F {
255255
#[inline(always)] // Will be inlined into a function with `#[inline(never)]`, see above
256256
pub fn dec2flt<F: RawFloat>(s: &str) -> Result<F, ParseFloatError> {
257257
let mut s = s.as_bytes();
258-
let c = if let Some(&c) = s.first() {
259-
c
260-
} else {
261-
return Err(pfe_empty());
262-
};
258+
let Some(&c) = s.first() else { return Err(pfe_empty()) };
263259
let negative = c == b'-';
264260
if c == b'-' || c == b'+' {
265261
s = &s[1..];

library/core/src/time.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -672,11 +672,10 @@ impl Duration {
672672
let mut nanos = self.nanos.as_inner() + rhs.nanos.as_inner();
673673
if nanos >= NANOS_PER_SEC {
674674
nanos -= NANOS_PER_SEC;
675-
if let Some(new_secs) = secs.checked_add(1) {
676-
secs = new_secs;
677-
} else {
675+
let Some(new_secs) = secs.checked_add(1) else {
678676
return None;
679-
}
677+
};
678+
secs = new_secs;
680679
}
681680
debug_assert!(nanos < NANOS_PER_SEC);
682681
Some(Duration::new(secs, nanos))

library/std/src/sys/pal/windows/pipe.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -530,10 +530,7 @@ impl<'a> AsyncPipe<'a> {
530530

531531
impl<'a> Drop for AsyncPipe<'a> {
532532
fn drop(&mut self) {
533-
match self.state {
534-
State::Reading => {}
535-
_ => return,
536-
}
533+
let State::Reading = self.state else { return };
537534

538535
// If we have a pending read operation, then we have to make sure that
539536
// it's *done* before we actually drop this type. The kernel requires

0 commit comments

Comments
 (0)