about summary refs log tree commit diff
diff options
context:
space:
mode:
authorSamuel Moelius <sam@moeli.us>2025-03-20 14:01:04 -0400
committerSamuel Moelius <sam@moeli.us>2025-05-19 19:29:06 -0400
commit8e5b0cdae1aaffcee5455852cb0eec0c6b56b17f (patch)
tree08c7548dd2dca8e268771696c344c681b57d0759
parent8a9adba8525e7bf40ca5f3b08d359742173f6fa7 (diff)
downloadrust-8e5b0cdae1aaffcee5455852cb0eec0c6b56b17f.tar.gz
rust-8e5b0cdae1aaffcee5455852cb0eec0c6b56b17f.zip
Account for changes from issue 14396
-rw-r--r--clippy_lints/src/strings.rs16
-rw-r--r--tests/ui/string_to_string.fixed17
-rw-r--r--tests/ui/string_to_string.rs4
-rw-r--r--tests/ui/string_to_string.stderr12
4 files changed, 23 insertions, 26 deletions
diff --git a/clippy_lints/src/strings.rs b/clippy_lints/src/strings.rs
index 73a9fe71e00..9fcef84eeb3 100644
--- a/clippy_lints/src/strings.rs
+++ b/clippy_lints/src/strings.rs
@@ -494,21 +494,9 @@ impl<'tcx> LateLintPass<'tcx> for StringToString {
                 if path.ident.name == sym::to_string
                     && let ty = cx.typeck_results().expr_ty(self_arg)
                     && is_type_lang_item(cx, ty.peel_refs(), LangItem::String)
+                    && let Some(parent_span) = is_called_from_map_like(cx, expr)
                 {
-                    if let Some(parent_span) = is_called_from_map_like(cx, expr) {
-                        suggest_cloned_string_to_string(cx, parent_span);
-                    } else {
-                        #[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
-                        span_lint_and_then(
-                            cx,
-                            STRING_TO_STRING,
-                            expr.span,
-                            "`to_string()` called on a `String`",
-                            |diag| {
-                                diag.help("consider using `.clone()`");
-                            },
-                        );
-                    }
+                    suggest_cloned_string_to_string(cx, parent_span);
                 }
             },
             ExprKind::Path(QPath::TypeRelative(ty, segment)) => {
diff --git a/tests/ui/string_to_string.fixed b/tests/ui/string_to_string.fixed
index 354b6c7c9fb..751f451cb68 100644
--- a/tests/ui/string_to_string.fixed
+++ b/tests/ui/string_to_string.fixed
@@ -1,8 +1,21 @@
-#![warn(clippy::implicit_clone)]
-#![allow(clippy::redundant_clone)]
+#![warn(clippy::implicit_clone, clippy::string_to_string)]
+#![allow(clippy::redundant_clone, clippy::unnecessary_literal_unwrap)]
 
 fn main() {
     let mut message = String::from("Hello");
     let mut v = message.clone();
     //~^ ERROR: implicitly cloning a `String` by calling `to_string` on its dereferenced type
+
+    let variable1 = String::new();
+    let v = &variable1;
+    let variable2 = Some(v);
+    let _ = variable2.map(|x| {
+        println!();
+        x.clone()
+        //~^ ERROR: implicitly cloning a `String` by calling `to_string` on its dereferenced type
+    });
+
+    let x = Some(String::new());
+    let _ = x.unwrap_or_else(|| v.clone());
+    //~^ ERROR: implicitly cloning a `String` by calling `to_string` on its dereferenced type
 }
diff --git a/tests/ui/string_to_string.rs b/tests/ui/string_to_string.rs
index 43a1cc18cd0..d519677d59e 100644
--- a/tests/ui/string_to_string.rs
+++ b/tests/ui/string_to_string.rs
@@ -12,10 +12,10 @@ fn main() {
     let _ = variable2.map(|x| {
         println!();
         x.to_string()
+        //~^ ERROR: implicitly cloning a `String` by calling `to_string` on its dereferenced type
     });
-    //~^^ string_to_string
 
     let x = Some(String::new());
     let _ = x.unwrap_or_else(|| v.to_string());
-    //~^ string_to_string
+    //~^ ERROR: implicitly cloning a `String` by calling `to_string` on its dereferenced type
 }
diff --git a/tests/ui/string_to_string.stderr b/tests/ui/string_to_string.stderr
index 0e33c6c1bf3..220fe3170c6 100644
--- a/tests/ui/string_to_string.stderr
+++ b/tests/ui/string_to_string.stderr
@@ -7,21 +7,17 @@ LL |     let mut v = message.to_string();
    = note: `-D clippy::implicit-clone` implied by `-D warnings`
    = help: to override `-D warnings` add `#[allow(clippy::implicit_clone)]`
 
-error: `to_string()` called on a `String`
+error: implicitly cloning a `String` by calling `to_string` on its dereferenced type
   --> tests/ui/string_to_string.rs:14:9
    |
 LL |         x.to_string()
-   |         ^^^^^^^^^^^^^
-   |
-   = help: consider using `.clone()`
+   |         ^^^^^^^^^^^^^ help: consider using: `x.clone()`
 
-error: `to_string()` called on a `String`
+error: implicitly cloning a `String` by calling `to_string` on its dereferenced type
   --> tests/ui/string_to_string.rs:19:33
    |
 LL |     let _ = x.unwrap_or_else(|| v.to_string());
-   |                                 ^^^^^^^^^^^^^
-   |
-   = help: consider using `.clone()`
+   |                                 ^^^^^^^^^^^^^ help: consider using: `v.clone()`
 
 error: aborting due to 3 previous errors