summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/tests/ui/manual_string_new.fixed
diff options
context:
space:
mode:
Diffstat (limited to 'src/tools/clippy/tests/ui/manual_string_new.fixed')
-rw-r--r--src/tools/clippy/tests/ui/manual_string_new.fixed63
1 files changed, 63 insertions, 0 deletions
diff --git a/src/tools/clippy/tests/ui/manual_string_new.fixed b/src/tools/clippy/tests/ui/manual_string_new.fixed
new file mode 100644
index 000000000..a376411bf
--- /dev/null
+++ b/src/tools/clippy/tests/ui/manual_string_new.fixed
@@ -0,0 +1,63 @@
+// run-rustfix
+
+#![warn(clippy::manual_string_new)]
+
+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 {}
+ }
+}