rust/tests/ui/uninit_vec.rs

33 lines
734 B
Rust
Raw Normal View History

2021-09-17 01:55:26 -05:00
#![warn(clippy::uninit_vec)]
use std::mem::MaybeUninit;
fn main() {
// with_capacity() -> set_len() should be detected
let mut vec: Vec<u8> = Vec::with_capacity(1000);
unsafe {
vec.set_len(200);
}
// reserve() -> set_len() should be detected
vec.reserve(1000);
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);
vec.set_len(200);
vec.reserve(1000);
vec.set_len(200);
}
// MaybeUninit-wrapped types should not be detected
let mut vec: Vec<MaybeUninit<u8>> = Vec::with_capacity(1000);
unsafe {
vec.set_len(200);
}
}