Skip to content

Commit

Permalink
Use try_eval_usize over eval_usize
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnTitor committed Mar 2, 2020
1 parent 8b7f7e6 commit 124b948
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
8 changes: 7 additions & 1 deletion clippy_lints/src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,13 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
ExprKind::Tup(ref tup) => self.multi(tup).map(Constant::Tuple),
ExprKind::Repeat(ref value, _) => {
let n = match self.tables.expr_ty(e).kind {
ty::Array(_, n) => n.eval_usize(self.lcx.tcx, self.lcx.param_env),
ty::Array(_, n) => {
if let Some(n) = n.try_eval_usize(self.lcx.tcx, self.lcx.param_env) {
n
} else {
return None;
}
},
_ => span_bug!(e.span, "typeck error"),
};
self.expr(value).map(|v| Constant::Repeat(Box::new(v), n))
Expand Down
6 changes: 5 additions & 1 deletion clippy_lints/src/indexing_slicing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,11 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IndexingSlicing {
if let Some(range) = higher::range(cx, index) {
// Ranged indexes, i.e., &x[n..m], &x[n..], &x[..n] and &x[..]
if let ty::Array(_, s) = ty.kind {
let size: u128 = s.eval_usize(cx.tcx, cx.param_env).into();
let size: u128 = if let Some(size) = s.try_eval_usize(cx.tcx, cx.param_env) {
size.into()
} else {
return;
};

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

Expand Down
8 changes: 7 additions & 1 deletion clippy_lints/src/methods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2324,7 +2324,13 @@ fn derefs_to_slice<'a, 'tcx>(
ty::Slice(_) => true,
ty::Adt(def, _) if def.is_box() => may_slice(cx, ty.boxed_ty()),
ty::Adt(..) => is_type_diagnostic_item(cx, ty, Symbol::intern("vec_type")),
ty::Array(_, size) => size.eval_usize(cx.tcx, cx.param_env) < 32,
ty::Array(_, size) => {
if let Some(size) = size.try_eval_usize(cx.tcx, cx.param_env) {
size < 32
} else {
false
}
},
ty::Ref(_, inner, _) => may_slice(cx, inner),
_ => false,
}
Expand Down

0 comments on commit 124b948

Please sign in to comment.