Skip to content

Commit 7dca8eb

Browse files
committedAug 7, 2021
Use assert_matches! instead of if let {} else
·
1.87.01.56.0
1 parent 2157122 commit 7dca8eb

File tree

5 files changed

+281
-301
lines changed

5 files changed

+281
-301
lines changed
 

‎library/alloc/tests/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![feature(allocator_api)]
2+
#![feature(assert_matches)]
23
#![feature(box_syntax)]
34
#![feature(cow_is_borrowed)]
45
#![feature(const_cow_is_borrowed)]

‎library/alloc/tests/string.rs

Lines changed: 73 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::assert_matches::assert_matches;
12
use std::borrow::Cow;
23
use std::cell::Cell;
34
use std::collections::TryReserveErrorKind::*;
@@ -713,35 +714,32 @@ fn test_try_reserve() {
713714

714715
if guards_against_isize {
715716
// Check isize::MAX + 1 does count as overflow
716-
if let Err(CapacityOverflow) =
717-
empty_string.try_reserve(MAX_CAP + 1).map_err(|e| e.kind())
718-
{
719-
} else {
720-
panic!("isize::MAX + 1 should trigger an overflow!")
721-
}
717+
assert_matches!(
718+
empty_string.try_reserve(MAX_CAP + 1).map_err(|e| e.kind()),
719+
Err(CapacityOverflow),
720+
"isize::MAX + 1 should trigger an overflow!"
721+
);
722722

723723
// Check usize::MAX does count as overflow
724-
if let Err(CapacityOverflow) = empty_string.try_reserve(MAX_USIZE).map_err(|e| e.kind())
725-
{
726-
} else {
727-
panic!("usize::MAX should trigger an overflow!")
728-
}
724+
assert_matches!(
725+
empty_string.try_reserve(MAX_USIZE).map_err(|e| e.kind()),
726+
Err(CapacityOverflow),
727+
"usize::MAX should trigger an overflow!"
728+
);
729729
} else {
730730
// Check isize::MAX + 1 is an OOM
731-
if let Err(AllocError { .. }) =
732-
empty_string.try_reserve(MAX_CAP + 1).map_err(|e| e.kind())
733-
{
734-
} else {
735-
panic!("isize::MAX + 1 should trigger an OOM!")
736-
}
731+
assert_matches!(
732+
empty_string.try_reserve(MAX_CAP + 1).map_err(|e| e.kind()),
733+
Err(AllocError { .. }),
734+
"isize::MAX + 1 should trigger an OOM!"
735+
);
737736

738737
// Check usize::MAX is an OOM
739-
if let Err(AllocError { .. }) =
740-
empty_string.try_reserve(MAX_USIZE).map_err(|e| e.kind())
741-
{
742-
} else {
743-
panic!("usize::MAX should trigger an OOM!")
744-
}
738+
assert_matches!(
739+
empty_string.try_reserve(MAX_USIZE).map_err(|e| e.kind()),
740+
Err(AllocError { .. }),
741+
"usize::MAX should trigger an OOM!"
742+
);
745743
}
746744
}
747745

@@ -756,23 +754,24 @@ fn test_try_reserve() {
756754
panic!("isize::MAX shouldn't trigger an overflow!");
757755
}
758756
if guards_against_isize {
759-
if let Err(CapacityOverflow) = ten_bytes.try_reserve(MAX_CAP - 9).map_err(|e| e.kind())
760-
{
761-
} else {
762-
panic!("isize::MAX + 1 should trigger an overflow!");
763-
}
757+
assert_matches!(
758+
ten_bytes.try_reserve(MAX_CAP - 9).map_err(|e| e.kind()),
759+
Err(CapacityOverflow),
760+
"isize::MAX + 1 should trigger an overflow!"
761+
);
764762
} else {
765-
if let Err(AllocError { .. }) = ten_bytes.try_reserve(MAX_CAP - 9).map_err(|e| e.kind())
766-
{
767-
} else {
768-
panic!("isize::MAX + 1 should trigger an OOM!")
769-
}
763+
assert_matches!(
764+
ten_bytes.try_reserve(MAX_CAP - 9).map_err(|e| e.kind()),
765+
Err(AllocError { .. }),
766+
"isize::MAX + 1 should trigger an OOM!"
767+
);
770768
}
771769
// Should always overflow in the add-to-len
772-
if let Err(CapacityOverflow) = ten_bytes.try_reserve(MAX_USIZE).map_err(|e| e.kind()) {
773-
} else {
774-
panic!("usize::MAX should trigger an overflow!")
775-
}
770+
assert_matches!(
771+
ten_bytes.try_reserve(MAX_USIZE).map_err(|e| e.kind()),
772+
Err(CapacityOverflow),
773+
"usize::MAX should trigger an overflow!"
774+
);
776775
}
777776
}
778777

