rust/tests/ui/manual_string_new.fixed

63 lines
2.0 KiB
Rust
Raw Normal View History

#![warn(clippy::manual_string_new)]
#![allow(clippy::unnecessary_fallible_conversions)]
macro_rules! create_strings_from_macro {
// When inside a macro, nothing should warn to prevent false positives.
($some_str:expr) => {
let _: String = $some_str.into();
let _ = $some_str.to_string();
};
}
fn main() {
// Method calls
let _ = String::new();
let _ = "no warning".to_string();
let _ = String::new();
let _ = "no warning".to_owned();
let _: String = String::new();
let _: String = "no warning".into();
let _: SomeOtherStruct = "no warning".into();
let _: SomeOtherStruct = "".into(); // No warning too. We are not converting into String.
// Calls
let _ = String::new();
let _ = String::new();
let _ = String::from("no warning");
let _ = SomeOtherStruct::from("no warning");
let _ = SomeOtherStruct::from(""); // Again: no warning.
let _ = String::new();
let _ = String::try_from("no warning").unwrap();
let _ = String::try_from("no warning").expect("this should not warn");
let _ = SomeOtherStruct::try_from("no warning").unwrap();
let _ = SomeOtherStruct::try_from("").unwrap(); // Again: no warning.
let _: String = String::new();
let _: String = From::from("no warning");
let _: SomeOtherStruct = From::from("no warning");
let _: SomeOtherStruct = From::from(""); // Again: no warning.
let _: String = String::new();
let _: String = TryFrom::try_from("no warning").unwrap();
let _: String = TryFrom::try_from("no warning").expect("this should not warn");
let _: String = String::new();
let _: SomeOtherStruct = TryFrom::try_from("no_warning").unwrap();
let _: SomeOtherStruct = TryFrom::try_from("").unwrap(); // Again: no warning.
// Macros (never warn)
create_strings_from_macro!("");
create_strings_from_macro!("Hey");
}
struct SomeOtherStruct {}
impl From<&str> for SomeOtherStruct {
fn from(_value: &str) -> Self {
Self {}
}
}