@@ -3,11 +3,11 @@ use rustc_ast::ast::LitKind;
33use rustc_errors:: Applicability ;
44use rustc_hir:: intravisit:: FnKind ;
55use rustc_hir:: {
6- def, BinOpKind , BindingAnnotation , Body , Expr , ExprKind , FnDecl , HirId , Mutability , PatKind , Stmt , StmtKind , Ty ,
7- TyKind , UnOp ,
6+ self as hir , def, BinOpKind , BindingAnnotation , Body , Expr , ExprKind , FnDecl , HirId , Mutability , PatKind , Stmt ,
7+ StmtKind , TyKind , UnOp ,
88} ;
99use rustc_lint:: { LateContext , LateLintPass } ;
10- use rustc_middle:: ty;
10+ use rustc_middle:: ty:: { self , Ty } ;
1111use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
1212use rustc_span:: hygiene:: DesugaringKind ;
1313use rustc_span:: source_map:: { ExpnKind , Span } ;
@@ -571,6 +571,15 @@ fn is_array(cx: &LateContext<'_, '_>, expr: &Expr<'_>) -> bool {
571571}
572572
573573fn check_to_owned ( cx : & LateContext < ' _ , ' _ > , expr : & Expr < ' _ > , other : & Expr < ' _ > ) {
574+ fn symmetric_partial_eq < ' tcx > ( cx : & LateContext < ' _ , ' tcx > , lhs : Ty < ' tcx > , rhs : Ty < ' tcx > ) -> bool {
575+ if let Some ( trait_def_id) = cx. tcx . lang_items ( ) . eq_trait ( ) {
576+ return implements_trait ( cx, lhs, trait_def_id, & [ rhs. into ( ) ] )
577+ && implements_trait ( cx, rhs, trait_def_id, & [ lhs. into ( ) ] ) ;
578+ }
579+
580+ false
581+ }
582+
574583 let ( arg_ty, snip) = match expr. kind {
575584 ExprKind :: MethodCall ( .., ref args, _) if args. len ( ) == 1 => {
576585 if match_trait_method ( cx, expr, & paths:: TO_STRING ) || match_trait_method ( cx, expr, & paths:: TO_OWNED ) {
@@ -594,18 +603,14 @@ fn check_to_owned(cx: &LateContext<'_, '_>, expr: &Expr<'_>, other: &Expr<'_>) {
594603 } ;
595604
596605 let other_ty = cx. tables . expr_ty_adjusted ( other) ;
597- let partial_eq_trait_id = match cx. tcx . lang_items ( ) . eq_trait ( ) {
598- Some ( id) => id,
599- None => return ,
600- } ;
601606
602- let deref_arg_impl_partial_eq_other = arg_ty. builtin_deref ( true ) . map_or ( false , |tam| {
603- implements_trait ( cx , tam . ty , partial_eq_trait_id , & [ other_ty . into ( ) ] )
604- } ) ;
605- let arg_impl_partial_eq_deref_other = other_ty. builtin_deref ( true ) . map_or ( false , |tam| {
606- implements_trait ( cx , arg_ty , partial_eq_trait_id , & [ tam . ty . into ( ) ] )
607- } ) ;
608- let arg_impl_partial_eq_other = implements_trait ( cx, arg_ty, partial_eq_trait_id , & [ other_ty. into ( ) ] ) ;
607+ let deref_arg_impl_partial_eq_other = arg_ty
608+ . builtin_deref ( true )
609+ . map_or ( false , |tam| symmetric_partial_eq ( cx , tam . ty , other_ty ) ) ;
610+ let arg_impl_partial_eq_deref_other = other_ty
611+ . builtin_deref ( true )
612+ . map_or ( false , |tam| symmetric_partial_eq ( cx , arg_ty , tam . ty ) ) ;
613+ let arg_impl_partial_eq_other = symmetric_partial_eq ( cx, arg_ty, other_ty) ;
609614
610615 if !deref_arg_impl_partial_eq_other && !arg_impl_partial_eq_deref_other && !arg_impl_partial_eq_other {
611616 return ;
@@ -694,7 +699,7 @@ fn non_macro_local(cx: &LateContext<'_, '_>, res: def::Res) -> bool {
694699 }
695700}
696701
697- fn check_cast ( cx : & LateContext < ' _ , ' _ > , span : Span , e : & Expr < ' _ > , ty : & Ty < ' _ > ) {
702+ fn check_cast ( cx : & LateContext < ' _ , ' _ > , span : Span , e : & Expr < ' _ > , ty : & hir :: Ty < ' _ > ) {
698703 if_chain ! {
699704 if let TyKind :: Ptr ( ref mut_ty) = ty. kind;
700705 if let ExprKind :: Lit ( ref lit) = e. kind;
0 commit comments