@@ -801,33 +800,29 @@ fn test_try_reserve_exact() {
801800
}
802801

803802
if guards_against_isize {
804-
if let Err(CapacityOverflow) =
805-
empty_string.try_reserve_exact(MAX_CAP + 1).map_err(|e| e.kind())
806-
{
807-
} else {
808-
panic!("isize::MAX + 1 should trigger an overflow!")
809-
}
810-
811-
if let Err(CapacityOverflow) =
812-
empty_string.try_reserve_exact(MAX_USIZE).map_err(|e| e.kind())
813-
{
814-
} else {
815-
panic!("usize::MAX should trigger an overflow!")
816-
}
803+
assert_matches!(
804+
empty_string.try_reserve_exact(MAX_CAP + 1).map_err(|e| e.kind()),
805+
Err(CapacityOverflow),
806+
"isize::MAX + 1 should trigger an overflow!"
807+
);
808+
809+
assert_matches!(
810+
empty_string.try_reserve_exact(MAX_USIZE).map_err(|e| e.kind()),
811+
Err(CapacityOverflow),
812+
"usize::MAX should trigger an overflow!"
813+
);
817814
} else {
818-
if let Err(AllocError { .. }) =
819-
empty_string.try_reserve_exact(MAX_CAP + 1).map_err(|e| e.kind())
820-
{
821-
} else {
822-
panic!("isize::MAX + 1 should trigger an OOM!")
823-
}
824-
825-
if let Err(AllocError { .. }) =
826-
empty_string.try_reserve_exact(MAX_USIZE).map_err(|e| e.kind())
827-
{
828-
} else {
829-
panic!("usize::MAX should trigger an OOM!")
830-
}
815+
assert_matches!(
816+
empty_string.try_reserve_exact(MAX_CAP + 1).map_err(|e| e.kind()),
817+
Err(AllocError { .. }),
818+
"isize::MAX + 1 should trigger an OOM!"
819+
);
820+
821+
assert_matches!(
822+
empty_string.try_reserve_exact(MAX_USIZE).map_err(|e| e.kind()),
823+
Err(AllocError { .. }),
824+
"usize::MAX should trigger an OOM!"
825+
);
831826
}
832827
}
833828

@@ -845,25 +840,23 @@ fn test_try_reserve_exact() {
845840
panic!("isize::MAX shouldn't trigger an overflow!");
846841
}
847842
if guards_against_isize {
848-
if let Err(CapacityOverflow) =
849-
ten_bytes.try_reserve_exact(MAX_CAP - 9).map_err(|e| e.kind())
850-
{
851-
} else {
852-
panic!("isize::MAX + 1 should trigger an overflow!");
853-
}
854-
} else {
855-
if let Err(AllocError { .. }) =
856-
ten_bytes.try_reserve_exact(MAX_CAP - 9).map_err(|e| e.kind())
857-
{
858-
} else {
859-
panic!("isize::MAX + 1 should trigger an OOM!")
860-
}
861-
}
862-
if let Err(CapacityOverflow) = ten_bytes.try_reserve_exact(MAX_USIZE).map_err(|e| e.kind())
863-
{
843+
assert_matches!(
844+
ten_bytes.try_reserve_exact(MAX_CAP - 9).map_err(|e| e.kind()),
845+
Err(CapacityOverflow),
846+
"isize::MAX + 1 should trigger an overflow!"
847+
);
864848
} else {
865-
panic!("usize::MAX should trigger an overflow!")
849+
assert_matches!(
850+
ten_bytes.try_reserve_exact(MAX_CAP - 9).map_err(|e| e.kind()),
851+
Err(AllocError { .. }),
852+
"isize::MAX + 1 should trigger an OOM!"
853+
);
866854
}
855+
assert_matches!(
856+
ten_bytes.try_reserve_exact(MAX_USIZE).map_err(|e| e.kind()),
857+
Err(CapacityOverflow),
858+
"usize::MAX should trigger an overflow!"
859+
);
867860
}
868861
}
869862

