Skip to content

Commit

Permalink
Auto merge of #101808 - flip1995:clippy_backport, r=Mark-Simulacrum
Browse files Browse the repository at this point in the history
[Beta] Clippy: Fix hang in `vec_init_then_push`

Small Clippy ICE/hang fix backport before beta gets branched. We'd like to get this into stable ASAP. This fix is already in `master` as 1585932.
  • Loading branch information
bors committed Sep 16, 2022
2 parents 25912c0 + 0b3a19a commit b31188e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/tools/clippy/clippy_lints/src/vec_init_then_push.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ impl VecPushSearcher {
},
ExprKind::Unary(UnOp::Deref, _) | ExprKind::Index(..) if !needs_mut => {
let mut last_place = parent;
while let Some(parent) = get_parent_expr(cx, parent) {
while let Some(parent) = get_parent_expr(cx, last_place) {
if matches!(parent.kind, ExprKind::Unary(UnOp::Deref, _) | ExprKind::Field(..))
|| matches!(parent.kind, ExprKind::Index(e, _) if e.hir_id == last_place.hir_id)
{
Expand Down
6 changes: 6 additions & 0 deletions src/tools/clippy/tests/ui/vec_init_then_push.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,9 @@ fn _cond_push_with_large_start(x: bool) -> Vec<u32> {

v2
}

fn f() {
let mut v = Vec::new();
v.push((0i32, 0i32));
let y = v[0].0.abs();
}
9 changes: 8 additions & 1 deletion src/tools/clippy/tests/ui/vec_init_then_push.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,12 @@ LL | | v2.push(1);
LL | | v2.push(0);
| |_______________^ help: consider using the `vec![]` macro: `let mut v2 = vec![..];`

error: aborting due to 7 previous errors
error: calls to `push` immediately after creation
--> $DIR/vec_init_then_push.rs:109:5
|
LL | / let mut v = Vec::new();
LL | | v.push((0i32, 0i32));
| |_________________________^ help: consider using the `vec![]` macro: `let v = vec![..];`

error: aborting due to 8 previous errors

0 comments on commit b31188e

Please sign in to comment.