Skip to content

Commit f6882ed

Browse files
authored
Merge pull request #3287 from JoshMcguigan/cmp_owned-2925
cmp_owned false positive
2 parents 995a974 + ad5c29a commit f6882ed

File tree

3 files changed

+27
-15
lines changed

3 files changed

+27
-15
lines changed

clippy_lints/src/misc.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -334,11 +334,11 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
334334
|db| {
335335
let sugg = if binop.node == BinOpKind::Or { !sugg } else { sugg };
336336
db.span_suggestion_with_applicability(
337-
s.span,
337+
s.span,
338338
"replace it with",
339339
format!(
340340
"if {} {{ {}; }}",
341-
sugg,
341+
sugg,
342342
&snippet(cx, b.span, ".."),
343343
),
344344
Applicability::MachineApplicable, // snippet
@@ -520,16 +520,17 @@ fn check_to_owned(cx: &LateContext<'_, '_>, expr: &Expr, other: &Expr) {
520520
None => return,
521521
};
522522

523-
// *arg impls PartialEq<other>
524-
if !arg_ty
523+
let deref_arg_impl_partial_eq_other = arg_ty
525524
.builtin_deref(true)
526-
.map_or(false, |tam| implements_trait(cx, tam.ty, partial_eq_trait_id, &[other_ty.into()]))
527-
// arg impls PartialEq<*other>
528-
&& !other_ty
525+
.map_or(false, |tam| implements_trait(cx, tam.ty, partial_eq_trait_id, &[other_ty.into()]));
526+
let arg_impl_partial_eq_deref_other = other_ty
529527
.builtin_deref(true)
530-
.map_or(false, |tam| implements_trait(cx, arg_ty, partial_eq_trait_id, &[tam.ty.into()]))
531-
// arg impls PartialEq<other>
532-
&& !implements_trait(cx, arg_ty, partial_eq_trait_id, &[other_ty.into()])
528+
.map_or(false, |tam| implements_trait(cx, arg_ty, partial_eq_trait_id, &[tam.ty.into()]));
529+
let arg_impl_partial_eq_other = implements_trait(cx, arg_ty, partial_eq_trait_id, &[other_ty.into()]);
530+
531+
if !deref_arg_impl_partial_eq_other
532+
&& !arg_impl_partial_eq_deref_other
533+
&& !arg_impl_partial_eq_other
533534
{
534535
return;
535536
}
@@ -559,10 +560,11 @@ fn check_to_owned(cx: &LateContext<'_, '_>, expr: &Expr, other: &Expr) {
559560
}
560561
}
561562
}
563+
let try_hint = if deref_arg_impl_partial_eq_other { format!("*{}", snip) } else { snip.to_string() };
562564
db.span_suggestion_with_applicability(
563-
expr.span,
565+
expr.span,
564566
"try",
565-
snip.to_string(),
567+
try_hint,
566568
Applicability::MachineApplicable, // snippet
567569
);
568570
},

tests/ui/cmp_owned.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ fn main() {
3131
42.to_string() == "42";
3232

3333
Foo.to_owned() == Foo;
34+
35+
"abc".chars().filter(|c| c.to_owned() != 'X');
36+
37+
"abc".chars().filter(|c| *c != 'X');
3438
}
3539

3640
struct Foo;

tests/ui/cmp_owned.stderr

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,16 @@ error: this creates an owned instance just for comparison
3131
| ^^^^^^^^^^^^^^ help: try: `Foo`
3232

3333
error: this creates an owned instance just for comparison
34-
--> $DIR/cmp_owned.rs:40:9
34+
--> $DIR/cmp_owned.rs:35:30
3535
|
36-
40 | self.to_owned() == *other
36+
35 | "abc".chars().filter(|c| c.to_owned() != 'X');
37+
| ^^^^^^^^^^^^ help: try: `*c`
38+
39+
error: this creates an owned instance just for comparison
40+
--> $DIR/cmp_owned.rs:44:9
41+
|
42+
44 | self.to_owned() == *other
3743
| ^^^^^^^^^^^^^^^ try calling implementing the comparison without allocating
3844

39-
error: aborting due to 6 previous errors
45+
error: aborting due to 7 previous errors
4046

0 commit comments

Comments
 (0)