‎library/alloc/tests/vec.rs

Lines changed: 103 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::assert_matches::assert_matches;
12
use std::borrow::Cow;
23
use std::cell::Cell;
34
use std::collections::TryReserveErrorKind::*;
@@ -1488,34 +1489,32 @@ fn test_try_reserve() {
14881489

14891490
if guards_against_isize {
14901491
// Check isize::MAX + 1 does count as overflow
1491-
if let Err(CapacityOverflow) =
1492-
empty_bytes.try_reserve(MAX_CAP + 1).map_err(|e| e.kind())
1493-
{
1494-
} else {
1495-
panic!("isize::MAX + 1 should trigger an overflow!")
1496-
}
1492+
assert_matches!(
1493+
empty_bytes.try_reserve(MAX_CAP + 1).map_err(|e| e.kind()),
1494+
Err(CapacityOverflow),
1495+
"isize::MAX + 1 should trigger an overflow!"
1496+
);
14971497

14981498
// Check usize::MAX does count as overflow
1499-
if let Err(CapacityOverflow) = empty_bytes.try_reserve(MAX_USIZE).map_err(|e| e.kind())
1500-
{
1501-
} else {
1502-
panic!("usize::MAX should trigger an overflow!")
1503-
}
1499+
assert_matches!(
1500+
empty_bytes.try_reserve(MAX_USIZE).map_err(|e| e.kind()),
1501+
Err(CapacityOverflow),
1502+
"usize::MAX should trigger an overflow!"
1503+
);
15041504
} else {
15051505
// Check isize::MAX + 1 is an OOM
1506-
if let Err(AllocError { .. }) =
1507-
empty_bytes.try_reserve(MAX_CAP + 1).map_err(|e| e.kind())
1508-
{
1509-
} else {
1510-
panic!("isize::MAX + 1 should trigger an OOM!")
1511-
}
1506+
assert_matches!(
1507+
empty_bytes.try_reserve(MAX_CAP + 1).map_err(|e| e.kind()),
1508+
Err(AllocError { .. }),
1509+
"isize::MAX + 1 should trigger an OOM!"
1510+
);
15121511

15131512
// Check usize::MAX is an OOM
1514-
if let Err(AllocError { .. }) = empty_bytes.try_reserve(MAX_USIZE).map_err(|e| e.kind())
1515-
{
1516-
} else {
1517-
panic!("usize::MAX should trigger an OOM!")
1518-
}
1513+
assert_matches!(
1514+
empty_bytes.try_reserve(MAX_USIZE).map_err(|e| e.kind()),
1515+
Err(AllocError { .. }),
1516+
"usize::MAX should trigger an OOM!"
1517+
);
15191518
}
15201519
}
15211520

@@ -1530,23 +1529,24 @@ fn test_try_reserve() {
15301529
panic!("isize::MAX shouldn't trigger an overflow!");
15311530
}
15321531
if guards_against_isize {
1533-
if let Err(CapacityOverflow) = ten_bytes.try_reserve(MAX_CAP - 9).map_err(|e| e.kind())
1534-
{
1535-
} else {
1536-
panic!("isize::MAX + 1 should trigger an overflow!");
1537-
}
1532+
assert_matches!(
1533+
ten_bytes.try_reserve(MAX_CAP - 9).map_err(|e| e.kind()),
1534+
Err(CapacityOverflow),
1535+
"isize::MAX + 1 should trigger an overflow!"
1536+
);
15381537
} else {
1539-
if let Err(AllocError { .. }) = ten_bytes.try_reserve(MAX_CAP - 9).map_err(|e| e.kind())
1540-
{
1541-
} else {
1542-
panic!("isize::MAX + 1 should trigger an OOM!")
1543-
}
1538+
assert_matches!(
1539+
ten_bytes.try_reserve(MAX_CAP - 9).map_err(|e| e.kind()),
1540+
Err(AllocError { .. }),
1541+
"isize::MAX + 1 should trigger an OOM!"
1542+
);
15441543
}
15451544
// Should always overflow in the add-to-len
1546-
if let Err(CapacityOverflow) = ten_bytes.try_reserve(MAX_USIZE).map_err(|e| e.kind()) {
1547-
} else {
1548-
panic!("usize::MAX should trigger an overflow!")
1549-
}
1545+
assert_matches!(
1546+
ten_bytes.try_reserve(MAX_USIZE).map_err(|e| e.kind()),
1547+
Err(CapacityOverflow),
1548+
"usize::MAX should trigger an overflow!"
1549+
);
15501550
}
15511551

