@@ -3,11 +3,11 @@ use rustc_ast::ast::LitKind;
3
3
use rustc_errors:: Applicability ;
4
4
use rustc_hir:: intravisit:: FnKind ;
5
5
use 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 ,
8
8
} ;
9
9
use rustc_lint:: { LateContext , LateLintPass } ;
10
- use rustc_middle:: ty;
10
+ use rustc_middle:: ty:: { self , Ty } ;
11
11
use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
12
12
use rustc_span:: hygiene:: DesugaringKind ;
13
13
use rustc_span:: source_map:: { ExpnKind , Span } ;
@@ -571,6 +571,15 @@ fn is_array(cx: &LateContext<'_, '_>, expr: &Expr<'_>) -> bool {
571
571
}
572
572
573
573
fn 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
+
574
583
let ( arg_ty, snip) = match expr. kind {
575
584
ExprKind :: MethodCall ( .., ref args, _) if args. len ( ) == 1 => {
576
585
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<'_>) {
594
603
} ;
595
604
596
605
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
- } ;
601
606
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) ;
609
614
610
615
if !deref_arg_impl_partial_eq_other && !arg_impl_partial_eq_deref_other && !arg_impl_partial_eq_other {
611
616
return ;
@@ -694,7 +699,7 @@ fn non_macro_local(cx: &LateContext<'_, '_>, res: def::Res) -> bool {
694
699
}
695
700
}
696
701
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 < ' _ > ) {
698
703
if_chain ! {
699
704
if let TyKind :: Ptr ( ref mut_ty) = ty. kind;
700
705
if let ExprKind :: Lit ( ref lit) = e. kind;
0 commit comments