@@ -657,10 +657,16 @@ fn lint_nan<'tcx>(
657
657
cx. emit_span_lint ( INVALID_NAN_COMPARISONS , e. span , lint) ;
658
658
}
659
659
660
+ #[ derive( Debug , PartialEq ) ]
661
+ enum ComparisonOp {
662
+ BinOp ( hir:: BinOpKind ) ,
663
+ Other ,
664
+ }
665
+
660
666
fn lint_wide_pointer < ' tcx > (
661
667
cx : & LateContext < ' tcx > ,
662
668
e : & ' tcx hir:: Expr < ' tcx > ,
663
- binop : hir :: BinOpKind ,
669
+ cmpop : ComparisonOp ,
664
670
l : & ' tcx hir:: Expr < ' tcx > ,
665
671
r : & ' tcx hir:: Expr < ' tcx > ,
666
672
) {
@@ -679,7 +685,7 @@ fn lint_wide_pointer<'tcx>(
679
685
}
680
686
} ;
681
687
682
- // PartialEq::{eq,ne} takes references, remove any explicit references
688
+ // the left and right operands can have references, remove any explicit references
683
689
let l = l. peel_borrows ( ) ;
684
690
let r = r. peel_borrows ( ) ;
685
691
@@ -707,8 +713,8 @@ fn lint_wide_pointer<'tcx>(
707
713
) ;
708
714
} ;
709
715
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 ) ) ;
712
718
let is_dyn_comparison = l_inner_ty_is_dyn && r_inner_ty_is_dyn;
713
719
714
720
let left = e. span . shrink_to_lo ( ) . until ( l_span. shrink_to_lo ( ) ) ;
@@ -745,12 +751,12 @@ fn lint_wide_pointer<'tcx>(
745
751
AmbiguousWidePointerComparisonsAddrSuggestion :: Cast {
746
752
deref_left,
747
753
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 ( ) ,
754
760
}
755
761
} ,
756
762
} ,
@@ -773,7 +779,7 @@ impl<'tcx> LateLintPass<'tcx> for TypeLimits {
773
779
cx. emit_span_lint ( UNUSED_COMPARISONS , e. span , UnusedComparisons ) ;
774
780
} else {
775
781
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) ;
777
783
}
778
784
}
779
785
}
@@ -782,16 +788,16 @@ impl<'tcx> LateLintPass<'tcx> for TypeLimits {
782
788
if let ExprKind :: Path ( ref qpath) = path. kind
783
789
&& let Some ( def_id) = cx. qpath_res ( qpath, path. hir_id ) . opt_def_id ( )
784
790
&& 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) =>
786
792
{
787
- lint_wide_pointer ( cx, e, binop , l, r) ;
793
+ lint_wide_pointer ( cx, e, cmpop , l, r) ;
788
794
}
789
795
hir:: ExprKind :: MethodCall ( _, l, [ r] , _)
790
796
if let Some ( def_id) = cx. typeck_results ( ) . type_dependent_def_id ( e. hir_id )
791
797
&& 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) =>
793
799
{
794
- lint_wide_pointer ( cx, e, binop , l, r) ;
800
+ lint_wide_pointer ( cx, e, cmpop , l, r) ;
795
801
}
796
802
_ => { }
797
803
} ;
@@ -876,14 +882,20 @@ impl<'tcx> LateLintPass<'tcx> for TypeLimits {
876
882
)
877
883
}
878
884
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
+ } )
887
899
}
888
900
}
889
901
}
0 commit comments