@@ -9,6 +9,7 @@ use rustc_ast as ast;
99use rustc_data_structures:: fx:: FxHashSet ;
1010use rustc_hir as hir;
1111use rustc_lint:: { LateContext , LateLintPass } ;
12+ use rustc_middle:: ty:: Ty ;
1213use rustc_session:: impl_lint_pass;
1314use rustc_span:: source_map:: { Span , Spanned } ;
1415
@@ -67,20 +68,14 @@ impl ArithmeticSideEffects {
6768 }
6869
6970 /// 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 ( ) )
7974 }
8075
8176 // 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 ( )
8479 }
8580
8681 // Common entry-point to avoid code duplication.
@@ -129,10 +124,13 @@ impl ArithmeticSideEffects {
129124 ) {
130125 return ;
131126 } ;
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) {
133131 return ;
134132 }
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 ) {
136134 match ( Self :: literal_integer ( lhs) , Self :: literal_integer ( rhs) ) {
137135 ( None , None ) => false ,
138136 ( None , Some ( local_expr) ) => Self :: has_valid_op ( op, local_expr) ,
0 commit comments