Skip to content

Commit 625bd55

Browse files
Fix cmp_owned suggests wrongly on PathBuf (#16628)
Closes #16448 Closes #16564 The previous implementation hardcoded bans on `str` to avoid adding `*` when comparing with `String`, which becomes a problem for `Pathbuf` as `PartialEq<&str>` is not yet implemented on it. changelog: [`cmp_owned`] fix wrong suggestions on `PathBuf`
2 parents a2b46cc + b26ef2a commit 625bd55

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

clippy_lints/src/operators/cmp_owned.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,10 @@ fn check_op(cx: &LateContext<'_>, outer: &Expr<'_>, expr: &Expr<'_>, other: &Exp
100100

101101
let mut applicability = Applicability::MachineApplicable;
102102
let (arg_snip, _) = snippet_with_context(cx, arg_span, expr.span.ctxt(), "..", &mut applicability);
103-
let (expr_snip, eq_impl) = if with_deref.is_implemented() && !arg_ty.peel_refs().is_str() {
104-
(format!("*{arg_snip}"), with_deref)
105-
} else {
103+
let (expr_snip, eq_impl) = if without_deref.is_implemented() {
106104
(arg_snip.to_string(), without_deref)
105+
} else {
106+
(format!("*{arg_snip}"), with_deref)
107107
};
108108

109109
let (span, hint) = if (eq_impl.ty_eq_other && left) || (eq_impl.other_eq_ty && !left) {

tests/ui/cmp_owned/with_suggestion.fixed

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,11 @@ fn issue16458() {
150150
required_version: String, m!().to_string();
151151
}
152152
}
153+
154+
fn issue16564(path: std::path::PathBuf) {
155+
const ROOT: &str = "root";
156+
if path != *ROOT {
157+
//~^ cmp_owned
158+
todo!()
159+
}
160+
}

tests/ui/cmp_owned/with_suggestion.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,11 @@ fn issue16458() {
150150
required_version: String, m!().to_string();
151151
}
152152
}
153+
154+
fn issue16564(path: std::path::PathBuf) {
155+
const ROOT: &str = "root";
156+
if path != std::path::PathBuf::from(ROOT) {
157+
//~^ cmp_owned
158+
todo!()
159+
}
160+
}

tests/ui/cmp_owned/with_suggestion.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,5 +68,11 @@ LL | | }
6868
|
6969
= note: this error originates in the macro `all_comes_from_macro` (in Nightly builds, run with -Z macro-backtrace for more info)
7070

71-
error: aborting due to 10 previous errors
71+
error: this creates an owned instance just for comparison
72+
--> tests/ui/cmp_owned/with_suggestion.rs:156:16
73+
|
74+
LL | if path != std::path::PathBuf::from(ROOT) {
75+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `*ROOT`
76+
77+
error: aborting due to 11 previous errors
7278

0 commit comments

Comments
 (0)