@@ -20,8 +20,8 @@ use rustc_span::symbol::sym;
20
20
use clippy_utils:: consts:: { constant, Constant } ;
21
21
use clippy_utils:: sugg:: Sugg ;
22
22
use clippy_utils:: {
23
- get_item_name, get_parent_expr, in_constant, is_diag_trait_item , is_integer_const, iter_input_pats,
24
- last_path_segment , match_any_def_paths, path_def_id, paths, unsext, SpanlessEq ,
23
+ get_item_name, get_parent_expr, in_constant, is_integer_const, iter_input_pats, last_path_segment ,
24
+ match_any_def_paths, path_def_id, paths, unsext, SpanlessEq ,
25
25
} ;
26
26
27
27
declare_clippy_lint ! {
@@ -569,33 +569,30 @@ fn check_to_owned(cx: &LateContext<'_>, expr: &Expr<'_>, other: &Expr<'_>, left:
569
569
} )
570
570
}
571
571
572
- let ( arg_ty, snip) = match expr. kind {
573
- ExprKind :: MethodCall ( .., args, _) if args. len ( ) == 1 => {
574
- if_chain ! (
575
- if let Some ( expr_def_id) = cx. typeck_results( ) . type_dependent_def_id( expr. hir_id) ;
576
- if is_diag_trait_item( cx, expr_def_id, sym:: ToString )
577
- || is_diag_trait_item( cx, expr_def_id, sym:: ToOwned ) ;
578
- then {
579
- ( cx. typeck_results( ) . expr_ty( & args[ 0 ] ) , snippet( cx, args[ 0 ] . span, ".." ) )
580
- } else {
581
- return ;
582
- }
583
- )
572
+ let typeck = cx. typeck_results ( ) ;
573
+ let ( arg, arg_span) = match expr. kind {
574
+ ExprKind :: MethodCall ( .., [ arg] , _)
575
+ if typeck
576
+ . type_dependent_def_id ( expr. hir_id )
577
+ . and_then ( |id| cx. tcx . trait_of_item ( id) )
578
+ . map_or ( false , |id| {
579
+ matches ! ( cx. tcx. get_diagnostic_name( id) , Some ( sym:: ToString | sym:: ToOwned ) )
580
+ } ) =>
581
+ {
582
+ ( arg, arg. span )
584
583
} ,
585
- ExprKind :: Call ( path, [ arg] ) => {
584
+ ExprKind :: Call ( path, [ arg] )
586
585
if path_def_id ( cx, path)
587
586
. and_then ( |id| match_any_def_paths ( cx, id, & [ & paths:: FROM_STR_METHOD , & paths:: FROM_FROM ] ) )
588
- . is_some ( )
589
- {
590
- ( cx. typeck_results ( ) . expr_ty ( arg) , snippet ( cx, arg. span , ".." ) )
591
- } else {
592
- return ;
593
- }
587
+ . is_some ( ) =>
588
+ {
589
+ ( arg, arg. span )
594
590
} ,
595
591
_ => return ,
596
592
} ;
597
593
598
- let other_ty = cx. typeck_results ( ) . expr_ty ( other) ;
594
+ let arg_ty = typeck. expr_ty ( arg) ;
595
+ let other_ty = typeck. expr_ty ( other) ;
599
596
600
597
let without_deref = symmetric_partial_eq ( cx, arg_ty, other_ty) . unwrap_or_default ( ) ;
601
598
let with_deref = arg_ty
@@ -627,13 +624,14 @@ fn check_to_owned(cx: &LateContext<'_>, expr: &Expr<'_>, other: &Expr<'_>, left:
627
624
return ;
628
625
}
629
626
627
+ let arg_snip = snippet ( cx, arg_span, ".." ) ;
630
628
let expr_snip;
631
629
let eq_impl;
632
630
if with_deref. is_implemented ( ) {
633
- expr_snip = format ! ( "*{}" , snip ) ;
631
+ expr_snip = format ! ( "*{}" , arg_snip ) ;
634
632
eq_impl = with_deref;
635
633
} else {
636
- expr_snip = snip . to_string ( ) ;
634
+ expr_snip = arg_snip . to_string ( ) ;
637
635
eq_impl = without_deref;
638
636
} ;
639
637
0 commit comments