@@ -657,10 +657,16 @@ fn lint_nan<'tcx>(
657657 cx. emit_span_lint ( INVALID_NAN_COMPARISONS , e. span , lint) ;
658658}
659659
660+ #[ derive( Debug , PartialEq ) ]
661+ enum ComparisonOp {
662+ BinOp ( hir:: BinOpKind ) ,
663+ Other ,
664+ }
665+
660666fn lint_wide_pointer < ' tcx > (
661667 cx : & LateContext < ' tcx > ,
662668 e : & ' tcx hir:: Expr < ' tcx > ,
663- binop : hir :: BinOpKind ,
669+ cmpop : ComparisonOp ,
664670 l : & ' tcx hir:: Expr < ' tcx > ,
665671 r : & ' tcx hir:: Expr < ' tcx > ,
666672) {
@@ -679,7 +685,7 @@ fn lint_wide_pointer<'tcx>(
679685 }
680686 } ;
681687
682- // PartialEq::{eq,ne} takes references, remove any explicit references
688+ // the left and right operands can have references, remove any explicit references
683689 let l = l. peel_borrows ( ) ;
684690 let r = r. peel_borrows ( ) ;
685691
@@ -707,8 +713,8 @@ fn lint_wide_pointer<'tcx>(
707713 ) ;
708714 } ;
709715
710- let ne = if binop == hir:: BinOpKind :: Ne { "!" } else { "" } ;
711- let is_eq_ne = matches ! ( binop , hir:: BinOpKind :: Eq | hir:: BinOpKind :: Ne ) ;
716+ let ne = if cmpop == ComparisonOp :: BinOp ( hir:: BinOpKind :: Ne ) { "!" } else { "" } ;
717+ let is_eq_ne = matches ! ( cmpop , ComparisonOp :: BinOp ( hir:: BinOpKind :: Eq | hir:: BinOpKind :: Ne ) ) ;
712718 let is_dyn_comparison = l_inner_ty_is_dyn && r_inner_ty_is_dyn;
713719
714720 let left = e. span . shrink_to_lo ( ) . until ( l_span. shrink_to_lo ( ) ) ;
@@ -745,12 +751,12 @@ fn lint_wide_pointer<'tcx>(
745751 AmbiguousWidePointerComparisonsAddrSuggestion :: Cast {
746752 deref_left,
747753 deref_right,
748- // those two Options are required for correctness as having
749- // an empty span and an empty suggestion is not permitted
750- left_before : ( l_ty_refs != 0 ) . then_some ( left ) ,
751- right_before : ( r_ty_refs != 0 ) . then ( || r_span . shrink_to_lo ( ) ) ,
752- left : l_span . shrink_to_hi ( ) ,
753- right ,
754+ paren_left : if l_ty_refs != 0 { ")" } else { "" } ,
755+ paren_right : if r_ty_refs != 0 { ")" } else { "" } ,
756+ left_before : ( l_ty_refs != 0 ) . then_some ( l_span . shrink_to_lo ( ) ) ,
757+ left_after : l_span . shrink_to_hi ( ) ,
758+ right_before : ( r_ty_refs != 0 ) . then_some ( r_span . shrink_to_lo ( ) ) ,
759+ right_after : r_span . shrink_to_hi ( ) ,
754760 }
755761 } ,
756762 } ,
@@ -773,7 +779,7 @@ impl<'tcx> LateLintPass<'tcx> for TypeLimits {
773779 cx. emit_span_lint ( UNUSED_COMPARISONS , e. span , UnusedComparisons ) ;
774780 } else {
775781 lint_nan ( cx, e, binop, l, r) ;
776- lint_wide_pointer ( cx, e, binop. node , l, r) ;
782+ lint_wide_pointer ( cx, e, ComparisonOp :: BinOp ( binop. node ) , l, r) ;
777783 }
778784 }
779785 }
@@ -782,16 +788,16 @@ impl<'tcx> LateLintPass<'tcx> for TypeLimits {
782788 if let ExprKind :: Path ( ref qpath) = path. kind
783789 && let Some ( def_id) = cx. qpath_res ( qpath, path. hir_id ) . opt_def_id ( )
784790 && let Some ( diag_item) = cx. tcx . get_diagnostic_name ( def_id)
785- && let Some ( binop ) = partialeq_binop ( diag_item) =>
791+ && let Some ( cmpop ) = diag_item_cmpop ( diag_item) =>
786792 {
787- lint_wide_pointer ( cx, e, binop , l, r) ;
793+ lint_wide_pointer ( cx, e, cmpop , l, r) ;
788794 }
789795 hir:: ExprKind :: MethodCall ( _, l, [ r] , _)
790796 if let Some ( def_id) = cx. typeck_results ( ) . type_dependent_def_id ( e. hir_id )
791797 && let Some ( diag_item) = cx. tcx . get_diagnostic_name ( def_id)
792- && let Some ( binop ) = partialeq_binop ( diag_item) =>
798+ && let Some ( cmpop ) = diag_item_cmpop ( diag_item) =>
793799 {
794- lint_wide_pointer ( cx, e, binop , l, r) ;
800+ lint_wide_pointer ( cx, e, cmpop , l, r) ;
795801 }
796802 _ => { }
797803 } ;
@@ -876,14 +882,20 @@ impl<'tcx> LateLintPass<'tcx> for TypeLimits {
876882 )
877883 }
878884
879- fn partialeq_binop ( diag_item : Symbol ) -> Option < hir:: BinOpKind > {
880- if diag_item == sym:: cmp_partialeq_eq {
881- Some ( hir:: BinOpKind :: Eq )
882- } else if diag_item == sym:: cmp_partialeq_ne {
883- Some ( hir:: BinOpKind :: Ne )
884- } else {
885- None
886- }
885+ fn diag_item_cmpop ( diag_item : Symbol ) -> Option < ComparisonOp > {
886+ Some ( match diag_item {
887+ sym:: cmp_ord_max => ComparisonOp :: Other ,
888+ sym:: cmp_ord_min => ComparisonOp :: Other ,
889+ sym:: ord_cmp_method => ComparisonOp :: Other ,
890+ sym:: cmp_partialeq_eq => ComparisonOp :: BinOp ( hir:: BinOpKind :: Eq ) ,
891+ sym:: cmp_partialeq_ne => ComparisonOp :: BinOp ( hir:: BinOpKind :: Ne ) ,
892+ sym:: cmp_partialord_cmp => ComparisonOp :: Other ,
893+ sym:: cmp_partialord_ge => ComparisonOp :: BinOp ( hir:: BinOpKind :: Ge ) ,
894+ sym:: cmp_partialord_gt => ComparisonOp :: BinOp ( hir:: BinOpKind :: Gt ) ,
895+ sym:: cmp_partialord_le => ComparisonOp :: BinOp ( hir:: BinOpKind :: Le ) ,
896+ sym:: cmp_partialord_lt => ComparisonOp :: BinOp ( hir:: BinOpKind :: Lt ) ,
897+ _ => return None ,
898+ } )
887899 }
888900 }
889901}
0 commit comments