add manual_repeat_n lint

This commit is contained in:
lapla-cogito 2024-12-20 22:29:06 +09:00
parent 25d319d1b2
commit 544f71f48d
No known key found for this signature in database
GPG key ID: B39C71D9F127FF9F
15 changed files with 188 additions and 19 deletions

View file

@ -1,6 +1,6 @@
#![warn(clippy::from_iter_instead_of_collect)]
#![allow(unused_imports)]
#![allow(clippy::useless_vec)]
#![allow(clippy::useless_vec, clippy::manual_repeat_n)]
use std::collections::{BTreeMap, BTreeSet, HashMap, VecDeque};

View file

@ -1,6 +1,6 @@
#![warn(clippy::from_iter_instead_of_collect)]
#![allow(unused_imports)]
#![allow(clippy::useless_vec)]
#![allow(clippy::useless_vec, clippy::manual_repeat_n)]
use std::collections::{BTreeMap, BTreeSet, HashMap, VecDeque};

View file

@ -0,0 +1,30 @@
#![warn(clippy::manual_repeat_n)]
use std::iter::repeat;
fn main() {
let _ = std::iter::repeat_n(10, 3);
let _ = std::iter::repeat_n(String::from("foo"), 4);
for value in std::iter::repeat_n(5, 3) {}
let _: Vec<_> = std::iter::repeat_n(String::from("bar"), 10).collect();
let _ = std::iter::repeat_n(vec![1, 2], 2);
}
mod foo_lib {
pub fn iter() -> std::iter::Take<std::iter::Repeat<&'static [u8]>> {
todo!()
}
}
fn foo() {
let _ = match 1 {
1 => foo_lib::iter(),
// Shouldn't lint because `external_lib::iter` doesn't return `std::iter::RepeatN`.
2 => std::iter::repeat([1, 2].as_slice()).take(2),
_ => todo!(),
};
}

View file

@ -0,0 +1,30 @@
#![warn(clippy::manual_repeat_n)]
use std::iter::repeat;
fn main() {
let _ = repeat(10).take(3);
let _ = repeat(String::from("foo")).take(4);
for value in std::iter::repeat(5).take(3) {}
let _: Vec<_> = std::iter::repeat(String::from("bar")).take(10).collect();
let _ = repeat(vec![1, 2]).take(2);
}
mod foo_lib {
pub fn iter() -> std::iter::Take<std::iter::Repeat<&'static [u8]>> {
todo!()
}
}
fn foo() {
let _ = match 1 {
1 => foo_lib::iter(),
// Shouldn't lint because `external_lib::iter` doesn't return `std::iter::RepeatN`.
2 => std::iter::repeat([1, 2].as_slice()).take(2),
_ => todo!(),
};
}

View file

