]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/useless_conversion_try.rs
Rollup merge of #78462 - danielframpton:fixnullisa, r=nagisa
[rust.git] / src / tools / clippy / tests / ui / useless_conversion_try.rs
1 #![deny(clippy::useless_conversion)]
2
3 use std::convert::{TryFrom, TryInto};
4
5 fn test_generic<T: Copy>(val: T) -> T {
6     let _ = T::try_from(val).unwrap();
7     val.try_into().unwrap()
8 }
9
10 fn test_generic2<T: Copy + Into<i32> + Into<U>, U: From<T>>(val: T) {
11     // ok
12     let _: i32 = val.try_into().unwrap();
13     let _: U = val.try_into().unwrap();
14     let _ = U::try_from(val).unwrap();
15 }
16
17 fn main() {
18     test_generic(10i32);
19     test_generic2::<i32, i32>(10i32);
20
21     let _: String = "foo".try_into().unwrap();
22     let _: String = TryFrom::try_from("foo").unwrap();
23     let _ = String::try_from("foo").unwrap();
24     #[allow(clippy::useless_conversion)]
25     {
26         let _ = String::try_from("foo").unwrap();
27         let _: String = "foo".try_into().unwrap();
28     }
29     let _: String = "foo".to_string().try_into().unwrap();
30     let _: String = TryFrom::try_from("foo".to_string()).unwrap();
31     let _ = String::try_from("foo".to_string()).unwrap();
32     let _ = String::try_from(format!("A: {:04}", 123)).unwrap();
33     let _: String = format!("Hello {}", "world").try_into().unwrap();
34     let _: String = "".to_owned().try_into().unwrap();
35     let _: String = match String::from("_").try_into() {
36         Ok(a) => a,
37         Err(_) => "".into(),
38     };
39     // FIXME this is a false negative
40     #[allow(clippy::cmp_owned)]
41     if String::from("a") == TryInto::<String>::try_into(String::from("a")).unwrap() {}
42 }