about summary refs log tree commit diff
path: root/tests
diff options
context:
space:
mode:
authorSamuel "Sam" Tardieu <sam@rfc1149.net>2023-03-11 10:20:44 +0100
committerSamuel Tardieu <sam@rfc1149.net>2023-03-16 21:42:04 +0100
commitffabdab8cf1499e1857d9ebcac2c410220137025 (patch)
treef837fab4e01472690de4bcf16cd3c826e245bb2b /tests
parent5afa93bd8ea57faa67f8ea75103971831066f283 (diff)
downloadrust-ffabdab8cf1499e1857d9ebcac2c410220137025.tar.gz
rust-ffabdab8cf1499e1857d9ebcac2c410220137025.zip
New lint to detect `&std::path::MAIN_SEPARATOR.to_string()`
Diffstat (limited to 'tests')
-rw-r--r--tests/ui/manual_main_separator_str.fixed39
-rw-r--r--tests/ui/manual_main_separator_str.rs39
-rw-r--r--tests/ui/manual_main_separator_str.stderr28
3 files changed, 106 insertions, 0 deletions
diff --git a/tests/ui/manual_main_separator_str.fixed b/tests/ui/manual_main_separator_str.fixed
new file mode 100644
index 00000000000..50f46d6b355
--- /dev/null
+++ b/tests/ui/manual_main_separator_str.fixed
@@ -0,0 +1,39 @@
+// run-rustfix
+
+#![allow(unused)]
+#![warn(clippy::manual_main_separator_str)]
+
+use std::path::MAIN_SEPARATOR;
+
+fn len(s: &str) -> usize {
+    s.len()
+}
+
+struct U<'a> {
+    f: &'a str,
+    g: &'a String,
+}
+
+struct V<T> {
+    f: T,
+}
+
+fn main() {
+    // Should lint
+    let _: &str = std::path::MAIN_SEPARATOR_STR;
+    let _ = len(std::path::MAIN_SEPARATOR_STR);
+    let _: Vec<u16> = std::path::MAIN_SEPARATOR_STR.encode_utf16().collect();
+
+    // Should lint for field `f` only
+    let _ = U {
+        f: std::path::MAIN_SEPARATOR_STR,
+        g: &MAIN_SEPARATOR.to_string(),
+    };
+
+    // Should not lint
+    let _: &String = &MAIN_SEPARATOR.to_string();
+    let _ = &MAIN_SEPARATOR.to_string();
+    let _ = V {
+        f: &MAIN_SEPARATOR.to_string(),
+    };
+}
diff --git a/tests/ui/manual_main_separator_str.rs b/tests/ui/manual_main_separator_str.rs
new file mode 100644
index 00000000000..2dbb9e66151
--- /dev/null
+++ b/tests/ui/manual_main_separator_str.rs
@@ -0,0 +1,39 @@
+// run-rustfix
+
+#![allow(unused)]
+#![warn(clippy::manual_main_separator_str)]
+
+use std::path::MAIN_SEPARATOR;
+
+fn len(s: &str) -> usize {
+    s.len()
+}
+
+struct U<'a> {
+    f: &'a str,
+    g: &'a String,
+}
+
+struct V<T> {
+    f: T,
+}
+
+fn main() {
+    // Should lint
+    let _: &str = &MAIN_SEPARATOR.to_string();
+    let _ = len(&MAIN_SEPARATOR.to_string());
+    let _: Vec<u16> = MAIN_SEPARATOR.to_string().encode_utf16().collect();
+
+    // Should lint for field `f` only
+    let _ = U {
+        f: &MAIN_SEPARATOR.to_string(),
+        g: &MAIN_SEPARATOR.to_string(),
+    };
+
+    // Should not lint
+    let _: &String = &MAIN_SEPARATOR.to_string();
+    let _ = &MAIN_SEPARATOR.to_string();
+    let _ = V {
+        f: &MAIN_SEPARATOR.to_string(),
+    };
+}
diff --git a/tests/ui/manual_main_separator_str.stderr b/tests/ui/manual_main_separator_str.stderr
new file mode 100644
index 00000000000..e6cefde66a7
--- /dev/null
+++ b/tests/ui/manual_main_separator_str.stderr
@@ -0,0 +1,28 @@
+error: taking a reference on `std::path::MAIN_SEPARATOR` conversion to `String`
+  --> $DIR/manual_main_separator_str.rs:23:19
+   |
+LL |     let _: &str = &MAIN_SEPARATOR.to_string();
+   |                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `std::path::MAIN_SEPARATOR_STR`
+   |
+   = note: `-D clippy::manual-main-separator-str` implied by `-D warnings`
+
+error: taking a reference on `std::path::MAIN_SEPARATOR` conversion to `String`
+  --> $DIR/manual_main_separator_str.rs:24:17
+   |
+LL |     let _ = len(&MAIN_SEPARATOR.to_string());
+   |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `std::path::MAIN_SEPARATOR_STR`
+
+error: taking a reference on `std::path::MAIN_SEPARATOR` conversion to `String`
+  --> $DIR/manual_main_separator_str.rs:25:23
+   |
+LL |     let _: Vec<u16> = MAIN_SEPARATOR.to_string().encode_utf16().collect();
+   |                       ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `std::path::MAIN_SEPARATOR_STR`
+
+error: taking a reference on `std::path::MAIN_SEPARATOR` conversion to `String`
+  --> $DIR/manual_main_separator_str.rs:29:12
+   |
+LL |         f: &MAIN_SEPARATOR.to_string(),
+   |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `std::path::MAIN_SEPARATOR_STR`
+
+error: aborting due to 4 previous errors
+