]> git.lizzy.rs Git - rust.git/blob - tests/ui/identity_conversion.rs
Auto merge of #3635 - matthiaskrgr:revert_random_state_3603, r=xfix
[rust.git] / tests / ui / identity_conversion.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10 #![deny(clippy::identity_conversion)]
11
12 fn test_generic<T: Copy>(val: T) -> T {
13     let _ = T::from(val);
14     val.into()
15 }
16
17 fn test_generic2<T: Copy + Into<i32> + Into<U>, U: From<T>>(val: T) {
18     // ok
19     let _: i32 = val.into();
20     let _: U = val.into();
21     let _ = U::from(val);
22 }
23
24 fn test_questionmark() -> Result<(), ()> {
25     {
26         let _: i32 = 0i32.into();
27         Ok(Ok(()))
28     }??;
29     Ok(())
30 }
31
32 fn main() {
33     test_generic(10i32);
34     test_generic2::<i32, i32>(10i32);
35     test_questionmark().unwrap();
36
37     let _: String = "foo".into();
38     let _: String = From::from("foo");
39     let _ = String::from("foo");
40     #[allow(clippy::identity_conversion)]
41     {
42         let _: String = "foo".into();
43         let _ = String::from("foo");
44         let _ = "".lines().into_iter();
45     }
46
47     let _: String = "foo".to_string().into();
48     let _: String = From::from("foo".to_string());
49     let _ = String::from("foo".to_string());
50     let _ = String::from(format!("A: {:04}", 123));
51     let _ = "".lines().into_iter();
52     let _ = vec![1, 2, 3].into_iter().into_iter();
53     let _: String = format!("Hello {}", "world").into();
54 }