mirror of
https://github.com/rust-lang/rust.git
synced 2025-01-18 18:53:04 +00:00
Add testcases
This commit is contained in:
parent
b1aa3064b6
commit
de0d2b1500
@ -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))
|
||||
|
@ -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);
|
||||
|
@ -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
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user