rust/tests/ui/same_item_push.rs

165 lines
3.5 KiB
Rust
Raw Normal View History

2020-07-18 09:28:31 -05:00
#![warn(clippy::same_item_push)]
const VALUE: u8 = 7;
2020-07-18 09:28:31 -05:00
fn mutate_increment(x: &mut u8) -> u8 {
*x += 1;
*x
}
fn increment(x: u8) -> u8 {
x + 1
}
fn fun() -> usize {
42
}
2020-07-18 09:28:31 -05:00
fn main() {
2020-09-08 09:03:15 -05:00
// ** linted cases **
let mut vec: Vec<u8> = Vec::new();
2020-07-18 09:28:31 -05:00
let item = 2;
for _ in 5..=20 {
2020-09-08 09:03:15 -05:00
vec.push(item);
//~^ ERROR: it looks like the same item is being pushed into this Vec
2020-07-18 09:28:31 -05:00
}
2020-09-08 09:03:15 -05:00
let mut vec: Vec<u8> = Vec::new();
2020-07-18 09:28:31 -05:00
for _ in 0..15 {
let item = 2;
2020-09-08 09:03:15 -05:00
vec.push(item);
//~^ ERROR: it looks like the same item is being pushed into this Vec
2020-07-18 09:28:31 -05:00
}
2020-09-08 09:03:15 -05:00
let mut vec: Vec<u8> = Vec::new();
2020-07-18 09:28:31 -05:00
for _ in 0..15 {
2020-09-08 09:03:15 -05:00
vec.push(13);
//~^ ERROR: it looks like the same item is being pushed into this Vec
2020-09-08 09:03:15 -05:00
}
let mut vec = Vec::new();
for _ in 0..20 {
vec.push(VALUE);
//~^ ERROR: it looks like the same item is being pushed into this Vec
2020-09-08 09:03:15 -05:00
}
let mut vec = Vec::new();
let item = VALUE;
for _ in 0..20 {
vec.push(item);
//~^ ERROR: it looks like the same item is being pushed into this Vec
2020-09-08 09:03:15 -05:00
}
// ** non-linted cases **
let mut spaces = Vec::with_capacity(10);
for _ in 0..10 {
spaces.push(vec![b' ']);
2020-07-18 09:28:31 -05:00
}
// Suggestion should not be given as pushed variable can mutate
2020-09-08 09:03:15 -05:00
let mut vec: Vec<u8> = Vec::new();
2020-07-18 09:28:31 -05:00
let mut item: u8 = 2;
for _ in 0..30 {
2020-09-08 09:03:15 -05:00
vec.push(mutate_increment(&mut item));
2020-07-18 09:28:31 -05:00
}
2020-09-08 09:03:15 -05:00
let mut vec: Vec<u8> = Vec::new();
2020-07-18 09:28:31 -05:00
let mut item: u8 = 2;
let mut item2 = &mut mutate_increment(&mut item);
for _ in 0..30 {
2020-09-08 09:03:15 -05:00
vec.push(mutate_increment(item2));
2020-07-18 09:28:31 -05:00
}
2020-09-08 09:03:15 -05:00
let mut vec: Vec<usize> = Vec::new();
2020-07-18 09:28:31 -05:00
for (a, b) in [0, 1, 4, 9, 16].iter().enumerate() {
2020-09-08 09:03:15 -05:00
vec.push(a);
2020-07-18 09:28:31 -05:00
}
2020-09-08 09:03:15 -05:00
let mut vec: Vec<u8> = Vec::new();
2020-07-18 09:28:31 -05:00
for i in 0..30 {
2020-09-08 09:03:15 -05:00
vec.push(increment(i));
2020-07-18 09:28:31 -05:00
}
2020-09-08 09:03:15 -05:00
let mut vec: Vec<u8> = Vec::new();
2020-07-18 09:28:31 -05:00
for i in 0..30 {
2020-09-08 09:03:15 -05:00
vec.push(i + i * i);
2020-07-18 09:28:31 -05:00
}
// Suggestion should not be given as there are multiple pushes that are not the same
2020-09-08 09:03:15 -05:00
let mut vec: Vec<u8> = Vec::new();
2020-07-18 09:28:31 -05:00
let item: u8 = 2;
for _ in 0..30 {
2020-09-08 09:03:15 -05:00
vec.push(item);
vec.push(item * 2);
2020-07-18 09:28:31 -05:00
}
// Suggestion should not be given as Vec is not involved
for _ in 0..5 {
println!("Same Item Push");
}
2020-07-19 09:43:35 -05:00
struct A {
kind: u32,
}
let mut vec_a: Vec<A> = Vec::new();
for i in 0..30 {
2020-07-20 18:25:11 -05:00
vec_a.push(A { kind: i });
2020-07-19 09:43:35 -05:00
}
2020-09-08 09:03:15 -05:00
let mut vec: Vec<u8> = Vec::new();
2020-07-19 09:43:35 -05:00
for a in vec_a {
2020-09-08 09:03:15 -05:00
vec.push(2u8.pow(a.kind));
2020-07-19 09:43:35 -05:00
}
// Fix #5902
2020-09-08 09:03:15 -05:00
let mut vec: Vec<u8> = Vec::new();
let mut item = 0;
for _ in 0..10 {
2020-09-08 09:03:15 -05:00
vec.push(item);
item += 10;
}
// Fix #5979
2020-09-08 09:03:15 -05:00
let mut vec: Vec<std::fs::File> = Vec::new();
for _ in 0..10 {
2020-09-08 09:03:15 -05:00
vec.push(std::fs::File::open("foobar").unwrap());
}
2020-09-05 10:02:35 -05:00
// Fix #5979
#[derive(Clone)]
2022-03-27 07:41:09 -05:00
struct S;
2020-09-05 10:02:35 -05:00
trait T {}
impl T for S {}
2020-09-08 09:03:15 -05:00
let mut vec: Vec<Box<dyn T>> = Vec::new();
2020-09-05 10:02:35 -05:00
for _ in 0..10 {
2020-09-08 09:03:15 -05:00
vec.push(Box::new(S {}));
}
2020-09-08 09:03:15 -05:00
// Fix #5985
let mut vec = Vec::new();
let item = 42;
let item = fun();
for _ in 0..20 {
2020-09-08 09:03:15 -05:00
vec.push(item);
}
2020-09-08 09:03:15 -05:00
// Fix #5985
let mut vec = Vec::new();
let key = 1;
for _ in 0..20 {
let item = match key {
1 => 10,
_ => 0,
};
2020-09-08 09:03:15 -05:00
vec.push(item);
}
// Fix #6987
let mut vec = Vec::new();
2022-07-08 04:30:17 -05:00
#[allow(clippy::needless_borrow)]
for _ in 0..10 {
vec.push(1);
vec.extend(&[2]);
}
2020-07-18 09:28:31 -05:00
}