15521552
{
@@ -1562,25 +1562,24 @@ fn test_try_reserve() {
15621562
panic!("isize::MAX shouldn't trigger an overflow!");
15631563
}
15641564
if guards_against_isize {
1565-
if let Err(CapacityOverflow) =
1566-
ten_u32s.try_reserve(MAX_CAP / 4 - 9).map_err(|e| e.kind())
1567-
{
1568-
} else {
1569-
panic!("isize::MAX + 1 should trigger an overflow!");
1570-
}
1565+
assert_matches!(
1566+
ten_u32s.try_reserve(MAX_CAP / 4 - 9).map_err(|e| e.kind()),
1567+
Err(CapacityOverflow),
1568+
"isize::MAX + 1 should trigger an overflow!"
1569+
);
15711570
} else {
1572-
if let Err(AllocError { .. }) =
1573-
ten_u32s.try_reserve(MAX_CAP / 4 - 9).map_err(|e| e.kind())
1574-
{
1575-
} else {
1576-
panic!("isize::MAX + 1 should trigger an OOM!")
1577-
}
1571+
assert_matches!(
1572+
ten_u32s.try_reserve(MAX_CAP / 4 - 9).map_err(|e| e.kind()),
1573+
Err(AllocError { .. }),
1574+
"isize::MAX + 1 should trigger an OOM!"
1575+
);
15781576
}
15791577
// Should fail in the mul-by-size
1580-
if let Err(CapacityOverflow) = ten_u32s.try_reserve(MAX_USIZE - 20).map_err(|e| e.kind()) {
1581-
} else {
1582-
panic!("usize::MAX should trigger an overflow!");
1583-
}
1578+
assert_matches!(
1579+
ten_u32s.try_reserve(MAX_USIZE - 20).map_err(|e| e.kind()),
1580+
Err(CapacityOverflow),
1581+
"usize::MAX should trigger an overflow!"
1582+
);
15841583
}
15851584
}
15861585

@@ -1609,33 +1608,29 @@ fn test_try_reserve_exact() {
16091608
}
16101609

16111610
if guards_against_isize {
1612-
if let Err(CapacityOverflow) =
1613-
empty_bytes.try_reserve_exact(MAX_CAP + 1).map_err(|e| e.kind())
1614-
{
1615-
} else {
1616-
panic!("isize::MAX + 1 should trigger an overflow!")
1617-
}
1618-
1619-
if let Err(CapacityOverflow) =
1620-
empty_bytes.try_reserve_exact(MAX_USIZE).map_err(|e| e.kind())
1621-
{
1622-
} else {
1623-
panic!("usize::MAX should trigger an overflow!")
1624-
}
1611+
assert_matches!(
1612+
empty_bytes.try_reserve_exact(MAX_CAP + 1).map_err(|e| e.kind()),
1613+
Err(CapacityOverflow),
1614+
"isize::MAX + 1 should trigger an overflow!"
1615+
);
1616+
1617+
assert_matches!(
1618+
empty_bytes.try_reserve_exact(MAX_USIZE).map_err(|e| e.kind()),
1619+
Err(CapacityOverflow),
1620+
"usize::MAX should trigger an overflow!"
1621+
);
16251622
} else {
1626-
if let Err(AllocError { .. }) =
1627-
empty_bytes.try_reserve_exact(MAX_CAP + 1).map_err(|e| e.kind())
1628-
{
1629-
} else {
1630-
panic!("isize::MAX + 1 should trigger an OOM!")
1631-
}
1623+
assert_matches!(
1624+
empty_bytes.try_reserve_exact(MAX_CAP + 1).map_err(|e| e.kind()),
1625+
Err(AllocError { .. }),
1626+
"isize::MAX + 1 should trigger an OOM!"
1627+
);
16321628

1633-
if let Err(AllocError { .. }) =
1634-
empty_bytes.try_reserve_exact(MAX_USIZE).map_err(|e| e.kind())
1635-
{
1636-
} else {
1637-
panic!("usize::MAX should trigger an OOM!")
1638-
}
1629+
assert_matches!(
1630+
empty_bytes.try_reserve_exact(MAX_USIZE).map_err(|e| e.kind()),
1631+
Err(AllocError { .. }),
1632+
"usize::MAX should trigger an OOM!"
1633+
);
16391634
}
16401635
}
16411636

