@@ -56,20 +56,14 @@ impl Display for ShiftDirection {
56
56
}
57
57
}
58
58
59
- fn parse_shift < ' tcx > (
60
- cx : & LateContext < ' tcx > ,
61
- expr : & ' tcx Expr < ' tcx > ,
62
- ) -> Option < ( ShiftDirection , u128 , & ' tcx Expr < ' tcx > ) > {
59
+ fn parse_shift < ' tcx > ( expr : & ' tcx Expr < ' tcx > ) -> Option < ( ShiftDirection , & ' tcx Expr < ' tcx > , & ' tcx Expr < ' tcx > ) > {
63
60
if let ExprKind :: Binary ( op, l, r) = expr. kind {
64
61
let dir = match op. node {
65
62
BinOpKind :: Shl => ShiftDirection :: Left ,
66
63
BinOpKind :: Shr => ShiftDirection :: Right ,
67
64
_ => return None ,
68
65
} ;
69
- let const_expr = ConstEvalCtxt :: new ( cx) . eval ( r) ?;
70
- if let Constant :: Int ( shift) = const_expr {
71
- return Some ( ( dir, shift, l) ) ;
72
- }
66
+ return Some ( ( dir, l, r) ) ;
73
67
}
74
68
None
75
69
}
@@ -78,33 +72,39 @@ impl LateLintPass<'_> for ManualRotate {
78
72
fn check_expr < ' tcx > ( & mut self , cx : & LateContext < ' tcx > , expr : & Expr < ' tcx > ) {
79
73
if let ExprKind :: Binary ( op, l, r) = expr. kind
80
74
&& let BinOpKind :: Add | BinOpKind :: BitOr = op. node
81
- && let Some ( ( l_shift_dir, l_amount , l_expr ) ) = parse_shift ( cx , l)
82
- && let Some ( ( r_shift_dir, r_amount , r_expr ) ) = parse_shift ( cx , r)
75
+ && let Some ( ( l_shift_dir, l_expr , l_amount ) ) = parse_shift ( l)
76
+ && let Some ( ( r_shift_dir, r_expr , r_amount ) ) = parse_shift ( r)
83
77
&& l_shift_dir != r_shift_dir
84
78
&& clippy_utils:: eq_expr_value ( cx, l_expr, r_expr)
85
79
&& let Some ( bit_width) = match cx. typeck_results ( ) . expr_ty ( expr) . kind ( ) {
86
80
ty:: Int ( itype) => itype. bit_width ( ) ,
87
81
ty:: Uint ( itype) => itype. bit_width ( ) ,
88
82
_ => return ,
89
83
}
90
- && l_amount + r_amount == u128:: from ( bit_width)
91
84
{
92
- let ( shift_function, amount) = if l_amount < r_amount {
93
- ( l_shift_dir, l_amount)
94
- } else {
95
- ( r_shift_dir, r_amount)
96
- } ;
97
- let mut applicability = Applicability :: MachineApplicable ;
98
- let expr_sugg = sugg:: Sugg :: hir_with_applicability ( cx, l_expr, "_" , & mut applicability) . maybe_paren ( ) ;
99
- span_lint_and_sugg (
100
- cx,
101
- MANUAL_ROTATE ,
102
- expr. span ,
103
- "there is no need to manually implement bit rotation" ,
104
- "this expression can be rewritten as" ,
105
- format ! ( "{expr_sugg}.{shift_function}({amount})" ) ,
106
- Applicability :: MachineApplicable ,
107
- ) ;
85
+ let const_eval = ConstEvalCtxt :: new ( cx) ;
86
+
87
+ if let Some ( Constant :: Int ( l_amount) ) = const_eval. eval ( l_amount)
88
+ && let Some ( Constant :: Int ( r_amount) ) = const_eval. eval ( r_amount)
89
+ && l_amount + r_amount == u128:: from ( bit_width)
90
+ {
91
+ let ( shift_function, amount) = if l_amount < r_amount {
92
+ ( l_shift_dir, l_amount)
93
+ } else {
94
+ ( r_shift_dir, r_amount)
95
+ } ;
96
+ let mut applicability = Applicability :: MachineApplicable ;
97
+ let expr_sugg = sugg:: Sugg :: hir_with_applicability ( cx, l_expr, "_" , & mut applicability) . maybe_paren ( ) ;
98
+ span_lint_and_sugg (
99
+ cx,
100
+ MANUAL_ROTATE ,
101
+ expr. span ,
102
+ "there is no need to manually implement bit rotation" ,
103
+ "this expression can be rewritten as" ,
104
+ format ! ( "{expr_sugg}.{shift_function}({amount})" ) ,
105
+ Applicability :: MachineApplicable ,
106
+ ) ;
107
+ }
108
108
}
109
109
}
110
110
}
0 commit comments