Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit a42be85

Browse files
committed
Fix vec_init_then_push FP
1 parent c44eafd commit a42be85

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

clippy_lints/src/vec_init_then_push.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,11 @@ impl VecPushSearcher {
8383
}
8484

8585
impl LateLintPass<'_> for VecInitThenPush {
86-
fn check_local(&mut self, cx: &LateContext<'tcx>, local: &'tcx Local<'tcx>) {
86+
fn check_block(&mut self, _: &LateContext<'tcx>, _: &'tcx Block<'tcx>) {
8787
self.searcher = None;
88+
}
89+
90+
fn check_local(&mut self, cx: &LateContext<'tcx>, local: &'tcx Local<'tcx>) {
8891
if_chain! {
8992
if !in_external_macro(cx.sess(), local.span);
9093
if let Some(init) = local.init;

tests/ui/vec_init_then_push.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,35 @@ fn main() {
1212
cap_err.push(0);
1313
cap_err.push(1);
1414
cap_err.push(2);
15+
if true {
16+
// don't include this one
17+
cap_err.push(3);
18+
}
1519

1620
let mut cap_ok = Vec::with_capacity(10);
1721
cap_ok.push(0);
1822

1923
new_err = Vec::new();
2024
new_err.push(0);
25+
26+
let mut vec = Vec::new();
27+
// control flow at block final expression
28+
if true {
29+
// no lint
30+
vec.push(1);
31+
}
32+
}
33+
34+
pub fn no_lint() -> Vec<i32> {
35+
let mut p = Some(1);
36+
let mut vec = Vec::new();
37+
loop {
38+
match p {
39+
None => return vec,
40+
Some(i) => {
41+
vec.push(i);
42+
p = None;
43+
},
44+
}
45+
}
2146
}

tests/ui/vec_init_then_push.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ LL | | cap_err.push(2);
2424
| |____________________^ help: consider using the `vec![]` macro: `let mut cap_err = vec![..];`
2525

2626
error: calls to `push` immediately after creation
27-
--> $DIR/vec_init_then_push.rs:19:5
27+
--> $DIR/vec_init_then_push.rs:23:5
2828
|
2929
LL | / new_err = Vec::new();
3030
LL | | new_err.push(0);

0 commit comments

Comments
 (0)