@@ -11,7 +11,10 @@ use rustc_hir as hir;
11
11
use rustc_lint:: { LateContext , LateLintPass } ;
12
12
use rustc_middle:: ty:: Ty ;
13
13
use rustc_session:: impl_lint_pass;
14
- use rustc_span:: source_map:: { Span , Spanned } ;
14
+ use rustc_span:: {
15
+ source_map:: { Span , Spanned } ,
16
+ Symbol ,
17
+ } ;
15
18
16
19
const HARD_CODED_ALLOWED_BINARY : & [ [ & str ; 2 ] ] = & [
17
20
[ "f32" , "f32" ] ,
@@ -21,6 +24,7 @@ const HARD_CODED_ALLOWED_BINARY: &[[&str; 2]] = &[
21
24
[ "std::string::String" , "&str" ] ,
22
25
] ;
23
26
const HARD_CODED_ALLOWED_UNARY : & [ & str ] = & [ "f32" , "f64" , "std::num::Saturating" , "std::num::Wrapping" ] ;
27
+ const INTEGER_METHODS : & [ & str ] = & [ "saturating_div" , "wrapping_div" , "wrapping_rem" , "wrapping_rem_euclid" ] ;
24
28
25
29
#[ derive( Debug ) ]
26
30
pub struct ArithmeticSideEffects {
@@ -29,6 +33,7 @@ pub struct ArithmeticSideEffects {
29
33
// Used to check whether expressions are constants, such as in enum discriminants and consts
30
34
const_span : Option < Span > ,
31
35
expr_span : Option < Span > ,
36
+ integer_methods : FxHashSet < Symbol > ,
32
37
}
33
38
34
39
impl_lint_pass ! ( ArithmeticSideEffects => [ ARITHMETIC_SIDE_EFFECTS ] ) ;
@@ -54,6 +59,7 @@ impl ArithmeticSideEffects {
54
59
allowed_unary,
55
60
const_span : None ,
56
61
expr_span : None ,
62
+ integer_methods : INTEGER_METHODS . iter ( ) . map ( |el| Symbol :: intern ( el) ) . collect ( ) ,
57
63
}
58
64
}
59
65
@@ -194,7 +200,6 @@ impl ArithmeticSideEffects {
194
200
ps : & hir:: PathSegment < ' tcx > ,
195
201
receiver : & hir:: Expr < ' tcx > ,
196
202
) {
197
- const METHODS : & [ & str ] = & [ "saturating_div" , "wrapping_div" , "wrapping_rem" , "wrapping_rem_euclid" ] ;
198
203
let Some ( arg) = args. first ( ) else { return ; } ;
199
204
if constant_simple ( cx, cx. typeck_results ( ) , receiver) . is_some ( ) {
200
205
return ;
@@ -203,7 +208,7 @@ impl ArithmeticSideEffects {
203
208
if !Self :: is_integral ( instance_ty) {
204
209
return ;
205
210
}
206
- if METHODS . iter ( ) . copied ( ) . all ( |method| method != ps. ident . as_str ( ) ) {
211
+ if ! self . integer_methods . contains ( & ps. ident . name ) {
207
212
return ;
208
213
}
209
214
let ( actual_arg, _) = peel_hir_expr_refs ( arg) ;
0 commit comments