about summary refs log tree commit diff
path: root/tests
diff options
context:
space:
mode:
authorTimo <30553356+y21@users.noreply.github.com>2025-08-02 11:41:00 +0000
committerGitHub <noreply@github.com>2025-08-02 11:41:00 +0000
commite8185ec091d7fe71b03cacb7846ae1c8323e013a (patch)
tree79856308959be898a2070439f6db56452205d9e1 /tests
parentffcd12946e2d986e50174649e81463707ce7b8c3 (diff)
parente910c0e7e6338bdc4c63b584bfff3f2abf1c3896 (diff)
downloadrust-e8185ec091d7fe71b03cacb7846ae1c8323e013a.tar.gz
rust-e8185ec091d7fe71b03cacb7846ae1c8323e013a.zip
Extend `implicit_clone` to handle `to_string` calls (#14177)
Put another way, merge `string_to_string` into `implicit_clone`, as
suggested here:
https://github.com/rust-lang/rust-clippy/issues/14173#issuecomment-2645846915

Note: [I
wrote](https://github.com/rust-lang/rust-clippy/commit/b8913894a13431bea99400dc9f53a1fd9f41a6c6)
this comment:
https://github.com/rust-lang/rust-clippy/blob/6cdb7f68c39a2458c6b8f6dc63da4123a6a5af89/clippy_lints/src/methods/implicit_clone.rs#L43-L45

Here is the context for why I wrote it:
https://github.com/rust-lang/rust-clippy/pull/7978#discussion_r769128853

Regardless, it's probably time for the comment to go away. Extending
`implicit_clone` to handle `to_string` calls yields many hits within
Clippy's codebase.

changelog: extend `implicit_clone` to handle `to_string` calls
Diffstat (limited to 'tests')
-rw-r--r--tests/ui/deprecated.rs1
-rw-r--r--tests/ui/deprecated.stderr18
-rw-r--r--tests/ui/implicit_clone.fixed6
-rw-r--r--tests/ui/implicit_clone.rs6
-rw-r--r--tests/ui/implicit_clone.stderr14
-rw-r--r--tests/ui/string_to_string.rs21
-rw-r--r--tests/ui/string_to_string.stderr28
-rw-r--r--tests/ui/string_to_string_in_map.fixed20
-rw-r--r--tests/ui/string_to_string_in_map.rs20
-rw-r--r--tests/ui/string_to_string_in_map.stderr38
10 files changed, 38 insertions, 134 deletions
diff --git a/tests/ui/deprecated.rs b/tests/ui/deprecated.rs
index 6b69bdd29ce..9743a83fb93 100644
--- a/tests/ui/deprecated.rs
+++ b/tests/ui/deprecated.rs
@@ -12,6 +12,7 @@
 #![warn(clippy::regex_macro)] //~ ERROR: lint `clippy::regex_macro`
 #![warn(clippy::replace_consts)] //~ ERROR: lint `clippy::replace_consts`
 #![warn(clippy::should_assert_eq)] //~ ERROR: lint `clippy::should_assert_eq`
+#![warn(clippy::string_to_string)] //~ ERROR: lint `clippy::string_to_string`
 #![warn(clippy::unsafe_vector_initialization)] //~ ERROR: lint `clippy::unsafe_vector_initialization`
 #![warn(clippy::unstable_as_mut_slice)] //~ ERROR: lint `clippy::unstable_as_mut_slice`
 #![warn(clippy::unstable_as_slice)] //~ ERROR: lint `clippy::unstable_as_slice`
diff --git a/tests/ui/deprecated.stderr b/tests/ui/deprecated.stderr
index 07e59d33d60..cd225da611c 100644
--- a/tests/ui/deprecated.stderr
+++ b/tests/ui/deprecated.stderr
@@ -61,35 +61,41 @@ error: lint `clippy::should_assert_eq` has been removed: `assert!(a == b)` can n
 LL | #![warn(clippy::should_assert_eq)]
    |         ^^^^^^^^^^^^^^^^^^^^^^^^
 
-error: lint `clippy::unsafe_vector_initialization` has been removed: the suggested alternative could be substantially slower
+error: lint `clippy::string_to_string` has been removed: `clippy:implicit_clone` covers those cases
   --> tests/ui/deprecated.rs:15:9
    |
+LL | #![warn(clippy::string_to_string)]
+   |         ^^^^^^^^^^^^^^^^^^^^^^^^
+
+error: lint `clippy::unsafe_vector_initialization` has been removed: the suggested alternative could be substantially slower
+  --> tests/ui/deprecated.rs:16:9
+   |
 LL | #![warn(clippy::unsafe_vector_initialization)]
    |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
 error: lint `clippy::unstable_as_mut_slice` has been removed: `Vec::as_mut_slice` is now stable
-  --> tests/ui/deprecated.rs:16:9
+  --> tests/ui/deprecated.rs:17:9
    |
 LL | #![warn(clippy::unstable_as_mut_slice)]
    |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
 error: lint `clippy::unstable_as_slice` has been removed: `Vec::as_slice` is now stable
-  --> tests/ui/deprecated.rs:17:9
+  --> tests/ui/deprecated.rs:18:9
    |
 LL | #![warn(clippy::unstable_as_slice)]
    |         ^^^^^^^^^^^^^^^^^^^^^^^^^
 
 error: lint `clippy::unused_collect` has been removed: `Iterator::collect` is now marked as `#[must_use]`
-  --> tests/ui/deprecated.rs:18:9
+  --> tests/ui/deprecated.rs:19:9
    |
 LL | #![warn(clippy::unused_collect)]
    |         ^^^^^^^^^^^^^^^^^^^^^^
 
 error: lint `clippy::wrong_pub_self_convention` has been removed: `clippy::wrong_self_convention` now covers this case via the `avoid-breaking-exported-api` config
-  --> tests/ui/deprecated.rs:19:9
+  --> tests/ui/deprecated.rs:20:9
    |
 LL | #![warn(clippy::wrong_pub_self_convention)]
    |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
-error: aborting due to 15 previous errors
+error: aborting due to 16 previous errors
 
diff --git a/tests/ui/implicit_clone.fixed b/tests/ui/implicit_clone.fixed
index d60d1cb0ec0..267514c5f3d 100644
--- a/tests/ui/implicit_clone.fixed
+++ b/tests/ui/implicit_clone.fixed
@@ -135,4 +135,10 @@ fn main() {
     }
     let no_clone = &NoClone;
     let _ = no_clone.to_owned();
+
+    let s = String::from("foo");
+    let _ = s.clone();
+    //~^ implicit_clone
+    let _ = s.clone();
+    //~^ implicit_clone
 }
diff --git a/tests/ui/implicit_clone.rs b/tests/ui/implicit_clone.rs
index b96828f28c8..fba954026e7 100644
--- a/tests/ui/implicit_clone.rs
+++ b/tests/ui/implicit_clone.rs
@@ -135,4 +135,10 @@ fn main() {
     }
     let no_clone = &NoClone;
     let _ = no_clone.to_owned();
+
+    let s = String::from("foo");
+    let _ = s.to_owned();
+    //~^ implicit_clone
+    let _ = s.to_string();
+    //~^ implicit_clone
 }
