about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--clippy_lints/src/operators/cmp_owned.rs2
-rw-r--r--tests/ui/cmp_owned/with_suggestion.fixed9
-rw-r--r--tests/ui/cmp_owned/with_suggestion.rs9
-rw-r--r--tests/ui/cmp_owned/with_suggestion.stderr14
4 files changed, 32 insertions, 2 deletions
diff --git a/clippy_lints/src/operators/cmp_owned.rs b/clippy_lints/src/operators/cmp_owned.rs
index cf6b8992973..9b2cfd91b85 100644
--- a/clippy_lints/src/operators/cmp_owned.rs
+++ b/clippy_lints/src/operators/cmp_owned.rs
@@ -98,7 +98,7 @@ fn check_op(cx: &LateContext<'_>, expr: &Expr<'_>, other: &Expr<'_>, left: bool)
             let arg_snip = snippet(cx, arg_span, "..");
             let expr_snip;
             let eq_impl;
-            if with_deref.is_implemented() {
+            if with_deref.is_implemented() && !arg_ty.peel_refs().is_str() {
                 expr_snip = format!("*{arg_snip}");
                 eq_impl = with_deref;
             } else {
diff --git a/tests/ui/cmp_owned/with_suggestion.fixed b/tests/ui/cmp_owned/with_suggestion.fixed
index eb01633a25f..85d0991bef0 100644
--- a/tests/ui/cmp_owned/with_suggestion.fixed
+++ b/tests/ui/cmp_owned/with_suggestion.fixed
@@ -74,3 +74,12 @@ impl ToOwned for Baz {
         Baz
     }
 }
+
+fn issue_8103() {
+    let foo1 = String::from("foo");
+    let _ = foo1 == "foo";
+    //~^ cmp_owned
+    let foo2 = "foo";
+    let _ = foo1 == foo2;
+    //~^ cmp_owned
+}
diff --git a/tests/ui/cmp_owned/with_suggestion.rs b/tests/ui/cmp_owned/with_suggestion.rs
index 82409f27b12..2393757d76f 100644
--- a/tests/ui/cmp_owned/with_suggestion.rs
+++ b/tests/ui/cmp_owned/with_suggestion.rs
@@ -74,3 +74,12 @@ impl ToOwned for Baz {
         Baz
     }
 }
+
+fn issue_8103() {
+    let foo1 = String::from("foo");
+    let _ = foo1 == "foo".to_owned();
+    //~^ cmp_owned
+    let foo2 = "foo";
+    let _ = foo1 == foo2.to_owned();
+    //~^ cmp_owned
+}
diff --git a/tests/ui/cmp_owned/with_suggestion.stderr b/tests/ui/cmp_owned/with_suggestion.stderr
index ca2ab448472..dd9ffa70897 100644
--- a/tests/ui/cmp_owned/with_suggestion.stderr
+++ b/tests/ui/cmp_owned/with_suggestion.stderr
@@ -37,5 +37,17 @@ error: this creates an owned instance just for comparison
 LL |     "abc".chars().filter(|c| c.to_owned() != 'X');
    |                              ^^^^^^^^^^^^ help: try: `*c`
 
-error: aborting due to 6 previous errors
+error: this creates an owned instance just for comparison
+  --> tests/ui/cmp_owned/with_suggestion.rs:80:21
+   |
+LL |     let _ = foo1 == "foo".to_owned();
+   |                     ^^^^^^^^^^^^^^^^ help: try: `"foo"`
+
+error: this creates an owned instance just for comparison
+  --> tests/ui/cmp_owned/with_suggestion.rs:83:21
+   |
+LL |     let _ = foo1 == foo2.to_owned();
+   |                     ^^^^^^^^^^^^^^^ help: try: `foo2`
+
+error: aborting due to 8 previous errors