Skip to content

Commit 4e72ca3

Browse files
committed
[map_entry]: call the visitor on the local's else block
1 parent 660b058 commit 4e72ca3

File tree

4 files changed

+46
-2
lines changed

4 files changed

+46
-2
lines changed

clippy_lints/src/entry.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ struct InsertSearcher<'cx, 'tcx> {
358358
can_use_entry: bool,
359359
/// Whether this expression is the final expression in this code path. This may be a statement.
360360
in_tail_pos: bool,
361-
// Is this expression a single insert. A slightly better suggestion can be made in this case.
361+
/// Is this expression a single insert. A slightly better suggestion can be made in this case.
362362
is_single_insert: bool,
363363
/// If the visitor has seen the map being used.
364364
is_map_used: bool,
@@ -431,6 +431,9 @@ impl<'tcx> Visitor<'tcx> for InsertSearcher<'_, 'tcx> {
431431
self.is_single_insert = false;
432432
self.visit_expr(e);
433433
}
434+
if let Some(els) = &l.els {
435+
self.visit_block(els);
436+
}
434437
},
435438
StmtKind::Item(_) => {
436439
self.allow_insert_closure &= !self.in_tail_pos;

tests/ui/entry.fixed

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,4 +176,14 @@ pub fn issue_11935() {
176176
}
177177
}
178178

179+
fn issue12489(map: &mut HashMap<u64, u64>) -> Option<()> {
180+
if let std::collections::hash_map::Entry::Vacant(e) = map.entry(1) {
181+
let Some(1) = Some(2) else {
182+
return None;
183+
};
184+
e.insert(42);
185+
}
186+
Some(())
187+
}
188+
179189
fn main() {}

tests/ui/entry.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,4 +180,14 @@ pub fn issue_11935() {
180180
}
181181
}
182182

183+
fn issue12489(map: &mut HashMap<u64, u64>) -> Option<()> {
184+
if !map.contains_key(&1) {
185+
let Some(1) = Some(2) else {
186+
return None;
187+
};
188+
map.insert(1, 42);
189+
}
190+
Some(())
191+
}
192+
183193
fn main() {}

tests/ui/entry.stderr

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,5 +214,26 @@ LL + v
214214
LL + });
215215
|
216216

217-
error: aborting due to 10 previous errors
217+
error: usage of `contains_key` followed by `insert` on a `HashMap`
218+
--> tests/ui/entry.rs:184:5
219+
|
220+
LL | / if !map.contains_key(&1) {
221+
LL | | let Some(1) = Some(2) else {
222+
LL | | return None;
223+
LL | | };
224+
LL | | map.insert(1, 42);
225+
LL | | }
226+
| |_____^
227+
|
228+
help: try
229+
|
230+
LL ~ if let std::collections::hash_map::Entry::Vacant(e) = map.entry(1) {
231+
LL + let Some(1) = Some(2) else {
232+
LL + return None;
233+
LL + };
234+
LL + e.insert(42);
235+
LL + }
236+
|
237+
238+
error: aborting due to 11 previous errors
218239

0 commit comments

Comments
 (0)