Skip to content

Commit fd956e6

Browse files
authored
Create a single ConstEvalCtxt in expr_eagerness (#17228)
Most of changes here are to remove the dependence on passing `LateContext` changelog: none
2 parents 70d77f5 + aac7672 commit fd956e6

33 files changed

Lines changed: 204 additions & 202 deletions

clippy_lints/src/assertions_on_constants.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ impl<'tcx> LateLintPass<'tcx> for AssertionsOnConstants {
5252
_ => return,
5353
}
5454
&& let Some((condition, _)) = find_assert_args(cx, e, macro_call.expn)
55-
&& is_const_evaluatable(cx, condition)
55+
// Check if the whole expression can be moved into a const context.
56+
// Note that const eval can evaluate things which cannot be moved (e.g. `false && x`).
57+
&& is_const_evaluatable(cx.tcx, cx.typeck_results(), condition)
5658
&& let Some((Constant::Bool(assert_val), const_src)) =
5759
ConstEvalCtxt::new(cx).eval_with_source(condition, macro_call.span.ctxt())
5860
&& let in_const_context = is_inside_always_const_context(cx.tcx, e.hir_id)

clippy_lints/src/casts/needless_type_cast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ fn can_coerce_to_target_type(expr: &Expr<'_>) -> bool {
249249
fn check_binding_usages<'a>(cx: &LateContext<'a>, body: &Body<'a>, hir_id: HirId, binding_info: &BindingInfo<'a>) {
250250
let mut usages = Vec::new();
251251

252-
for_each_expr(cx, body.value, |expr| {
252+
for_each_expr(cx.tcx, body.value, |expr| {
253253
if let ExprKind::Path(ref qpath) = expr.kind
254254
&& !expr.span.from_expansion()
255255
&& let Res::Local(id) = cx.qpath_res(qpath, expr.hir_id)

clippy_lints/src/cloned_ref_to_slice_refs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ impl<'tcx> LateLintPass<'tcx> for ClonedRefToSliceRefs<'_> {
8585
&& let Some(adjustment) = is_needless_clone_or_equivalent(cx, recv, path.ident.name, item.hir_id)
8686

8787
// check for immutability or purity
88-
&& (!is_mutable(cx, recv) || is_const_evaluatable(cx, recv))
88+
&& (!is_mutable(cx, recv) || is_const_evaluatable(cx.tcx, cx.typeck_results(), recv))
8989

9090
// get appropriate crate for `slice::from_ref`
9191
&& let Some(builtin_crate) = clippy_utils::std_or_core(cx)

clippy_lints/src/collection_is_never_read.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ fn has_no_read_access<'tcx, T: Visitable<'tcx>>(cx: &LateContext<'tcx>, id: HirI
7979
let mut has_read_access = false;
8080

8181
// Inspect all expressions and sub-expressions in the block.
82-
for_each_expr(cx, block, |expr| {
82+
for_each_expr(cx.tcx, block, |expr| {
8383
// Ignore expressions that are not simply `id`.
8484
if expr.res_local_id() != Some(id) {
8585
return ControlFlow::Continue(());

clippy_lints/src/doc/missing_headers.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ pub fn check(
9999
fn find_panic(cx: &LateContext<'_>, body_id: BodyId) -> Option<Span> {
100100
let mut panic_span = None;
101101
let typeck = cx.tcx.typeck_body(body_id);
102-
for_each_expr(cx, cx.tcx.hir_body(body_id), |expr| {
102+
for_each_expr(cx.tcx, cx.tcx.hir_body(body_id), |expr| {
103103
if is_inside_always_const_context(cx.tcx, expr.hir_id) {
104104
return ControlFlow::<!>::Continue(());
105105
}

clippy_lints/src/entry.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ fn is_any_expr_in_map_used<'tcx>(
604604
map: &'tcx Expr<'tcx>,
605605
expr: &'tcx Expr<'tcx>,
606606
) -> bool {
607-
for_each_expr(cx, map, |e| {
607+
for_each_expr(cx.tcx, map, |e| {
608608
if spanless_eq.eq_expr(ctxt, e, expr) {
609609
return ControlFlow::Break(());
610610
}

clippy_lints/src/functions/not_unsafe_ptr_arg_deref.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ fn check_raw_ptr<'tcx>(
5050

5151
if !raw_ptrs.is_empty() {
5252
let typeck = cx.tcx.typeck_body(body.id());
53-
let _: Option<!> = for_each_expr(cx, body.value, |e| {
53+
let _: Option<!> = for_each_expr(cx.tcx, body.value, |e| {
5454
match e.kind {
5555
hir::ExprKind::Call(f, args) if is_unsafe_fn(cx, typeck.expr_ty(f)) => {
5656
for arg in args {

clippy_lints/src/loops/char_indices_as_byte_indices.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, pat: &Pat<'_>, iterable: &Expr
4848
&& let PatKind::Binding(_, binding_id, ..) = pat.kind
4949
{
5050
// Destructured iterator element `(idx, _)`, look for uses of the binding
51-
for_each_expr(cx, body, |expr| {
51+
for_each_expr(cx.tcx, body, |expr| {
5252
if expr.res_local_id() == Some(binding_id) {
5353
check_index_usage(cx, expr, pat, enumerate_span, chars_span, chars_recv);
5454
}
5555
CONTINUE
5656
});
5757
} else if let PatKind::Binding(_, binding_id, ..) = pat.kind {
5858
// Bound as a tuple, look for `tup.0`
59-
for_each_expr(cx, body, |expr| {
59+
for_each_expr(cx.tcx, body, |expr| {
6060
if let ExprKind::Field(e, field) = expr.kind
6161
&& e.res_local_id() == Some(binding_id)
6262
&& field.name == sym::integer(0)

clippy_lints/src/manual_clamp.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -383,12 +383,16 @@ fn is_call_max_min_pattern<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>)
383383
&& let Some(inner_seg) = segment(cx, inner_fn)
384384
&& let Some(outer_seg) = segment(cx, outer_fn)
385385
{
386-
let (input, inner_arg) = match (is_const_evaluatable(cx, first), is_const_evaluatable(cx, second)) {
386+
let typeck = cx.typeck_results();
387+
let (input, inner_arg) = match (
388+
is_const_evaluatable(cx.tcx, typeck, first),
389+
is_const_evaluatable(cx.tcx, typeck, second),
390+
) {
387391
(true, false) => (second, first),
388392
(false, true) => (first, second),
389393
_ => return None,
390394
};
391-
let is_float = cx.typeck_results().expr_ty_adjusted(input).is_floating_point();
395+
let is_float = typeck.expr_ty_adjusted(input).is_floating_point();
392396
let (min, max) = match (inner_seg, outer_seg) {
393397
(FunctionType::CmpMin, FunctionType::CmpMax) => (outer_arg, inner_arg),
394398
(FunctionType::CmpMax, FunctionType::CmpMin) => (inner_arg, outer_arg),

clippy_lints/src/methods/chunks_exact_to_as_chunks.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub(super) fn check<'tcx>(
2525
return;
2626
}
2727

28-
if is_const_evaluatable(cx, arg) {
28+
if is_const_evaluatable(cx.tcx, cx.typeck_results(), arg) {
2929
if !msrv.meets(cx, msrvs::AS_CHUNKS) {
3030
return;
3131
}

0 commit comments

Comments
 (0)