1
- use std:: cmp:: Ordering ;
2
-
3
1
use rustc_hir:: { Expr , ExprKind } ;
4
2
use rustc_lint:: { LateContext , LateLintPass } ;
5
3
use rustc_middle:: ty:: layout:: LayoutOf ;
6
4
use rustc_middle:: ty:: { self , IntTy , UintTy } ;
7
5
use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
8
6
use rustc_span:: Span ;
9
7
8
+ use clippy_utils:: comparisons;
10
9
use clippy_utils:: comparisons:: Rel ;
11
- use clippy_utils:: consts:: { constant , Constant } ;
10
+ use clippy_utils:: consts:: { constant_full_int , FullInt } ;
12
11
use clippy_utils:: diagnostics:: span_lint;
13
12
use clippy_utils:: source:: snippet;
14
- use clippy_utils:: { comparisons, sext} ;
15
13
16
14
declare_clippy_lint ! {
17
15
/// ### What it does
@@ -39,53 +37,6 @@ declare_clippy_lint! {
39
37
40
38
declare_lint_pass ! ( InvalidUpcastComparisons => [ INVALID_UPCAST_COMPARISONS ] ) ;
41
39
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
-
89
40
fn numeric_cast_precast_bounds < ' a > ( cx : & LateContext < ' _ > , expr : & ' a Expr < ' _ > ) -> Option < ( FullInt , FullInt ) > {
90
41
if let ExprKind :: Cast ( cast_exp, _) = expr. kind {
91
42
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<'_>) ->
118
69
}
119
70
}
120
71
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
-
134
72
fn err_upcast_comparison ( cx : & LateContext < ' _ > , span : Span , expr : & Expr < ' _ > , always : bool ) {
135
73
if let ExprKind :: Cast ( cast_val, _) = expr. kind {
136
74
span_lint (
@@ -156,7 +94,7 @@ fn upcast_comparison_bounds_err<'tcx>(
156
94
invert : bool ,
157
95
) {
158
96
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) {
160
98
if rel == Rel :: Eq || rel == Rel :: Ne {
161
99
if norm_rhs_val < lb || norm_rhs_val > ub {
162
100
err_upcast_comparison ( cx, span, lhs, rel == Rel :: Ne ) ;
0 commit comments