]> git.lizzy.rs Git - rust.git/blob - tests/ui/useless_conversion.rs
Move MSRV tests into the lint specific test files
[rust.git] / tests / ui / useless_conversion.rs
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 _ = T::from(val);
8     val.into()
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.into();
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 test_issue_5833() -> Result<(), ()> {
37     let text = "foo\r\nbar\n\nbaz\n";
38     let lines = text.lines();
39     if Some("ok") == lines.into_iter().next() {}
40
41     Ok(())
42 }
43
44 fn main() {
45     test_generic(10i32);
46     test_generic2::<i32, i32>(10i32);
47     test_questionmark().unwrap();
48     test_issue_3913().unwrap();
49     test_issue_5833().unwrap();
50
51     let _: String = "foo".into();
52     let _: String = From::from("foo");
53     let _ = String::from("foo");
54     #[allow(clippy::useless_conversion)]
55     {
56         let _: String = "foo".into();
57         let _ = String::from("foo");
58         let _ = "".lines().into_iter();
59     }
60
61     let _: String = "foo".to_string().into();
62     let _: String = From::from("foo".to_string());
63     let _ = String::from("foo".to_string());
64     let _ = String::from(format!("A: {:04}", 123));
65     let _ = "".lines().into_iter();
66     let _ = vec![1, 2, 3].into_iter().into_iter();
67     let _: String = format!("Hello {}", "world").into();
68
69     // keep parentheses around `a + b` for suggestion (see #4750)
70     let a: i32 = 1;
71     let b: i32 = 1;
72     let _ = i32::from(a + b) * 3;
73
74     // see #7205
75     let s: Foo<'a'> = Foo;
76     let _: Foo<'b'> = s.into();
77     let s2: Foo<'a'> = Foo;
78     let _: Foo<'a'> = s2.into();
79     let s3: Foo<'a'> = Foo;
80     let _ = Foo::<'a'>::from(s3);
81     let s4: Foo<'a'> = Foo;
82     let _ = vec![s4, s4, s4].into_iter().into_iter();
83 }
84
85 #[derive(Copy, Clone)]
86 struct Foo<const C: char>;
87
88 impl From<Foo<'a'>> for Foo<'b'> {
89     fn from(_s: Foo<'a'>) -> Self {
90         Foo
91     }
92 }