2020-05-09 14:28:31 -05:00
|
|
|
#![deny(clippy::useless_conversion)]
|
2024-06-06 15:44:06 -05:00
|
|
|
#![allow(
|
|
|
|
clippy::needless_if,
|
|
|
|
clippy::unnecessary_fallible_conversions,
|
|
|
|
clippy::manual_unwrap_or_default
|
|
|
|
)]
|
2020-05-09 14:28:31 -05:00
|
|
|
|
|
|
|
fn test_generic<T: Copy>(val: T) -> T {
|
2020-05-22 04:46:17 -05:00
|
|
|
let _ = T::try_from(val).unwrap();
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: useless conversion to the same type: `T`
|
2020-05-22 04:46:17 -05:00
|
|
|
val.try_into().unwrap()
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: useless conversion to the same type: `T`
|
2020-05-09 14:28:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_generic2<T: Copy + Into<i32> + Into<U>, U: From<T>>(val: T) {
|
2020-05-22 04:46:17 -05:00
|
|
|
// ok
|
|
|
|
let _: i32 = val.try_into().unwrap();
|
|
|
|
let _: U = val.try_into().unwrap();
|
2020-05-09 14:28:31 -05:00
|
|
|
let _ = U::try_from(val).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
test_generic(10i32);
|
|
|
|
test_generic2::<i32, i32>(10i32);
|
|
|
|
|
2020-05-22 04:46:17 -05:00
|
|
|
let _: String = "foo".try_into().unwrap();
|
2020-05-09 14:28:31 -05:00
|
|
|
let _: String = TryFrom::try_from("foo").unwrap();
|
|
|
|
let _ = String::try_from("foo").unwrap();
|
|
|
|
#[allow(clippy::useless_conversion)]
|
2020-05-22 04:46:17 -05:00
|
|
|
{
|
|
|
|
let _ = String::try_from("foo").unwrap();
|
|
|
|
let _: String = "foo".try_into().unwrap();
|
|
|
|
}
|
|
|
|
let _: String = "foo".to_string().try_into().unwrap();
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: useless conversion to the same type: `std::string::String`
|
2020-05-09 14:28:31 -05:00
|
|
|
let _: String = TryFrom::try_from("foo".to_string()).unwrap();
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: useless conversion to the same type: `std::string::String`
|
2020-05-09 14:28:31 -05:00
|
|
|
let _ = String::try_from("foo".to_string()).unwrap();
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: useless conversion to the same type: `std::string::String`
|
2020-05-09 14:28:31 -05:00
|
|
|
let _ = String::try_from(format!("A: {:04}", 123)).unwrap();
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: useless conversion to the same type: `std::string::String`
|
2020-05-22 04:46:17 -05:00
|
|
|
let _: String = format!("Hello {}", "world").try_into().unwrap();
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: useless conversion to the same type: `std::string::String`
|
2022-08-05 18:59:50 -05:00
|
|
|
let _: String = String::new().try_into().unwrap();
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: useless conversion to the same type: `std::string::String`
|
2020-05-26 11:40:42 -05:00
|
|
|
let _: String = match String::from("_").try_into() {
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: useless conversion to the same type: `std::string::String`
|
2020-05-26 11:40:42 -05:00
|
|
|
Ok(a) => a,
|
2022-08-05 18:59:50 -05:00
|
|
|
Err(_) => String::new(),
|
2020-05-26 11:40:42 -05:00
|
|
|
};
|
|
|
|
// FIXME this is a false negative
|
|
|
|
#[allow(clippy::cmp_owned)]
|
|
|
|
if String::from("a") == TryInto::<String>::try_into(String::from("a")).unwrap() {}
|
2020-05-09 14:28:31 -05:00
|
|
|
}
|