@@ -1653,25 +1648,23 @@ fn test_try_reserve_exact() {
16531648
panic!("isize::MAX shouldn't trigger an overflow!");
16541649
}
16551650
if guards_against_isize {
1656-
if let Err(CapacityOverflow) =
1657-
ten_bytes.try_reserve_exact(MAX_CAP - 9).map_err(|e| e.kind())
1658-
{
1659-
} else {
1660-
panic!("isize::MAX + 1 should trigger an overflow!");
1661-
}
1662-
} else {
1663-
if let Err(AllocError { .. }) =
1664-
ten_bytes.try_reserve_exact(MAX_CAP - 9).map_err(|e| e.kind())
1665-
{
1666-
} else {
1667-
panic!("isize::MAX + 1 should trigger an OOM!")
1668-
}
1669-
}
1670-
if let Err(CapacityOverflow) = ten_bytes.try_reserve_exact(MAX_USIZE).map_err(|e| e.kind())
1671-
{
1651+
assert_matches!(
1652+
ten_bytes.try_reserve_exact(MAX_CAP - 9).map_err(|e| e.kind()),
1653+
Err(CapacityOverflow),
1654+
"isize::MAX + 1 should trigger an overflow!"
1655+
);
16721656
} else {
1673-
panic!("usize::MAX should trigger an overflow!")
1657+
assert_matches!(
1658+
ten_bytes.try_reserve_exact(MAX_CAP - 9).map_err(|e| e.kind()),
1659+
Err(AllocError { .. }),
1660+
"isize::MAX + 1 should trigger an OOM!"
1661+
);
16741662
}
1663+
assert_matches!(
1664+
ten_bytes.try_reserve_exact(MAX_USIZE).map_err(|e| e.kind()),
1665+
Err(CapacityOverflow),
1666+
"usize::MAX should trigger an overflow!"
1667+
);
16751668
}
16761669

16771670
{
@@ -1688,26 +1681,23 @@ fn test_try_reserve_exact() {
16881681
panic!("isize::MAX shouldn't trigger an overflow!");
16891682
}
16901683
if guards_against_isize {
1691-
if let Err(CapacityOverflow) =
1692-
ten_u32s.try_reserve_exact(MAX_CAP / 4 - 9).map_err(|e| e.kind())
1693-
{
1694-
} else {
1695-
panic!("isize::MAX + 1 should trigger an overflow!");
1696-
}
1684+
assert_matches!(
1685+
ten_u32s.try_reserve_exact(MAX_CAP / 4 - 9).map_err(|e| e.kind()),
1686+
Err(CapacityOverflow),
1687+
"isize::MAX + 1 should trigger an overflow!"
1688+
);
16971689
} else {
1698-
if let Err(AllocError { .. }) =
1699-
ten_u32s.try_reserve_exact(MAX_CAP / 4 - 9).map_err(|e| e.kind())
1700-
{
1701-
} else {
1702-
panic!("isize::MAX + 1 should trigger an OOM!")
1703-
}
1704-
}
1705-
if let Err(CapacityOverflow) =
1706-
ten_u32s.try_reserve_exact(MAX_USIZE - 20).map_err(|e| e.kind())
1707-
{
1708-
} else {
1709-
panic!("usize::MAX should trigger an overflow!")
1710-
}
1690+
assert_matches!(
1691+
ten_u32s.try_reserve_exact(MAX_CAP / 4 - 9).map_err(|e| e.kind()),
1692+
Err(AllocError { .. }),
1693+
"isize::MAX + 1 should trigger an OOM!"
1694+
);
1695+
}
1696+
assert_matches!(
1697+
ten_u32s.try_reserve_exact(MAX_USIZE - 20).map_err(|e| e.kind()),
1698+
Err(CapacityOverflow),
1699+
"usize::MAX should trigger an overflow!"
1700+
);
17111701
}
17121702
}
17131703

‎library/alloc/tests/vec_deque.rs

