]> git.lizzy.rs Git - rust.git/blob - tests/ui/useless_conversion.fixed
Identify more cases of useless `into_iter()` calls
[rust.git] / tests / ui / useless_conversion.fixed
1 // run-rustfix
2
3 #![deny(clippy::useless_conversion)]
4 #![allow(clippy::unnecessary_wraps)]
5
6 fn test_generic<T: Copy>(val: T) -> T {
7     let _ = val;
8     val
9 }
10
11 fn test_generic2<T: Copy + Into<i32> + Into<U>, U: From<T>>(val: T) {
12     // ok
13     let _: i32 = val.into();
14     let _: U = val.into();
15     let _ = U::from(val);
16 }
17
18 fn test_questionmark() -> Result<(), ()> {
19     {
20         let _: i32 = 0i32;
21         Ok(Ok(()))
22     }??;
23     Ok(())
24 }
25
26 fn test_issue_3913() -> Result<(), std::io::Error> {
27     use std::fs;
28     use std::path::Path;
29
30     let path = Path::new(".");
31     for _ in fs::read_dir(path)? {}
32
33     Ok(())
34 }
35
36 fn dont_lint_into_iter_on_immutable_local_implementing_iterator_in_expr() {
37     let text = "foo\r\nbar\n\nbaz\n";
38     let lines = text.lines();
39     if Some("ok") == lines.into_iter().next() {}
40 }
41
42 fn lint_into_iter_on_mutable_local_implementing_iterator_in_expr() {
43     let text = "foo\r\nbar\n\nbaz\n";
44     let mut lines = text.lines();
45     if Some("ok") == lines.next() {}
46 }
47
48 fn lint_into_iter_on_expr_implementing_iterator() {
49     let text = "foo\r\nbar\n\nbaz\n";
50     let mut lines = text.lines();
51     if Some("ok") == lines.next() {}
52 }
53
54 fn lint_into_iter_on_expr_implementing_iterator_2() {
55     let text = "foo\r\nbar\n\nbaz\n";
56     if Some("ok") == text.lines().next() {}
57 }
58
59 #[allow(const_item_mutation)]
60 fn lint_into_iter_on_const_implementing_iterator() {
61     const NUMBERS: std::ops::Range<i32> = 0..10;
62     let _ = NUMBERS.next();
63 }
64
65 fn lint_into_iter_on_const_implementing_iterator_2() {
66     const NUMBERS: std::ops::Range<i32> = 0..10;
67     let mut n = NUMBERS;
68     n.next();
69 }
70
71 #[derive(Clone, Copy)]
72 struct CopiableCounter {
73     counter: u32,
74 }
75
76 impl Iterator for CopiableCounter {
77     type Item = u32;
78
79     fn next(&mut self) -> Option<Self::Item> {
80         self.counter = self.counter.wrapping_add(1);
81         Some(self.counter)
82     }
83 }
84
85 fn dont_lint_into_iter_on_copy_iter() {
86     let mut c = CopiableCounter { counter: 0 };
87     assert_eq!(c.into_iter().next(), Some(1));
88     assert_eq!(c.into_iter().next(), Some(1));
89     assert_eq!(c.next(), Some(1));
90     assert_eq!(c.next(), Some(2));
91 }
92
93 fn dont_lint_into_iter_on_static_copy_iter() {
94     static mut C: CopiableCounter = CopiableCounter { counter: 0 };
95     unsafe {
96         assert_eq!(C.into_iter().next(), Some(1));
97         assert_eq!(C.into_iter().next(), Some(1));
98         assert_eq!(C.next(), Some(1));
99         assert_eq!(C.next(), Some(2));
100     }
101 }
102
103 fn main() {
104     test_generic(10i32);
105     test_generic2::<i32, i32>(10i32);
106     test_questionmark().unwrap();
107     test_issue_3913().unwrap();
108
109     dont_lint_into_iter_on_immutable_local_implementing_iterator_in_expr();
110     lint_into_iter_on_mutable_local_implementing_iterator_in_expr();
111     lint_into_iter_on_expr_implementing_iterator();
112     lint_into_iter_on_expr_implementing_iterator_2();
113     lint_into_iter_on_const_implementing_iterator();
114     lint_into_iter_on_const_implementing_iterator_2();
115     dont_lint_into_iter_on_copy_iter();
116     dont_lint_into_iter_on_static_copy_iter();
117
118     let _: String = "foo".into();
119     let _: String = From::from("foo");
120     let _ = String::from("foo");
121     #[allow(clippy::useless_conversion)]
122     {
123         let _: String = "foo".into();
124         let _ = String::from("foo");
125         let _ = "".lines().into_iter();
126     }
127
128     let _: String = "foo".to_string();
129     let _: String = "foo".to_string();
130     let _ = "foo".to_string();
131     let _ = format!("A: {:04}", 123);
132     let _ = "".lines();
133     let _ = vec![1, 2, 3].into_iter();
134     let _: String = format!("Hello {}", "world");
135
136     // keep parentheses around `a + b` for suggestion (see #4750)
137     let a: i32 = 1;
138     let b: i32 = 1;
139     let _ = (a + b) * 3;
140
141     // see #7205
142     let s: Foo<'a'> = Foo;
143     let _: Foo<'b'> = s.into();
144     let s2: Foo<'a'> = Foo;
145     let _: Foo<'a'> = s2;
146     let s3: Foo<'a'> = Foo;
147     let _ = s3;
148     let s4: Foo<'a'> = Foo;
149     let _ = vec![s4, s4, s4].into_iter();
150 }
151
152 #[derive(Copy, Clone)]
153 struct Foo<const C: char>;
154
155 impl From<Foo<'a'>> for Foo<'b'> {
156     fn from(_s: Foo<'a'>) -> Self {
157         Foo
158     }
159 }