rust/tests/ui/reserve_after_initialization.rs

52 lines
900 B
Rust
Raw Normal View History

2023-08-22 12:36:58 -05:00
//@aux-build:proc_macros.rs
#![warn(clippy::reserve_after_initialization)]
2023-08-22 11:46:16 -05:00
#![no_main]
2023-08-22 12:36:58 -05:00
extern crate proc_macros;
use proc_macros::{external, with_span};
2023-08-22 11:46:16 -05:00
// Should lint
fn standard() {
2023-08-21 16:36:15 -05:00
let mut v1: Vec<usize> = vec![];
v1.reserve(10);
2023-08-22 11:46:16 -05:00
}
2023-08-21 16:36:15 -05:00
2023-08-22 11:46:16 -05:00
// Should lint
fn capacity_as_expr() {
2023-08-21 16:36:15 -05:00
let capacity = 10;
let mut v2: Vec<usize> = vec![];
v2.reserve(capacity);
2023-08-22 11:46:16 -05:00
}
2023-08-21 16:36:15 -05:00
2023-08-22 11:46:16 -05:00
// Shouldn't lint
fn vec_init_with_argument() {
2023-08-21 16:36:15 -05:00
let mut v3 = vec![1];
v3.reserve(10);
2023-08-22 11:46:16 -05:00
}
// Shouldn't lint
fn called_with_capacity() {
let _v4: Vec<usize> = Vec::with_capacity(10);
}
2023-08-21 16:36:15 -05:00
2023-08-22 11:46:16 -05:00
// Should lint
fn assign_expression() {
let mut v5: Vec<usize> = Vec::new();
v5 = Vec::new();
v5.reserve(10);
}
2023-08-22 11:46:16 -05:00
2023-08-22 12:36:58 -05:00
fn in_macros() {
2023-08-22 11:46:16 -05:00
external! {
2023-08-22 12:36:58 -05:00
let mut v: Vec<usize> = vec![];
v.reserve(10);
2023-08-22 11:46:16 -05:00
}
with_span! {
span
2023-08-22 12:36:58 -05:00
let mut v: Vec<usize> = vec![];
v.reserve(10);
2023-08-22 11:46:16 -05:00
}
2023-08-22 12:36:58 -05:00
}