Lines changed: 92 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::assert_matches::assert_matches;
12
use std::collections::TryReserveErrorKind::*;
23
use std::collections::{vec_deque::Drain, VecDeque};
34
use std::fmt::Debug;
@@ -1181,28 +1182,28 @@ fn test_try_reserve() {
11811182

11821183
if guards_against_isize {
11831184
// Check isize::MAX + 1 does count as overflow
1184-
if let Err(CapacityOverflow) =
1185-
empty_bytes.try_reserve(MAX_CAP + 1).map_err(|e| e.kind())
1186-
{
1187-
} else {
1188-
panic!("isize::MAX + 1 should trigger an overflow!")
1189-
}
1185+
assert_matches!(
1186+
empty_bytes.try_reserve(MAX_CAP + 1).map_err(|e| e.kind()),
1187+
Err(CapacityOverflow),
1188+
"isize::MAX + 1 should trigger an overflow!"
1189+
);
11901190

11911191
// Check usize::MAX does count as overflow
1192-
if let Err(CapacityOverflow) = empty_bytes.try_reserve(MAX_USIZE).map_err(|e| e.kind())
1193-
{
1194-
} else {
1195-
panic!("usize::MAX should trigger an overflow!")
1196-
}
1192+
assert_matches!(
1193+
empty_bytes.try_reserve(MAX_USIZE).map_err(|e| e.kind()),
1194+
Err(CapacityOverflow),
1195+
"usize::MAX should trigger an overflow!"
1196+
);
11971197
} else {
11981198
// Check isize::MAX is an OOM
11991199
// VecDeque starts with capacity 7, always adds 1 to the capacity
12001200
// and also rounds the number to next power of 2 so this is the
12011201
// furthest we can go without triggering CapacityOverflow
1202-
if let Err(AllocError { .. }) = empty_bytes.try_reserve(MAX_CAP).map_err(|e| e.kind()) {
1203-
} else {
1204-
panic!("isize::MAX + 1 should trigger an OOM!")
1205-
}
1202+
assert_matches!(
1203+
empty_bytes.try_reserve(MAX_CAP).map_err(|e| e.kind()),
1204+
Err(AllocError { .. }),
1205+
"isize::MAX + 1 should trigger an OOM!"
1206+
);
12061207
}
12071208
}
12081209

@@ -1217,23 +1218,24 @@ fn test_try_reserve() {
12171218
panic!("isize::MAX shouldn't trigger an overflow!");
12181219
}
12191220
if guards_against_isize {
1220-
if let Err(CapacityOverflow) = ten_bytes.try_reserve(MAX_CAP - 9).map_err(|e| e.kind())
1221-
{
1222-
} else {
1223-
panic!("isize::MAX + 1 should trigger an overflow!");
1224-
}
1221+
assert_matches!(
1222+
ten_bytes.try_reserve(MAX_CAP - 9).map_err(|e| e.kind()),
1223+
Err(CapacityOverflow),
1224+
"isize::MAX + 1 should trigger an overflow!"
1225+
);
12251226
} else {
1226-
if let Err(AllocError { .. }) = ten_bytes.try_reserve(MAX_CAP - 9).map_err(|e| e.kind())
1227-
{
1228-
} else {
1229-
panic!("isize::MAX + 1 should trigger an OOM!")
1230-
}
1227+
assert_matches!(
1228+
ten_bytes.try_reserve(MAX_CAP - 9).map_err(|e| e.kind()),
1229+
Err(AllocError { .. }),
1230+
"isize::MAX + 1 should trigger an OOM!"
1231+
);
12311232
}
12321233
// Should always overflow in the add-to-len
1233-
if let Err(CapacityOverflow) = ten_bytes.try_reserve(MAX_USIZE).map_err(|e| e.kind()) {
1234-
} else {
1235-
panic!("usize::MAX should trigger an overflow!")
1236-
}
1234+
assert_matches!(
1235+
ten_bytes.try_reserve(MAX_USIZE).map_err(|e| e.kind()),
1236+
Err(CapacityOverflow),
1237+
"usize::MAX should trigger an overflow!"
1238+
);
12371239
}
12381240

