Skip to content

Commit bc9892d

Browse files
committed
Auto merge of #10675 - c410-f3r:bbbbbbbbbbb, r=llogiq
[arithmetic_side_effects] Cache symbols An internal-only modification to speed up the processing of symbols because "intern" isn't very cheap, even more when you are doing the same thing for every method expression.
2 parents 06dace2 + 0b16f80 commit bc9892d

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

clippy_lints/src/operators/arithmetic_side_effects.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ use rustc_hir as hir;
1111
use rustc_lint::{LateContext, LateLintPass};
1212
use rustc_middle::ty::Ty;
1313
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+
};
1518

1619
const HARD_CODED_ALLOWED_BINARY: &[[&str; 2]] = &[
1720
["f32", "f32"],
@@ -21,6 +24,7 @@ const HARD_CODED_ALLOWED_BINARY: &[[&str; 2]] = &[
2124
["std::string::String", "&str"],
2225
];
2326
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"];
2428

2529
#[derive(Debug)]
2630
pub struct ArithmeticSideEffects {
@@ -29,6 +33,7 @@ pub struct ArithmeticSideEffects {
2933
// Used to check whether expressions are constants, such as in enum discriminants and consts
3034
const_span: Option<Span>,
3135
expr_span: Option<Span>,
36+
integer_methods: FxHashSet<Symbol>,
3237
}
3338

3439
impl_lint_pass!(ArithmeticSideEffects => [ARITHMETIC_SIDE_EFFECTS]);
@@ -54,6 +59,7 @@ impl ArithmeticSideEffects {
5459
allowed_unary,
5560
const_span: None,
5661
expr_span: None,
62+
integer_methods: INTEGER_METHODS.iter().map(|el| Symbol::intern(el)).collect(),
5763
}
5864
}
5965

@@ -194,7 +200,6 @@ impl ArithmeticSideEffects {
194200
ps: &hir::PathSegment<'tcx>,
195201
receiver: &hir::Expr<'tcx>,
196202
) {
197-
const METHODS: &[&str] = &["saturating_div", "wrapping_div", "wrapping_rem", "wrapping_rem_euclid"];
198203
let Some(arg) = args.first() else { return; };
199204
if constant_simple(cx, cx.typeck_results(), receiver).is_some() {
200205
return;
@@ -203,7 +208,7 @@ impl ArithmeticSideEffects {
203208
if !Self::is_integral(instance_ty) {
204209
return;
205210
}
206-
if METHODS.iter().copied().all(|method| method != ps.ident.as_str()) {
211+
if !self.integer_methods.contains(&ps.ident.name) {
207212
return;
208213
}
209214
let (actual_arg, _) = peel_hir_expr_refs(arg);

0 commit comments

Comments
 (0)