diff options
| -rw-r--r-- | clippy_utils/src/higher.rs | 20 | ||||
| -rw-r--r-- | tests/ui/uninit_vec.rs | 17 | ||||
| -rw-r--r-- | tests/ui/uninit_vec.stderr | 45 |
3 files changed, 66 insertions, 16 deletions
diff --git a/clippy_utils/src/higher.rs b/clippy_utils/src/higher.rs index d60ddbd3c56..30f6831dcf4 100644 --- a/clippy_utils/src/higher.rs +++ b/clippy_utils/src/higher.rs @@ -658,18 +658,18 @@ pub fn get_vec_init_kind<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) - { if name.ident.name == sym::new { return Some(VecInitKind::New); + } else if name.ident.name.as_str() == "default" { + return Some(VecInitKind::Default); } else if name.ident.name.as_str() == "with_capacity" { - return args.get(0).and_then(|arg| { - if_chain! { - if let ExprKind::Lit(lit) = &arg.kind; - if let LitKind::Int(num, _) = lit.node; - then { - Some(VecInitKind::WithLiteralCapacity(num.try_into().ok()?)) - } else { - Some(VecInitKind::WithExprCapacity(arg.hir_id)) - } + let arg = args.get(0)?; + if_chain! { + if let ExprKind::Lit(lit) = &arg.kind; + if let LitKind::Int(num, _) = lit.node; + then { + return Some(VecInitKind::WithLiteralCapacity(num.try_into().ok()?)) } - }); + } + return Some(VecInitKind::WithExprCapacity(arg.hir_id)); } } ExprKind::Path(QPath::Resolved(_, path)) diff --git a/tests/ui/uninit_vec.rs b/tests/ui/uninit_vec.rs index c8f9067e549..dc150cf28f2 100644 --- a/tests/ui/uninit_vec.rs +++ b/tests/ui/uninit_vec.rs @@ -20,6 +20,23 @@ fn main() { vec.set_len(200); } + // new() -> set_len() should be detected + let mut vec: Vec<u8> = Vec::new(); + unsafe { + vec.set_len(200); + } + + // default() -> set_len() should be detected + let mut vec: Vec<u8> = Default::default(); + unsafe { + vec.set_len(200); + } + + let mut vec: Vec<u8> = Vec::default(); + unsafe { + vec.set_len(200); + } + // test when both calls are enclosed in the same unsafe block unsafe { let mut vec: Vec<u8> = Vec::with_capacity(1000); diff --git a/tests/ui/uninit_vec.stderr b/tests/ui/uninit_vec.stderr index 31f8ae40350..81b91cef92a 100644 --- a/tests/ui/uninit_vec.stderr +++ b/tests/ui/uninit_vec.stderr @@ -22,7 +22,40 @@ LL | vec.set_len(200); = help: initialize the buffer or wrap the content in `MaybeUninit` error: calling `set_len()` immediately after reserving a buffer creates uninitialized values - --> $DIR/uninit_vec.rs:32:5 + --> $DIR/uninit_vec.rs:24:5 + | +LL | let mut vec: Vec<u8> = Vec::new(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +LL | unsafe { +LL | vec.set_len(200); + | ^^^^^^^^^^^^^^^^ + | + = help: initialize the buffer or wrap the content in `MaybeUninit` + +error: calling `set_len()` immediately after reserving a buffer creates uninitialized values + --> $DIR/uninit_vec.rs:30:5 + | +LL | let mut vec: Vec<u8> = Default::default(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +LL | unsafe { +LL | vec.set_len(200); + | ^^^^^^^^^^^^^^^^ + | + = help: initialize the buffer or wrap the content in `MaybeUninit` + +error: calling `set_len()` immediately after reserving a buffer creates uninitialized values + --> $DIR/uninit_vec.rs:35:5 + | +LL | let mut vec: Vec<u8> = Vec::default(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +LL | unsafe { +LL | vec.set_len(200); + | ^^^^^^^^^^^^^^^^ + | + = help: initialize the buffer or wrap the content in `MaybeUninit` + +error: calling `set_len()` immediately after reserving a buffer creates uninitialized values + --> $DIR/uninit_vec.rs:49:5 | LL | let mut vec: Vec<u8> = Vec::with_capacity(1000); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -33,7 +66,7 @@ LL | vec.set_len(200); = help: initialize the buffer or wrap the content in `MaybeUninit` error: calling `set_len()` immediately after reserving a buffer creates uninitialized values - --> $DIR/uninit_vec.rs:41:5 + --> $DIR/uninit_vec.rs:58:5 | LL | my_vec.vec.reserve(1000); | ^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -44,7 +77,7 @@ LL | my_vec.vec.set_len(200); = help: initialize the buffer or wrap the content in `MaybeUninit` error: calling `set_len()` immediately after reserving a buffer creates uninitialized values - --> $DIR/uninit_vec.rs:46:5 + --> $DIR/uninit_vec.rs:63:5 | LL | my_vec.vec = Vec::with_capacity(1000); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -55,7 +88,7 @@ LL | my_vec.vec.set_len(200); = help: initialize the buffer or wrap the content in `MaybeUninit` error: calling `set_len()` immediately after reserving a buffer creates uninitialized values - --> $DIR/uninit_vec.rs:25:9 + --> $DIR/uninit_vec.rs:42:9 | LL | let mut vec: Vec<u8> = Vec::with_capacity(1000); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -65,7 +98,7 @@ LL | vec.set_len(200); = help: initialize the buffer or wrap the content in `MaybeUninit` error: calling `set_len()` immediately after reserving a buffer creates uninitialized values - --> $DIR/uninit_vec.rs:28:9 + --> $DIR/uninit_vec.rs:45:9 | LL | vec.reserve(1000); | ^^^^^^^^^^^^^^^^^^ @@ -74,5 +107,5 @@ LL | vec.set_len(200); | = help: initialize the buffer or wrap the content in `MaybeUninit` -error: aborting due to 7 previous errors +error: aborting due to 10 previous errors |