diff --git a/tests/ui/implicit_clone.stderr b/tests/ui/implicit_clone.stderr
index 1eb6ff1fe42..4cca9b0d0c0 100644
--- a/tests/ui/implicit_clone.stderr
+++ b/tests/ui/implicit_clone.stderr
@@ -67,5 +67,17 @@ error: implicitly cloning a `PathBuf` by calling `to_path_buf` on its dereferenc
 LL |     let _ = pathbuf_ref.to_path_buf();
    |             ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `(**pathbuf_ref).clone()`
 
-error: aborting due to 11 previous errors
+error: implicitly cloning a `String` by calling `to_owned` on its dereferenced type
+  --> tests/ui/implicit_clone.rs:140:13
+   |
+LL |     let _ = s.to_owned();
+   |             ^^^^^^^^^^^^ help: consider using: `s.clone()`
+
+error: implicitly cloning a `String` by calling `to_string` on its dereferenced type
+  --> tests/ui/implicit_clone.rs:142:13
+   |
+LL |     let _ = s.to_string();
+   |             ^^^^^^^^^^^^^ help: consider using: `s.clone()`
+
+error: aborting due to 13 previous errors
 
diff --git a/tests/ui/string_to_string.rs b/tests/ui/string_to_string.rs
deleted file mode 100644
index 7c5bd8a897b..00000000000
--- a/tests/ui/string_to_string.rs
+++ /dev/null
@@ -1,21 +0,0 @@
-#![warn(clippy::string_to_string)]
-#![allow(clippy::redundant_clone, clippy::unnecessary_literal_unwrap)]
-
-fn main() {
-    let mut message = String::from("Hello");
-    let mut v = message.to_string();
-    //~^ string_to_string
-
-    let variable1 = String::new();
-    let v = &variable1;
-    let variable2 = Some(v);
-    let _ = variable2.map(|x| {
-        println!();
-        x.to_string()
-    });
-    //~^^ string_to_string
-
-    let x = Some(String::new());
-    let _ = x.unwrap_or_else(|| v.to_string());
-    //~^ string_to_string
-}
diff --git a/tests/ui/string_to_string.stderr b/tests/ui/string_to_string.stderr
deleted file mode 100644
index 99eea06f18e..00000000000
--- a/tests/ui/string_to_string.stderr
+++ /dev/null
@@ -1,28 +0,0 @@
-error: `to_string()` called on a `String`
-  --> tests/ui/string_to_string.rs:6:17
-   |
-LL |     let mut v = message.to_string();
-   |                 ^^^^^^^^^^^^^^^^^^^
-   |
-   = help: consider using `.clone()`
-   = note: `-D clippy::string-to-string` implied by `-D warnings`
-   = help: to override `-D warnings` add `#[allow(clippy::string_to_string)]`
-
-error: `to_string()` called on a `String`
-  --> tests/ui/string_to_string.rs:14:9
-   |
-LL |         x.to_string()
-   |         ^^^^^^^^^^^^^
-   |
-   = help: consider using `.clone()`
-
-error: `to_string()` called on a `String`
-  --> tests/ui/string_to_string.rs:19:33
-   |
-LL |     let _ = x.unwrap_or_else(|| v.to_string());
-   |                                 ^^^^^^^^^^^^^
-   |
-   = help: consider using `.clone()`
-
-error: aborting due to 3 previous errors
-
diff --git a/tests/ui/string_to_string_in_map.fixed b/tests/ui/string_to_string_in_map.fixed
deleted file mode 100644
index efc085539f1..00000000000
--- a/tests/ui/string_to_string_in_map.fixed
+++ /dev/null
@@ -1,20 +0,0 @@
-#![deny(clippy::string_to_string)]
-#![allow(clippy::unnecessary_literal_unwrap, clippy::useless_vec, clippy::iter_cloned_collect)]
-
-fn main() {
-    let variable1 = String::new();
-    let v = &variable1;
-    let variable2 = Some(v);
-    let _ = variable2.cloned();
-    //~^ string_to_string
-    let _ = variable2.cloned();
-    //~^ string_to_string
-    #[rustfmt::skip]
-    let _ = variable2.cloned();
-    //~^ string_to_string
-
-    let _ = vec![String::new()].iter().cloned().collect::<Vec<_>>();
-    //~^ string_to_string
-    let _ = vec![String::new()].iter().cloned().collect::<Vec<_>>();
-    //~^ string_to_string
-}
diff --git a/tests/ui/string_to_string_in_map.rs b/tests/ui/string_to_string_in_map.rs
deleted file mode 100644
index 5bf1d7ba5a2..00000000000
--- a/tests/ui/string_to_string_in_map.rs
+++ /dev/null
@@ -1,20 +0,0 @@
-#![deny(clippy::string_to_string)]
-#![allow(clippy::unnecessary_literal_unwrap, clippy::useless_vec, clippy::iter_cloned_collect)]
-
-fn main() {
-    let variable1 = String::new();
-    let v = &variable1;
-    let variable2 = Some(v);
-    let _ = variable2.map(String::to_string);
-    //~^ string_to_string
-    let _ = variable2.map(|x| x.to_string());
-    //~^ string_to_string
-    #[rustfmt::skip]
-    let _ = variable2.map(|x| { x.to_string() });
-    //~^ string_to_string
-
-    let _ = vec![String::new()].iter().map(String::to_string).collect::<Vec<_>>();
-    //~^ string_to_string
-    let _ = vec![String::new()].iter().map(|x| x.to_string()).collect::<Vec<_>>();
-    //~^ string_to_string
-}
diff --git a/tests/ui/string_to_string_in_map.stderr b/tests/ui/string_to_string_in_map.stderr
deleted file mode 100644
index 35aeed656ee..00000000000
--- a/tests/ui/string_to_string_in_map.stderr
+++ /dev/null
@@ -1,38 +0,0 @@
-error: `to_string()` called on a `String`
-  --> tests/ui/string_to_string_in_map.rs:8:23
-   |
-LL |     let _ = variable2.map(String::to_string);
-   |                       ^^^^^^^^^^^^^^^^^^^^^^ help: try: `cloned()`
-   |
-note: the lint level is defined here
-  --> tests/ui/string_to_string_in_map.rs:1:9
-   |
-LL | #![deny(clippy::string_to_string)]
-   |         ^^^^^^^^^^^^^^^^^^^^^^^^
-
-error: `to_string()` called on a `String`
-  --> tests/ui/string_to_string_in_map.rs:10:23
-   |
-LL |     let _ = variable2.map(|x| x.to_string());
-   |                       ^^^^^^^^^^^^^^^^^^^^^^ help: try: `cloned()`
-
-error: `to_string()` called on a `String`
-  --> tests/ui/string_to_string_in_map.rs:13:23
-   |
-LL |     let _ = variable2.map(|x| { x.to_string() });
-   |                       ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `cloned()`
-
-error: `to_string()` called on a `String`
-  --> tests/ui/string_to_string_in_map.rs:16:40
-   |
-LL |     let _ = vec![String::new()].iter().map(String::to_string).collect::<Vec<_>>();
-   |                                        ^^^^^^^^^^^^^^^^^^^^^^ help: try: `cloned()`
-
-error: `to_string()` called on a `String`
-  --> tests/ui/string_to_string_in_map.rs:18:40
-   |
-LL |     let _ = vec![String::new()].iter().map(|x| x.to_string()).collect::<Vec<_>>();
-   |                                        ^^^^^^^^^^^^^^^^^^^^^^ help: try: `cloned()`
-
-error: aborting due to 5 previous errors
-