Skip to content

Commit d364d8a

Browse files
committed
Auto merge of rust-lang#8299 - marekdownar:8214, r=Manishearth
rust-lang#8214 cmp_owned suggestion flips the comparison changelog: ``[`cmp_owned`]`` fixes rust-lang#8214 so that the suggestion does not flip the comparison
2 parents 537a7f3 + 5b6ec8c commit d364d8a

File tree

4 files changed

+95
-2
lines changed

4 files changed

+95
-2
lines changed

Diff for: clippy_lints/src/misc.rs

+19-2
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,7 @@ fn is_array(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
548548
matches!(&cx.typeck_results().expr_ty(expr).peel_refs().kind(), ty::Array(_, _))
549549
}
550550

551+
#[allow(clippy::too_many_lines)]
551552
fn check_to_owned(cx: &LateContext<'_>, expr: &Expr<'_>, other: &Expr<'_>, left: bool) {
552553
#[derive(Default)]
553554
struct EqImpl {
@@ -644,10 +645,26 @@ fn check_to_owned(cx: &LateContext<'_>, expr: &Expr<'_>, other: &Expr<'_>, left:
644645
hint = expr_snip;
645646
} else {
646647
span = expr.span.to(other.span);
648+
649+
let cmp_span = if other.span < expr.span {
650+
other.span.between(expr.span)
651+
} else {
652+
expr.span.between(other.span)
653+
};
647654
if eq_impl.ty_eq_other {
648-
hint = format!("{} == {}", expr_snip, snippet(cx, other.span, ".."));
655+
hint = format!(
656+
"{}{}{}",
657+
expr_snip,
658+
snippet(cx, cmp_span, ".."),
659+
snippet(cx, other.span, "..")
660+
);
649661
} else {
650-
hint = format!("{} == {}", snippet(cx, other.span, ".."), expr_snip);
662+
hint = format!(
663+
"{}{}{}",
664+
snippet(cx, other.span, ".."),
665+
snippet(cx, cmp_span, ".."),
666+
expr_snip
667+
);
651668
}
652669
}
653670

Diff for: tests/ui/cmp_owned/comparison_flip.fixed

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// run-rustfix
2+
3+
use std::fmt::{self, Display};
4+
5+
fn main() {
6+
let a = Foo;
7+
8+
if a != "bar" {
9+
println!("foo");
10+
}
11+
12+
if a != "bar" {
13+
println!("foo");
14+
}
15+
}
16+
17+
struct Foo;
18+
19+
impl Display for Foo {
20+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
21+
write!(f, "foo")
22+
}
23+
}
24+
25+
impl PartialEq<&str> for Foo {
26+
fn eq(&self, other: &&str) -> bool {
27+
"foo" == *other
28+
}
29+
}

Diff for: tests/ui/cmp_owned/comparison_flip.rs

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// run-rustfix
2+
3+
use std::fmt::{self, Display};
4+
5+
fn main() {
6+
let a = Foo;
7+
8+
if a.to_string() != "bar" {
9+
println!("foo");
10+
}
11+
12+
if "bar" != a.to_string() {
13+
println!("foo");
14+
}
15+
}
16+
17+
struct Foo;
18+
19+
impl Display for Foo {
20+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
21+
write!(f, "foo")
22+
}
23+
}
24+
25+
impl PartialEq<&str> for Foo {
26+
fn eq(&self, other: &&str) -> bool {
27+
"foo" == *other
28+
}
29+
}

Diff for: tests/ui/cmp_owned/comparison_flip.stderr

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error: this creates an owned instance just for comparison
2+
--> $DIR/comparison_flip.rs:8:8
3+
|
4+
LL | if a.to_string() != "bar" {
5+
| ^^^^^^^^^^^^^ help: try: `a`
6+
|
7+
= note: `-D clippy::cmp-owned` implied by `-D warnings`
8+
9+
error: this creates an owned instance just for comparison
10+
--> $DIR/comparison_flip.rs:12:17
11+
|
12+
LL | if "bar" != a.to_string() {
13+
| ---------^^^^^^^^^^^^^
14+
| |
15+
| help: try: `a != "bar"`
16+
17+
error: aborting due to 2 previous errors
18+

0 commit comments

Comments
 (0)