@@ -9,6 +9,7 @@ use rustc_ast as ast;
9
9
use rustc_data_structures:: fx:: FxHashSet ;
10
10
use rustc_hir as hir;
11
11
use rustc_lint:: { LateContext , LateLintPass } ;
12
+ use rustc_middle:: ty:: Ty ;
12
13
use rustc_session:: impl_lint_pass;
13
14
use rustc_span:: source_map:: { Span , Spanned } ;
14
15
@@ -67,20 +68,14 @@ impl ArithmeticSideEffects {
67
68
}
68
69
69
70
/// Checks if the given `expr` has any of the inner `allowed` elements.
70
- fn is_allowed_ty ( & self , cx : & LateContext < ' _ > , expr : & hir:: Expr < ' _ > ) -> bool {
71
- self . allowed . contains (
72
- cx. typeck_results ( )
73
- . expr_ty ( expr)
74
- . to_string ( )
75
- . split ( '<' )
76
- . next ( )
77
- . unwrap_or_default ( ) ,
78
- )
71
+ fn is_allowed_ty ( & self , ty : Ty < ' _ > ) -> bool {
72
+ self . allowed
73
+ . contains ( ty. to_string ( ) . split ( '<' ) . next ( ) . unwrap_or_default ( ) )
79
74
}
80
75
81
76
// For example, 8i32 or &i64::MAX.
82
- fn is_integral < ' expr , ' tcx > ( cx : & LateContext < ' tcx > , expr : & ' expr hir :: Expr < ' tcx > ) -> bool {
83
- cx . typeck_results ( ) . expr_ty ( expr ) . peel_refs ( ) . is_integral ( )
77
+ fn is_integral ( ty : Ty < ' _ > ) -> bool {
78
+ ty . peel_refs ( ) . is_integral ( )
84
79
}
85
80
86
81
// Common entry-point to avoid code duplication.
@@ -129,10 +124,13 @@ impl ArithmeticSideEffects {
129
124
) {
130
125
return ;
131
126
} ;
132
- if self . is_allowed_ty ( cx, lhs) && self . is_allowed_ty ( cx, rhs) {
127
+ let lhs_ty = cx. typeck_results ( ) . expr_ty ( lhs) ;
128
+ let rhs_ty = cx. typeck_results ( ) . expr_ty ( rhs) ;
129
+ let lhs_and_rhs_have_the_same_ty = lhs_ty == rhs_ty;
130
+ if lhs_and_rhs_have_the_same_ty && self . is_allowed_ty ( lhs_ty) && self . is_allowed_ty ( rhs_ty) {
133
131
return ;
134
132
}
135
- let has_valid_op = if Self :: is_integral ( cx , lhs ) && Self :: is_integral ( cx , rhs ) {
133
+ let has_valid_op = if Self :: is_integral ( lhs_ty ) && Self :: is_integral ( rhs_ty ) {
136
134
match ( Self :: literal_integer ( lhs) , Self :: literal_integer ( rhs) ) {
137
135
( None , None ) => false ,
138
136
( None , Some ( local_expr) ) => Self :: has_valid_op ( op, local_expr) ,
0 commit comments