Skip to content

Commit 36b2685

Browse files
committed
refactor default_numeric_fallback
We only need to store if the literal binding has an explicit type bound or not
1 parent e51e930 commit 36b2685

File tree

1 file changed

+18
-27
lines changed

1 file changed

+18
-27
lines changed

clippy_lints/src/default_numeric_fallback.rs

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ impl<'tcx> LateLintPass<'tcx> for DefaultNumericFallback {
6767

6868
struct NumericFallbackVisitor<'a, 'tcx> {
6969
/// Stack manages type bound of exprs. The top element holds current expr type.
70-
ty_bounds: Vec<TyBound<'tcx>>,
70+
ty_bounds: Vec<ExplicitTyBound>,
7171

7272
cx: &'a LateContext<'tcx>,
7373
}
@@ -76,9 +76,9 @@ impl<'a, 'tcx> NumericFallbackVisitor<'a, 'tcx> {
7676
fn new(cx: &'a LateContext<'tcx>, is_parent_const: bool) -> Self {
7777
Self {
7878
ty_bounds: vec![if is_parent_const {
79-
TyBound::Any
79+
ExplicitTyBound(true)
8080
} else {
81-
TyBound::Nothing
81+
ExplicitTyBound(false)
8282
}],
8383
cx,
8484
}
@@ -88,10 +88,10 @@ impl<'a, 'tcx> NumericFallbackVisitor<'a, 'tcx> {
8888
fn check_lit(&self, lit: &Lit, lit_ty: Ty<'tcx>, emit_hir_id: HirId) {
8989
if_chain! {
9090
if !in_external_macro(self.cx.sess(), lit.span);
91-
if let Some(ty_bound) = self.ty_bounds.last();
91+
if let Some(explicit_ty_bounds) = self.ty_bounds.last();
9292
if matches!(lit.node,
9393
LitKind::Int(_, LitIntType::Unsuffixed) | LitKind::Float(_, LitFloatType::Unsuffixed));
94-
if !ty_bound.is_numeric();
94+
if !explicit_ty_bounds.0;
9595
then {
9696
let (suffix, is_float) = match lit_ty.kind() {
9797
ty::Int(IntTy::I32) => ("i32", false),
@@ -132,7 +132,7 @@ impl<'a, 'tcx> Visitor<'tcx> for NumericFallbackVisitor<'a, 'tcx> {
132132
if let Some(fn_sig) = fn_sig_opt(self.cx, func.hir_id) {
133133
for (expr, bound) in iter::zip(*args, fn_sig.skip_binder().inputs()) {
134134
// Push found arg type, then visit arg.
135-
self.ty_bounds.push(TyBound::Ty(*bound));
135+
self.ty_bounds.push((*bound).into());
136136
self.visit_expr(expr);
137137
self.ty_bounds.pop();
138138
}
@@ -144,7 +144,7 @@ impl<'a, 'tcx> Visitor<'tcx> for NumericFallbackVisitor<'a, 'tcx> {
144144
if let Some(def_id) = self.cx.typeck_results().type_dependent_def_id(expr.hir_id) {
145145
let fn_sig = self.cx.tcx.fn_sig(def_id).skip_binder();
146146
for (expr, bound) in iter::zip(std::iter::once(*receiver).chain(args.iter()), fn_sig.inputs()) {
147-
self.ty_bounds.push(TyBound::Ty(*bound));
147+
self.ty_bounds.push((*bound).into());
148148
self.visit_expr(expr);
149149
self.ty_bounds.pop();
150150
}
@@ -178,7 +178,7 @@ impl<'a, 'tcx> Visitor<'tcx> for NumericFallbackVisitor<'a, 'tcx> {
178178

179179
// Visit base with no bound.
180180
if let Some(base) = base {
181-
self.ty_bounds.push(TyBound::Nothing);
181+
self.ty_bounds.push(ExplicitTyBound(false));
182182
self.visit_expr(base);
183183
self.ty_bounds.pop();
184184
}
@@ -201,9 +201,10 @@ impl<'a, 'tcx> Visitor<'tcx> for NumericFallbackVisitor<'a, 'tcx> {
201201

202202
fn visit_stmt(&mut self, stmt: &'tcx Stmt<'_>) {
203203
match stmt.kind {
204-
StmtKind::Local(local) if local.ty.is_some() => self.ty_bounds.push(TyBound::Any),
204+
// we cannot check the exact type since it's a hir::Ty which does not implement `is_numeric`
205+
StmtKind::Local(local) => self.ty_bounds.push(ExplicitTyBound(local.ty.is_some())),
205206

206-
_ => self.ty_bounds.push(TyBound::Nothing),
207+
_ => self.ty_bounds.push(ExplicitTyBound(false)),
207208
}
208209

209210
walk_stmt(self, stmt);
@@ -221,28 +222,18 @@ fn fn_sig_opt<'tcx>(cx: &LateContext<'tcx>, hir_id: HirId) -> Option<PolyFnSig<'
221222
}
222223
}
223224

225+
/// Wrapper around a `bool` to make the meaning of the value clearer
224226
#[derive(Debug, Clone, Copy)]
225-
enum TyBound<'tcx> {
226-
Any,
227-
Ty(Ty<'tcx>),
228-
Nothing,
229-
}
227+
struct ExplicitTyBound(pub bool);
230228

231-
impl<'tcx> TyBound<'tcx> {
232-
fn is_numeric(self) -> bool {
233-
match self {
234-
TyBound::Any => true,
235-
TyBound::Ty(t) => t.is_numeric(),
236-
TyBound::Nothing => false,
237-
}
229+
impl<'tcx> From<Ty<'tcx>> for ExplicitTyBound {
230+
fn from(v: Ty<'tcx>) -> Self {
231+
Self(v.is_numeric())
238232
}
239233
}
240234

241-
impl<'tcx> From<Option<Ty<'tcx>>> for TyBound<'tcx> {
235+
impl<'tcx> From<Option<Ty<'tcx>>> for ExplicitTyBound {
242236
fn from(v: Option<Ty<'tcx>>) -> Self {
243-
match v {
244-
Some(t) => TyBound::Ty(t),
245-
None => TyBound::Nothing,
246-
}
237+
Self(v.map_or(false, Ty::is_numeric))
247238
}
248239
}

0 commit comments

Comments
 (0)