12391241
{
@@ -1249,25 +1251,24 @@ fn test_try_reserve() {
12491251
panic!("isize::MAX shouldn't trigger an overflow!");
12501252
}
12511253
if guards_against_isize {
1252-
if let Err(CapacityOverflow) =
1253-
ten_u32s.try_reserve(MAX_CAP / 4 - 9).map_err(|e| e.kind())
1254-
{
1255-
} else {
1256-
panic!("isize::MAX + 1 should trigger an overflow!");
1257-
}
1254+
assert_matches!(
1255+
ten_u32s.try_reserve(MAX_CAP / 4 - 9).map_err(|e| e.kind()),
1256+
Err(CapacityOverflow),
1257+
"isize::MAX + 1 should trigger an overflow!"
1258+
);
12581259
} else {
1259-
if let Err(AllocError { .. }) =
1260-
ten_u32s.try_reserve(MAX_CAP / 4 - 9).map_err(|e| e.kind())
1261-
{
1262-
} else {
1263-
panic!("isize::MAX + 1 should trigger an OOM!")
1264-
}
1260+
assert_matches!(
1261+
ten_u32s.try_reserve(MAX_CAP / 4 - 9).map_err(|e| e.kind()),
1262+
Err(AllocError { .. }),
1263+
"isize::MAX + 1 should trigger an OOM!"
1264+
);
12651265
}
12661266
// Should fail in the mul-by-size
1267-
if let Err(CapacityOverflow) = ten_u32s.try_reserve(MAX_USIZE - 20).map_err(|e| e.kind()) {
1268-
} else {
1269-
panic!("usize::MAX should trigger an overflow!");
1270-
}
1267+
assert_matches!(
1268+
ten_u32s.try_reserve(MAX_USIZE - 20).map_err(|e| e.kind()),
1269+
Err(CapacityOverflow),
1270+
"usize::MAX should trigger an overflow!"
1271+
);
12711272
}
12721273
}
12731274

@@ -1296,30 +1297,27 @@ fn test_try_reserve_exact() {
12961297
}
12971298

12981299
if guards_against_isize {
1299-
if let Err(CapacityOverflow) =
1300-
empty_bytes.try_reserve_exact(MAX_CAP + 1).map_err(|e| e.kind())
1301-
{
1302-
} else {
1303-
panic!("isize::MAX + 1 should trigger an overflow!")
1304-
}
1305-
1306-
if let Err(CapacityOverflow) =
1307-
empty_bytes.try_reserve_exact(MAX_USIZE).map_err(|e| e.kind())
1308-
{
1309-
} else {
1310-
panic!("usize::MAX should trigger an overflow!")
1311-
}
1300+
assert_matches!(
1301+
empty_bytes.try_reserve_exact(MAX_CAP + 1).map_err(|e| e.kind()),
1302+
Err(CapacityOverflow),
1303+
"isize::MAX + 1 should trigger an overflow!"
1304+
);
1305+
1306+
assert_matches!(
1307+
empty_bytes.try_reserve_exact(MAX_USIZE).map_err(|e| e.kind()),
1308+
Err(CapacityOverflow),
1309+
"usize::MAX should trigger an overflow!"
1310+
);
13121311
} else {
13131312
// Check isize::MAX is an OOM
13141313
// VecDeque starts with capacity 7, always adds 1 to the capacity
13151314
// and also rounds the number to next power of 2 so this is the
13161315
// furthest we can go without triggering CapacityOverflow
1317-
if let Err(AllocError { .. }) =
1318-
empty_bytes.try_reserve_exact(MAX_CAP).map_err(|e| e.kind())
1319-
{
1320-
} else {
1321-
panic!("isize::MAX + 1 should trigger an OOM!")
1322-
}
1316+
assert_matches!(
1317+
empty_bytes.try_reserve_exact(MAX_CAP).map_err(|e| e.kind()),
1318+
Err(AllocError { .. }),
1319+
"isize::MAX + 1 should trigger an OOM!"
1320+
);
13231321
}
13241322
}
13251323

@@ -1337,25 +1335,23 @@ fn test_try_reserve_exact() {
13371335
panic!("isize::MAX shouldn't trigger an overflow!");
13381336
}
13391337
if guards_against_isize {
1340-
if let Err(CapacityOverflow) =
1341-
ten_bytes.try_reserve_exact(MAX_CAP - 9).map_err(|e| e.kind())
1342-
{
1343-
} else {
1344-
panic!("isize::MAX + 1 should trigger an overflow!");
1345-
}
1346-
} else {
1347-
if let Err(AllocError { .. }) =
1348-
ten_bytes.try_reserve_exact(MAX_CAP - 9).map_err(|e| e.kind())
1349-
{
1350-
} else {
1351-
panic!("isize::MAX + 1 should trigger an OOM!")
1352-
}
1353-
}
1354-
if let Err(CapacityOverflow) = ten_bytes.try_reserve_exact(MAX_USIZE).map_err(|e| e.kind())
1355-
{
1338+
assert_matches!(
1339+
ten_bytes.try_reserve_exact(MAX_CAP - 9).map_err(|e| e.kind()),
1340+
Err(CapacityOverflow),
1341+
"isize::MAX + 1 should trigger an overflow!"
1342+
);
13561343
} else {
1357-
panic!("usize::MAX should trigger an overflow!")
1344+
assert_matches!(
1345+
ten_bytes.try_reserve_exact(MAX_CAP - 9).map_err(|e| e.kind()),
1346+
Err(AllocError { .. }),
1347+
"isize::MAX + 1 should trigger an OOM!"
1348+
);
13581349
}
1350+
assert_matches!(
1351+
ten_bytes.try_reserve_exact(MAX_USIZE).map_err(|e| e.kind()),
1352+
Err(CapacityOverflow),
1353+
"usize::MAX should trigger an overflow!"
1354+
);
13591355
}
13601356

