1
+ use super :: { FLOAT_ARITHMETIC , INTEGER_ARITHMETIC } ;
1
2
use clippy_utils:: consts:: constant_simple;
2
3
use clippy_utils:: diagnostics:: span_lint;
4
+ use clippy_utils:: is_from_proc_macro;
3
5
use clippy_utils:: is_integer_literal;
4
6
use rustc_hir as hir;
5
7
use rustc_lint:: LateContext ;
6
8
use rustc_span:: source_map:: Span ;
7
9
8
- use super :: { FLOAT_ARITHMETIC , INTEGER_ARITHMETIC } ;
9
-
10
10
#[ derive( Default ) ]
11
11
pub struct Context {
12
12
expr_id : Option < hir:: HirId > ,
@@ -19,6 +19,10 @@ impl Context {
19
19
self . expr_id . is_some ( ) || self . const_span . map_or ( false , |span| span. contains ( e. span ) )
20
20
}
21
21
22
+ fn skip_expr_involving_integers < ' tcx > ( cx : & LateContext < ' tcx > , e : & hir:: Expr < ' tcx > ) -> bool {
23
+ is_from_proc_macro ( cx, e)
24
+ }
25
+
22
26
pub fn check_binary < ' tcx > (
23
27
& mut self ,
24
28
cx : & LateContext < ' tcx > ,
@@ -47,6 +51,9 @@ impl Context {
47
51
48
52
let ( l_ty, r_ty) = ( cx. typeck_results ( ) . expr_ty ( l) , cx. typeck_results ( ) . expr_ty ( r) ) ;
49
53
if l_ty. peel_refs ( ) . is_integral ( ) && r_ty. peel_refs ( ) . is_integral ( ) {
54
+ if Self :: skip_expr_involving_integers ( cx, expr) {
55
+ return ;
56
+ }
50
57
match op {
51
58
hir:: BinOpKind :: Div | hir:: BinOpKind :: Rem => match & r. kind {
52
59
hir:: ExprKind :: Lit ( _lit) => ( ) ,
@@ -79,6 +86,9 @@ impl Context {
79
86
let ty = cx. typeck_results ( ) . expr_ty ( arg) ;
80
87
if constant_simple ( cx, cx. typeck_results ( ) , expr) . is_none ( ) {
81
88
if ty. is_integral ( ) {
89
+ if Self :: skip_expr_involving_integers ( cx, expr) {
90
+ return ;
91
+ }
82
92
span_lint ( cx, INTEGER_ARITHMETIC , expr. span , "integer arithmetic detected" ) ;
83
93
self . expr_id = Some ( expr. hir_id ) ;
84
94
} else if ty. is_floating_point ( ) {
0 commit comments