]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/manual_str_repeat.rs
Auto merge of #107843 - bjorn3:sync_cg_clif-2023-02-09, r=bjorn3
[rust.git] / src / tools / clippy / tests / ui / manual_str_repeat.rs
1 // run-rustfix
2
3 #![warn(clippy::manual_str_repeat)]
4
5 use std::borrow::Cow;
6 use std::iter::repeat;
7
8 fn main() {
9     let _: String = std::iter::repeat("test").take(10).collect();
10     let _: String = std::iter::repeat('x').take(10).collect();
11     let _: String = std::iter::repeat('\'').take(10).collect();
12     let _: String = std::iter::repeat('"').take(10).collect();
13
14     let x = "test";
15     let count = 10;
16     let _ = repeat(x).take(count + 2).collect::<String>();
17
18     macro_rules! m {
19         ($e:expr) => {{ $e }};
20     }
21     // FIXME: macro args are fine
22     let _: String = repeat(m!("test")).take(m!(count)).collect();
23
24     let x = &x;
25     let _: String = repeat(*x).take(count).collect();
26
27     macro_rules! repeat_m {
28         ($e:expr) => {{ repeat($e) }};
29     }
30     // Don't lint, repeat is from a macro.
31     let _: String = repeat_m!("test").take(count).collect();
32
33     let x: Box<str> = Box::from("test");
34     let _: String = repeat(x).take(count).collect();
35
36     #[derive(Clone)]
37     struct S;
38     impl FromIterator<Box<S>> for String {
39         fn from_iter<T: IntoIterator<Item = Box<S>>>(_: T) -> Self {
40             Self::new()
41         }
42     }
43     // Don't lint, wrong box type
44     let _: String = repeat(Box::new(S)).take(count).collect();
45
46     let _: String = repeat(Cow::Borrowed("test")).take(count).collect();
47
48     let x = "x".to_owned();
49     let _: String = repeat(x).take(count).collect();
50
51     let x = 'x';
52     // Don't lint, not char literal
53     let _: String = repeat(x).take(count).collect();
54 }
55
56 #[clippy::msrv = "1.15"]
57 fn _msrv_1_15() {
58     // `str::repeat` was stabilized in 1.16. Do not lint this
59     let _: String = std::iter::repeat("test").take(10).collect();
60 }
61
62 #[clippy::msrv = "1.16"]
63 fn _msrv_1_16() {
64     let _: String = std::iter::repeat("test").take(10).collect();
65 }