13611357
{
@@ -1372,26 +1368,23 @@ fn test_try_reserve_exact() {
13721368
panic!("isize::MAX shouldn't trigger an overflow!");
13731369
}
13741370
if guards_against_isize {
1375-
if let Err(CapacityOverflow) =
1376-
ten_u32s.try_reserve_exact(MAX_CAP / 4 - 9).map_err(|e| e.kind())
1377-
{
1378-
} else {
1379-
panic!("isize::MAX + 1 should trigger an overflow!");
1380-
}
1381-
} else {
1382-
if let Err(AllocError { .. }) =
1383-
ten_u32s.try_reserve_exact(MAX_CAP / 4 - 9).map_err(|e| e.kind())
1384-
{
1385-
} else {
1386-
panic!("isize::MAX + 1 should trigger an OOM!")
1387-
}
1388-
}
1389-
if let Err(CapacityOverflow) =
1390-
ten_u32s.try_reserve_exact(MAX_USIZE - 20).map_err(|e| e.kind())
1391-
{
1371+
assert_matches!(
1372+
ten_u32s.try_reserve_exact(MAX_CAP / 4 - 9).map_err(|e| e.kind()),
1373+
Err(CapacityOverflow),
1374+
"isize::MAX + 1 should trigger an overflow!"
1375+
);
13921376
} else {
1393-
panic!("usize::MAX should trigger an overflow!")
1377+
assert_matches!(
1378+
ten_u32s.try_reserve_exact(MAX_CAP / 4 - 9).map_err(|e| e.kind()),
1379+
Err(AllocError { .. }),
1380+
"isize::MAX + 1 should trigger an OOM!"
1381+
);
13941382
}
1383+
assert_matches!(
1384+
ten_u32s.try_reserve_exact(MAX_USIZE - 20).map_err(|e| e.kind()),
1385+
Err(CapacityOverflow),
1386+
"usize::MAX should trigger an overflow!"
1387+
);
13951388
}
13961389
}
13971390

‎library/std/src/collections/hash/map/tests.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use super::Entry::{Occupied, Vacant};
22
use super::HashMap;
33
use super::RandomState;
4+
use crate::assert_matches::assert_matches;
45
use crate::cell::RefCell;
56
use rand::{thread_rng, Rng};
67
use realstd::collections::TryReserveErrorKind::*;
@@ -821,15 +822,17 @@ fn test_try_reserve() {
821822

822823
const MAX_USIZE: usize = usize::MAX;
823824

824-
if let Err(CapacityOverflow) = empty_bytes.try_reserve(MAX_USIZE).map_err(|e| e.kind()) {
825-
} else {
826-
panic!("usize::MAX should trigger an overflow!");
827-
}
828-
829-
if let Err(AllocError { .. }) = empty_bytes.try_reserve(MAX_USIZE / 8).map_err(|e| e.kind()) {
830-
} else {
831-
panic!("usize::MAX / 8 should trigger an OOM!")
832-
}
825+
assert_matches!(
826+
empty_bytes.try_reserve(MAX_USIZE).map_err(|e| e.kind()),
827+
Err(CapacityOverflow),
828+
"usize::MAX should trigger an overflow!"
829+
);
830+
831+
assert_matches!(
832+
empty_bytes.try_reserve(MAX_USIZE / 8).map_err(|e| e.kind()),
833+
Err(AllocError { .. }),
834+
"usize::MAX / 8 should trigger an OOM!"
835+
);
833836
}
834837

835838
#[test]

0 commit comments

Comments
 (0)
Please sign in to comment.