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