3 #![deny(clippy::useless_conversion)]
4 #![allow(clippy::unnecessary_wraps)]
6 fn test_generic<T: Copy>(val: T) -> T {
11 fn test_generic2<T: Copy + Into<i32> + Into<U>, U: From<T>>(val: T) {
13 let _: i32 = val.into();
14 let _: U = val.into();
18 fn test_questionmark() -> Result<(), ()> {
20 let _: i32 = 0i32.into();
26 fn test_issue_3913() -> Result<(), std::io::Error> {
30 let path = Path::new(".");
31 for _ in fs::read_dir(path)? {}
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() {}
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.into_iter().next() {}
48 fn lint_into_iter_on_expr_implementing_iterator() {
49 let text = "foo\r\nbar\n\nbaz\n";
50 let mut lines = text.lines().into_iter();
51 if Some("ok") == lines.next() {}
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().into_iter().next() {}
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.into_iter().next();
65 fn lint_into_iter_on_const_implementing_iterator_2() {
66 const NUMBERS: std::ops::Range<i32> = 0..10;
67 let mut n = NUMBERS.into_iter();
71 #[derive(Clone, Copy)]
72 struct CopiableCounter {
76 impl Iterator for CopiableCounter {
79 fn next(&mut self) -> Option<Self::Item> {
80 self.counter = self.counter.wrapping_add(1);
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));
93 fn dont_lint_into_iter_on_static_copy_iter() {
94 static mut C: CopiableCounter = CopiableCounter { counter: 0 };
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));
105 test_generic2::<i32, i32>(10i32);
106 test_questionmark().unwrap();
107 test_issue_3913().unwrap();
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();
118 let _: String = "foo".into();
119 let _: String = From::from("foo");
120 let _ = String::from("foo");
121 #[allow(clippy::useless_conversion)]
123 let _: String = "foo".into();
124 let _ = String::from("foo");
125 let _ = "".lines().into_iter();
128 let _: String = "foo".to_string().into();
129 let _: String = From::from("foo".to_string());
130 let _ = String::from("foo".to_string());
131 let _ = String::from(format!("A: {:04}", 123));
132 let _ = "".lines().into_iter();
133 let _ = vec![1, 2, 3].into_iter().into_iter();
134 let _: String = format!("Hello {}", "world").into();
136 // keep parentheses around `a + b` for suggestion (see #4750)
139 let _ = i32::from(a + b) * 3;
142 let s: Foo<'a'> = Foo;
143 let _: Foo<'b'> = s.into();
144 let s2: Foo<'a'> = Foo;
145 let _: Foo<'a'> = s2.into();
146 let s3: Foo<'a'> = Foo;
147 let _ = Foo::<'a'>::from(s3);
148 let s4: Foo<'a'> = Foo;
149 let _ = vec![s4, s4, s4].into_iter().into_iter();
152 #[derive(Copy, Clone)]
153 struct Foo<const C: char>;
155 impl From<Foo<'a'>> for Foo<'b'> {
156 fn from(_s: Foo<'a'>) -> Self {