Skip to content

Commit 3e52867

Browse files
authored
more natural suggestions for cmp_owned (rust-lang#14247)
Dereferencing string literals in suggestions is redundant. changelog: [`cmp_owned`]: more natural suggestions are provided for string literals now fixes rust-lang#8103
2 parents c4789a0 + 9c3b5cf commit 3e52867

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

clippy_lints/src/operators/cmp_owned.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ fn check_op(cx: &LateContext<'_>, expr: &Expr<'_>, other: &Expr<'_>, left: bool)
9898
let arg_snip = snippet(cx, arg_span, "..");
9999
let expr_snip;
100100
let eq_impl;
101-
if with_deref.is_implemented() {
101+
if with_deref.is_implemented() && !arg_ty.peel_refs().is_str() {
102102
expr_snip = format!("*{arg_snip}");
103103
eq_impl = with_deref;
104104
} else {

tests/ui/cmp_owned/with_suggestion.fixed

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,12 @@ impl ToOwned for Baz {
7474
Baz
7575
}
7676
}
77+
78+
fn issue_8103() {
79+
let foo1 = String::from("foo");
80+
let _ = foo1 == "foo";
81+
//~^ cmp_owned
82+
let foo2 = "foo";
83+
let _ = foo1 == foo2;
84+
//~^ cmp_owned
85+
}

tests/ui/cmp_owned/with_suggestion.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,12 @@ impl ToOwned for Baz {
7474
Baz
7575
}
7676
}
77+
78+
fn issue_8103() {
79+
let foo1 = String::from("foo");
80+
let _ = foo1 == "foo".to_owned();
81+
//~^ cmp_owned
82+
let foo2 = "foo";
83+
let _ = foo1 == foo2.to_owned();
84+
//~^ cmp_owned
85+
}

tests/ui/cmp_owned/with_suggestion.stderr

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,17 @@ error: this creates an owned instance just for comparison
3737
LL | "abc".chars().filter(|c| c.to_owned() != 'X');
3838
| ^^^^^^^^^^^^ help: try: `*c`
3939

40-
error: aborting due to 6 previous errors
40+
error: this creates an owned instance just for comparison
41+
--> tests/ui/cmp_owned/with_suggestion.rs:80:21
42+
|
43+
LL | let _ = foo1 == "foo".to_owned();
44+
| ^^^^^^^^^^^^^^^^ help: try: `"foo"`
45+
46+
error: this creates an owned instance just for comparison
47+
--> tests/ui/cmp_owned/with_suggestion.rs:83:21
48+
|
49+
LL | let _ = foo1 == foo2.to_owned();
50+
| ^^^^^^^^^^^^^^^ help: try: `foo2`
51+
52+
error: aborting due to 8 previous errors
4153

0 commit comments

Comments
 (0)