@@ -21,7 +21,7 @@ use crate::utils::{get_item_name, get_parent_expr, implements_trait, in_constant
21
21
iter_input_pats, last_path_segment, match_qpath, match_trait_method, paths, snippet, span_lint,
22
22
span_lint_and_then, walk_ptrs_ty, SpanlessEq } ;
23
23
use crate :: utils:: sugg:: Sugg ;
24
- use crate :: syntax:: ast:: { LitKind , CRATE_NODE_ID } ;
24
+ use crate :: syntax:: ast:: LitKind ;
25
25
use crate :: consts:: { constant, Constant } ;
26
26
use crate :: rustc_errors:: Applicability ;
27
27
@@ -540,18 +540,10 @@ fn check_to_owned(cx: &LateContext<'_, '_>, expr: &Expr, other: &Expr) {
540
540
_ => false ,
541
541
} ;
542
542
543
- let ( lint_span, try_hint) = if deref_arg_impl_partial_eq_other {
544
- // suggest deref on the left
545
- ( expr. span , format ! ( "*{}" , snip) )
546
- } else if other_gets_derefed {
547
- // suggest dropping the to_owned on the left and the deref on the right
548
- let other_snippet = snippet ( cx, other. span , ".." ) . into_owned ( ) ;
549
- let other_without_deref = other_snippet. replacen ( '*' , "" , 1 ) ;
550
-
551
- ( expr. span . to ( other. span ) , format ! ( "{} == {}" , snip. to_string( ) , other_without_deref) )
543
+ let lint_span = if other_gets_derefed {
544
+ expr. span . to ( other. span )
552
545
} else {
553
- // suggest dropping the to_owned on the left
554
- ( expr. span , snip. to_string ( ) )
546
+ expr. span
555
547
} ;
556
548
557
549
span_lint_and_then (
@@ -560,29 +552,20 @@ fn check_to_owned(cx: &LateContext<'_, '_>, expr: &Expr, other: &Expr) {
560
552
lint_span,
561
553
"this creates an owned instance just for comparison" ,
562
554
|db| {
563
- // this is as good as our recursion check can get, we can't prove that the
564
- // current function is
565
- // called by
566
- // PartialEq::eq, but we can at least ensure that this code is not part of it
567
- let parent_fn = cx. tcx . hir . get_parent ( expr. id ) ;
568
- let parent_impl = cx. tcx . hir . get_parent ( parent_fn) ;
569
- if parent_impl != CRATE_NODE_ID {
570
- if let Node :: Item ( item) = cx. tcx . hir . get ( parent_impl) {
571
- if let ItemKind :: Impl ( .., Some ( ref trait_ref) , _, _) = item. node {
572
- if trait_ref. path . def . def_id ( ) == partial_eq_trait_id {
573
- // we are implementing PartialEq, don't suggest not doing `to_owned`, otherwise
574
- // we go into
575
- // recursion
576
- db. span_label ( lint_span, "try implementing the comparison without allocating" ) ;
577
- return ;
578
- }
579
- }
580
- }
581
- }
555
+ // this also catches PartialEq implementations that call to_owned
582
556
if other_gets_derefed {
583
557
db. span_label ( lint_span, "try implementing the comparison without allocating" ) ;
584
558
return ;
585
559
}
560
+
561
+ let try_hint = if deref_arg_impl_partial_eq_other {
562
+ // suggest deref on the left
563
+ format ! ( "*{}" , snip)
564
+ } else {
565
+ // suggest dropping the to_owned on the left
566
+ snip. to_string ( )
567
+ } ;
568
+
586
569
db. span_suggestion_with_applicability (
587
570
lint_span,
588
571
"try" ,
0 commit comments