]> git.proxmox.com Git - rustc.git/blame - src/tools/clippy/tests/ui/useless_conversion.rs
Update upstream source from tag 'upstream/1.52.1+dfsg1'
[rustc.git] / src / tools / clippy / tests / ui / useless_conversion.rs
CommitLineData
f20569fa
XL
1// run-rustfix
2
3#![deny(clippy::useless_conversion)]
4#![allow(clippy::unnecessary_wraps)]
5
6fn test_generic<T: Copy>(val: T) -> T {
7 let _ = T::from(val);
8 val.into()
9}
10
11fn 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
18fn test_questionmark() -> Result<(), ()> {
19 {
20 let _: i32 = 0i32.into();
21 Ok(Ok(()))
22 }??;
23 Ok(())
24}
25
26fn 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
36fn 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
44fn 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 parenthesis 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}