1- use std:: cmp:: Ordering ;
2-
31use rustc_hir:: { Expr , ExprKind } ;
42use rustc_lint:: { LateContext , LateLintPass } ;
53use rustc_middle:: ty:: layout:: LayoutOf ;
64use rustc_middle:: ty:: { self , IntTy , UintTy } ;
75use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
86use rustc_span:: Span ;
97
8+ use clippy_utils:: comparisons;
109use clippy_utils:: comparisons:: Rel ;
11- use clippy_utils:: consts:: { constant , Constant } ;
10+ use clippy_utils:: consts:: { constant_full_int , FullInt } ;
1211use clippy_utils:: diagnostics:: span_lint;
1312use clippy_utils:: source:: snippet;
14- use clippy_utils:: { comparisons, sext} ;
1513
1614declare_clippy_lint ! {
1715 /// ### What it does
@@ -39,53 +37,6 @@ declare_clippy_lint! {
3937
4038declare_lint_pass ! ( InvalidUpcastComparisons => [ INVALID_UPCAST_COMPARISONS ] ) ;
4139
42- #[ derive( Copy , Clone , Debug , Eq ) ]
43- enum FullInt {
44- S ( i128 ) ,
45- U ( u128 ) ,
46- }
47-
48- impl FullInt {
49- #[ allow( clippy:: cast_sign_loss) ]
50- #[ must_use]
51- fn cmp_s_u ( s : i128 , u : u128 ) -> Ordering {
52- if s < 0 {
53- Ordering :: Less
54- } else if u > ( i128:: MAX as u128 ) {
55- Ordering :: Greater
56- } else {
57- ( s as u128 ) . cmp ( & u)
58- }
59- }
60- }
61-
62- impl PartialEq for FullInt {
63- #[ must_use]
64- fn eq ( & self , other : & Self ) -> bool {
65- self . partial_cmp ( other) . expect ( "`partial_cmp` only returns `Some(_)`" ) == Ordering :: Equal
66- }
67- }
68-
69- impl PartialOrd for FullInt {
70- #[ must_use]
71- fn partial_cmp ( & self , other : & Self ) -> Option < Ordering > {
72- Some ( match ( self , other) {
73- ( & Self :: S ( s) , & Self :: S ( o) ) => s. cmp ( & o) ,
74- ( & Self :: U ( s) , & Self :: U ( o) ) => s. cmp ( & o) ,
75- ( & Self :: S ( s) , & Self :: U ( o) ) => Self :: cmp_s_u ( s, o) ,
76- ( & Self :: U ( s) , & Self :: S ( o) ) => Self :: cmp_s_u ( o, s) . reverse ( ) ,
77- } )
78- }
79- }
80-
81- impl Ord for FullInt {
82- #[ must_use]
83- fn cmp ( & self , other : & Self ) -> Ordering {
84- self . partial_cmp ( other)
85- . expect ( "`partial_cmp` for FullInt can never return `None`" )
86- }
87- }
88-
8940fn numeric_cast_precast_bounds < ' a > ( cx : & LateContext < ' _ > , expr : & ' a Expr < ' _ > ) -> Option < ( FullInt , FullInt ) > {
9041 if let ExprKind :: Cast ( cast_exp, _) = expr. kind {
9142 let pre_cast_ty = cx. typeck_results ( ) . expr_ty ( cast_exp) ;
@@ -118,19 +69,6 @@ fn numeric_cast_precast_bounds<'a>(cx: &LateContext<'_>, expr: &'a Expr<'_>) ->
11869 }
11970}
12071
121- fn node_as_const_fullint < ' tcx > ( cx : & LateContext < ' tcx > , expr : & ' tcx Expr < ' _ > ) -> Option < FullInt > {
122- let val = constant ( cx, cx. typeck_results ( ) , expr) ?. 0 ;
123- if let Constant :: Int ( const_int) = val {
124- match * cx. typeck_results ( ) . expr_ty ( expr) . kind ( ) {
125- ty:: Int ( ity) => Some ( FullInt :: S ( sext ( cx. tcx , const_int, ity) ) ) ,
126- ty:: Uint ( _) => Some ( FullInt :: U ( const_int) ) ,
127- _ => None ,
128- }
129- } else {
130- None
131- }
132- }
133-
13472fn err_upcast_comparison ( cx : & LateContext < ' _ > , span : Span , expr : & Expr < ' _ > , always : bool ) {
13573 if let ExprKind :: Cast ( cast_val, _) = expr. kind {
13674 span_lint (
@@ -156,7 +94,7 @@ fn upcast_comparison_bounds_err<'tcx>(
15694 invert : bool ,
15795) {
15896 if let Some ( ( lb, ub) ) = lhs_bounds {
159- if let Some ( norm_rhs_val) = node_as_const_fullint ( cx, rhs) {
97+ if let Some ( norm_rhs_val) = constant_full_int ( cx, cx . typeck_results ( ) , rhs) {
16098 if rel == Rel :: Eq || rel == Rel :: Ne {
16199 if norm_rhs_val < lb || norm_rhs_val > ub {
162100 err_upcast_comparison ( cx, span, lhs, rel == Rel :: Ne ) ;
0 commit comments