rust-clippy/tests/ui/reserve_after_initialization.fixed

49 lines
884 B
Rust
Raw Normal View History

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