@ -0,0 +1,35 @@
error: this `repeat().take()` can be written more concisely
--> tests/ui/manual_repeat_n.rs:6:13
|
LL | let _ = repeat(10).take(3);
| ^^^^^^^^^^^^^^^^^^ help: consider using `repeat_n()` instead: `std::iter::repeat_n(10, 3)`
|
= note: `-D clippy::manual-repeat-n` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::manual_repeat_n)]`
error: this `repeat().take()` can be written more concisely
--> tests/ui/manual_repeat_n.rs:8:13
|
LL | let _ = repeat(String::from("foo")).take(4);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `repeat_n()` instead: `std::iter::repeat_n(String::from("foo"), 4)`
error: this `repeat().take()` can be written more concisely
--> tests/ui/manual_repeat_n.rs:10:18
|
LL | for value in std::iter::repeat(5).take(3) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `repeat_n()` instead: `std::iter::repeat_n(5, 3)`
error: this `repeat().take()` can be written more concisely
--> tests/ui/manual_repeat_n.rs:12:21
|
LL | let _: Vec<_> = std::iter::repeat(String::from("bar")).take(10).collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `repeat_n()` instead: `std::iter::repeat_n(String::from("bar"), 10)`
error: this `repeat().take()` can be written more concisely
--> tests/ui/manual_repeat_n.rs:14:13
|
LL | let _ = repeat(vec![1, 2]).take(2);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `repeat_n()` instead: `std::iter::repeat_n(vec![1, 2], 2)`
error: aborting due to 5 previous errors

View file

@ -1,4 +1,4 @@
#![allow(non_local_definitions)]
#![allow(non_local_definitions, clippy::manual_repeat_n)]
#![warn(clippy::manual_str_repeat)]
use std::borrow::Cow;

View file

@ -1,4 +1,4 @@
#![allow(non_local_definitions)]
#![allow(non_local_definitions, clippy::manual_repeat_n)]
#![warn(clippy::manual_str_repeat)]
use std::borrow::Cow;

View file

@ -1,4 +1,5 @@
#![allow(clippy::useless_vec)]
#![allow(clippy::useless_vec, clippy::manual_repeat_n)]
use std::iter::repeat;
fn main() {
resize_vector();

View file

@ -1,4 +1,5 @@
#![allow(clippy::useless_vec)]
#![allow(clippy::useless_vec, clippy::manual_repeat_n)]
use std::iter::repeat;
fn main() {
resize_vector();

View file

@ -1,5 +1,5 @@
error: slow zero-filling initialization
--> tests/ui/slow_vector_initialization.rs:13:20
--> tests/ui/slow_vector_initialization.rs:14:20
|
LL | let mut vec1 = Vec::with_capacity(len);
| ____________________^
@ -11,7 +11,7 @@ LL | | vec1.extend(repeat(0).take(len));
= help: to override `-D warnings` add `#[allow(clippy::slow_vector_initialization)]`
error: slow zero-filling initialization
--> tests/ui/slow_vector_initialization.rs:19:20
--> tests/ui/slow_vector_initialization.rs:20:20
|
LL | let mut vec2 = Vec::with_capacity(len - 10);
| ____________________^
@ -20,7 +20,7 @@ LL | | vec2.extend(repeat(0).take(len - 10));
| |_________________________________________^ help: consider replacing this with: `vec![0; len - 10]`
error: slow zero-filling initialization
--> tests/ui/slow_vector_initialization.rs:27:20
--> tests/ui/slow_vector_initialization.rs:28:20
|
LL | let mut vec4 = Vec::with_capacity(len);
| ____________________^
@ -29,7 +29,7 @@ LL | | vec4.extend(repeat(0).take(vec4.capacity()));
| |________________________________________________^ help: consider replacing this with: `vec![0; len]`
error: slow zero-filling initialization
--> tests/ui/slow_vector_initialization.rs:38:27
--> tests/ui/slow_vector_initialization.rs:39:27
|
LL | let mut resized_vec = Vec::with_capacity(30);
| ___________________________^
@ -38,7 +38,7 @@ LL | | resized_vec.resize(30, 0);
| |_____________________________^ help: consider replacing this with: `vec![0; 30]`
error: slow zero-filling initialization
--> tests/ui/slow_vector_initialization.rs:42:26
--> tests/ui/slow_vector_initialization.rs:43:26
|
LL | let mut extend_vec = Vec::with_capacity(30);
| __________________________^
@ -47,7 +47,7 @@ LL | | extend_vec.extend(repeat(0).take(30));
| |_________________________________________^ help: consider replacing this with: `vec![0; 30]`
error: slow zero-filling initialization
--> tests/ui/slow_vector_initialization.rs:50:20
--> tests/ui/slow_vector_initialization.rs:51:20
|
LL | let mut vec1 = Vec::with_capacity(len);
| ____________________^
@ -56,7 +56,7 @@ LL | | vec1.resize(len, 0);
| |_______________________^ help: consider replacing this with: `vec![0; len]`
error: slow zero-filling initialization
--> tests/ui/slow_vector_initialization.rs:59:20
--> tests/ui/slow_vector_initialization.rs:60:20
|
LL | let mut vec3 = Vec::with_capacity(len - 10);
| ____________________^
@ -65,7 +65,7 @@ LL | | vec3.resize(len - 10, 0);
| |____________________________^ help: consider replacing this with: `vec![0; len - 10]`
error: slow zero-filling initialization
--> tests/ui/slow_vector_initialization.rs:63:20
--> tests/ui/slow_vector_initialization.rs:64:20
|
LL | let mut vec4 = Vec::with_capacity(len);
| ____________________^
@ -74,7 +74,7 @@ LL | | vec4.resize(vec4.capacity(), 0);
| |___________________________________^ help: consider replacing this with: `vec![0; len]`
error: slow zero-filling initialization
--> tests/ui/slow_vector_initialization.rs:68:12
--> tests/ui/slow_vector_initialization.rs:69:12
|
LL | vec1 = Vec::with_capacity(10);
| ____________^
@ -83,7 +83,7 @@ LL | | vec1.resize(10, 0);
| |______________________^ help: consider replacing this with: `vec![0; 10]`
error: slow zero-filling initialization
--> tests/ui/slow_vector_initialization.rs:76:20
--> tests/ui/slow_vector_initialization.rs:77:20
|
LL | let mut vec1 = Vec::new();
| ____________________^
@ -92,7 +92,7 @@ LL | | vec1.resize(len, 0);
| |_______________________^ help: consider replacing this with: `vec![0; len]`
error: slow zero-filling initialization
--> tests/ui/slow_vector_initialization.rs:81:20
--> tests/ui/slow_vector_initialization.rs:82:20
|
LL | let mut vec3 = Vec::new();
| ____________________^
@ -101,7 +101,7 @@ LL | | vec3.resize(len - 10, 0);
| |____________________________^ help: consider replacing this with: `vec![0; len - 10]`
error: slow zero-filling initialization
--> tests/ui/slow_vector_initialization.rs:86:12
--> tests/ui/slow_vector_initialization.rs:87:12
|
LL | vec1 = Vec::new();
| ____________^
@ -110,7 +110,7 @@ LL | | vec1.resize(10, 0);
| |______________________^ help: consider replacing this with: `vec![0; 10]`
error: slow zero-filling initialization
--> tests/ui/slow_vector_initialization.rs:90:12
--> tests/ui/slow_vector_initialization.rs:91:12
|
LL | vec1 = vec![];
| ____________^