Skip to content

Commit

Permalink
Fix vec_init_then_push FP
Browse files Browse the repository at this point in the history
  • Loading branch information
camsteffen committed Feb 8, 2021
1 parent c44eafd commit a42be85
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
5 changes: 4 additions & 1 deletion clippy_lints/src/vec_init_then_push.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,11 @@ impl VecPushSearcher {
}

impl LateLintPass<'_> for VecInitThenPush {
fn check_local(&mut self, cx: &LateContext<'tcx>, local: &'tcx Local<'tcx>) {
fn check_block(&mut self, _: &LateContext<'tcx>, _: &'tcx Block<'tcx>) {
self.searcher = None;
}

fn check_local(&mut self, cx: &LateContext<'tcx>, local: &'tcx Local<'tcx>) {
if_chain! {
if !in_external_macro(cx.sess(), local.span);
if let Some(init) = local.init;
Expand Down
25 changes: 25 additions & 0 deletions tests/ui/vec_init_then_push.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,35 @@ fn main() {
cap_err.push(0);
cap_err.push(1);
cap_err.push(2);
if true {
// don't include this one
cap_err.push(3);
}

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

new_err = Vec::new();
new_err.push(0);

let mut vec = Vec::new();
// control flow at block final expression
if true {
// no lint
vec.push(1);
}
}

pub fn no_lint() -> Vec<i32> {
let mut p = Some(1);
let mut vec = Vec::new();
loop {
match p {
None => return vec,
Some(i) => {
vec.push(i);
p = None;
},
}
}
}
2 changes: 1 addition & 1 deletion tests/ui/vec_init_then_push.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ LL | | cap_err.push(2);
| |____________________^ help: consider using the `vec![]` macro: `let mut cap_err = vec![..];`

error: calls to `push` immediately after creation
--> $DIR/vec_init_then_push.rs:19:5
--> $DIR/vec_init_then_push.rs:23:5
|
LL | / new_err = Vec::new();
LL | | new_err.push(0);
Expand Down

0 comments on commit a42be85

Please sign in to comment.