#8214 cmp_owned suggestion flips the comparison

This commit is contained in:
Marek Downar 2022-01-16 20:27:00 +01:00
parent 27845a9205
commit 5b6ec8c57d
No known key found for this signature in database
GPG key ID: 62D7E3EF71CA85B2
4 changed files with 96 additions and 3 deletions

View file

@ -548,6 +548,7 @@ fn is_array(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
matches!(&cx.typeck_results().expr_ty(expr).peel_refs().kind(), ty::Array(_, _))
}
#[allow(clippy::too_many_lines)]
fn check_to_owned(cx: &LateContext<'_>, expr: &Expr<'_>, other: &Expr<'_>, left: bool) {
#[derive(Default)]
struct EqImpl {
@ -644,10 +645,26 @@ fn check_to_owned(cx: &LateContext<'_>, expr: &Expr<'_>, other: &Expr<'_>, left:
hint = expr_snip;
} else {
span = expr.span.to(other.span);
if eq_impl.ty_eq_other {
hint = format!("{} == {}", expr_snip, snippet(cx, other.span, ".."));
let cmp_span = if other.span < expr.span {
other.span.between(expr.span)
} else {
hint = format!("{} == {}", snippet(cx, other.span, ".."), expr_snip);
expr.span.between(other.span)
};
if eq_impl.ty_eq_other {
hint = format!(
"{}{}{}",
expr_snip,
snippet(cx, cmp_span, ".."),
snippet(cx, other.span, "..")
);
} else {
hint = format!(
"{}{}{}",
snippet(cx, other.span, ".."),
snippet(cx, cmp_span, ".."),
expr_snip
);
}
}