Skip to content

Commit 15c10c3

Browse files
rakivocuviper
authored andcommitted
Manage .unwrap_or_else closures properly to report user's call location in case of panic
1 parent b160489 commit 15c10c3

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

src/map.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1474,14 +1474,14 @@ impl<K, V, S> Index<usize> for IndexMap<K, V, S> {
14741474
///
14751475
/// ***Panics*** if `index` is out of bounds.
14761476
fn index(&self, index: usize) -> &V {
1477-
self.get_index(index)
1478-
.unwrap_or_else(|| {
1479-
panic!(
1480-
"index out of bounds: the len is {len} but the index is {index}",
1481-
len = self.len()
1482-
);
1483-
})
1484-
.1
1477+
if let Some((_, value)) = self.get_index(index) {
1478+
value
1479+
} else {
1480+
panic!(
1481+
"index out of bounds: the len is {len} but the index is {index}",
1482+
len = self.len()
1483+
);
1484+
}
14851485
}
14861486
}
14871487

@@ -1521,11 +1521,11 @@ impl<K, V, S> IndexMut<usize> for IndexMap<K, V, S> {
15211521
fn index_mut(&mut self, index: usize) -> &mut V {
15221522
let len: usize = self.len();
15231523

1524-
self.get_index_mut(index)
1525-
.unwrap_or_else(|| {
1526-
panic!("index out of bounds: the len is {len} but the index is {index}");
1527-
})
1528-
.1
1524+
if let Some((_, value)) = self.get_index_mut(index) {
1525+
value
1526+
} else {
1527+
panic!("index out of bounds: the len is {len} but the index is {index}");
1528+
}
15291529
}
15301530
}
15311531

src/set.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,12 +1106,14 @@ impl<T, S> Index<usize> for IndexSet<T, S> {
11061106
///
11071107
/// ***Panics*** if `index` is out of bounds.
11081108
fn index(&self, index: usize) -> &T {
1109-
self.get_index(index).unwrap_or_else(|| {
1109+
if let Some(value) = self.get_index(index) {
1110+
value
1111+
} else {
11101112
panic!(
11111113
"index out of bounds: the len is {len} but the index is {index}",
11121114
len = self.len()
11131115
);
1114-
})
1116+
}
11151117
}
11161118
}
11171119

0 commit comments

Comments
 (0)