Skip to content

Commit 3d44ad2

Browse files
committed
Use try_eval_usize over eval_usize
1 parent b96c3ca commit 3d44ad2

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

clippy_lints/src/consts.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,13 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
231231
ExprKind::Tup(ref tup) => self.multi(tup).map(Constant::Tuple),
232232
ExprKind::Repeat(ref value, _) => {
233233
let n = match self.tables.expr_ty(e).kind {
234-
ty::Array(_, n) => n.eval_usize(self.lcx.tcx, self.lcx.param_env),
234+
ty::Array(_, n) => {
235+
if let Some(n) = n.try_eval_usize(self.lcx.tcx, self.lcx.param_env) {
236+
n
237+
} else {
238+
return None;
239+
}
240+
},
235241
_ => span_bug!(e.span, "typeck error"),
236242
};
237243
self.expr(value).map(|v| Constant::Repeat(Box::new(v), n))

clippy_lints/src/indexing_slicing.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,11 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IndexingSlicing {
9292
if let Some(range) = higher::range(cx, index) {
9393
// Ranged indexes, i.e., &x[n..m], &x[n..], &x[..n] and &x[..]
9494
if let ty::Array(_, s) = ty.kind {
95-
let size: u128 = s.eval_usize(cx.tcx, cx.param_env).into();
95+
let size: u128 = if let Some(size) = s.try_eval_usize(cx.tcx, cx.param_env) {
96+
size.into()
97+
} else {
98+
return;
99+
};
96100

97101
let const_range = to_const_range(cx, range, size);
98102

clippy_lints/src/methods/mod.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -2324,7 +2324,13 @@ fn derefs_to_slice<'a, 'tcx>(
23242324
ty::Slice(_) => true,
23252325
ty::Adt(def, _) if def.is_box() => may_slice(cx, ty.boxed_ty()),
23262326
ty::Adt(..) => is_type_diagnostic_item(cx, ty, Symbol::intern("vec_type")),
2327-
ty::Array(_, size) => size.eval_usize(cx.tcx, cx.param_env) < 32,
2327+
ty::Array(_, size) => {
2328+
if let Some(size) = size.try_eval_usize(cx.tcx, cx.param_env) {
2329+
size < 32
2330+
} else {
2331+
false
2332+
}
2333+
},
23282334
ty::Ref(_, inner, _) => may_slice(cx, inner),
23292335
_ => false,
23302336
}

0 commit comments

Comments
 (0)