mirror of
https://github.com/rust-lang/rust.git
synced 2024-12-04 20:54:13 +00:00
ae0216d557
Follow up https://github.com/rust-lang/rust/pull/96861. This PR uses the traits added to the Rust 2021 Edition prelude. > The `TryInto`, `TryFrom` and `FromIterator` traits are now part of the prelude. https://doc.rust-lang.org/edition-guide/rust-2021/prelude.html
41 lines
1.3 KiB
Rust
41 lines
1.3 KiB
Rust
#![deny(clippy::useless_conversion)]
|
|
|
|
fn test_generic<T: Copy>(val: T) -> T {
|
|
let _ = T::try_from(val).unwrap();
|
|
val.try_into().unwrap()
|
|
}
|
|
|
|
fn test_generic2<T: Copy + Into<i32> + Into<U>, U: From<T>>(val: T) {
|
|
// ok
|
|
let _: i32 = val.try_into().unwrap();
|
|
let _: U = val.try_into().unwrap();
|
|
let _ = U::try_from(val).unwrap();
|
|
}
|
|
|
|
fn main() {
|
|
test_generic(10i32);
|
|
test_generic2::<i32, i32>(10i32);
|
|
|
|
let _: String = "foo".try_into().unwrap();
|
|
let _: String = TryFrom::try_from("foo").unwrap();
|
|
let _ = String::try_from("foo").unwrap();
|
|
#[allow(clippy::useless_conversion)]
|
|
{
|
|
let _ = String::try_from("foo").unwrap();
|
|
let _: String = "foo".try_into().unwrap();
|
|
}
|
|
let _: String = "foo".to_string().try_into().unwrap();
|
|
let _: String = TryFrom::try_from("foo".to_string()).unwrap();
|
|
let _ = String::try_from("foo".to_string()).unwrap();
|
|
let _ = String::try_from(format!("A: {:04}", 123)).unwrap();
|
|
let _: String = format!("Hello {}", "world").try_into().unwrap();
|
|
let _: String = "".to_owned().try_into().unwrap();
|
|
let _: String = match String::from("_").try_into() {
|
|
Ok(a) => a,
|
|
Err(_) => "".into(),
|
|
};
|
|
// FIXME this is a false negative
|
|
#[allow(clippy::cmp_owned)]
|
|
if String::from("a") == TryInto::<String>::try_into(String::from("a")).unwrap() {}
|
|
}
|