Skip to content

Commit e0ce9f0

Browse files
committed
Const eval changes:
* Remove `CoreConstant`. * Treat most constants from core as though they were inlined. * Don't evaluate `is_empty` for named constants.
1 parent 7fa85aa commit e0ce9f0

17 files changed

+392
-391
lines changed

clippy_lints/src/enum_clike.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,8 @@ impl<'tcx> LateLintPass<'tcx> for UnportableVariant {
4343
if let Some(anon_const) = &var.disr_expr {
4444
let def_id = cx.tcx.hir_body_owner_def_id(anon_const.body);
4545
let mut ty = cx.tcx.type_of(def_id.to_def_id()).instantiate_identity();
46-
let constant = cx
47-
.tcx
48-
.const_eval_poly(def_id.to_def_id())
49-
.ok()
50-
.map(|val| rustc_middle::mir::Const::from_value(val, ty));
51-
if let Some(Constant::Int(val)) = constant.and_then(|c| mir_to_const(cx.tcx, c)) {
46+
let constant = cx.tcx.const_eval_poly(def_id.to_def_id()).ok();
47+
if let Some(Constant::Int(val)) = constant.and_then(|c| mir_to_const(cx.tcx, c, ty)) {
5248
if let ty::Adt(adt, _) = ty.kind()
5349
&& adt.is_enum()
5450
{

clippy_lints/src/floating_point_arithmetic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ fn check_ln1p(cx: &LateContext<'_>, expr: &Expr<'_>, receiver: &Expr<'_>) {
206206
// ranges [-16777215, 16777216) for type f32 as whole number floats outside
207207
// this range are lossy and ambiguous.
208208
#[expect(clippy::cast_possible_truncation)]
209-
fn get_integer_from_float_constant(value: &Constant<'_>) -> Option<i32> {
209+
fn get_integer_from_float_constant(value: &Constant) -> Option<i32> {
210210
match value {
211211
F32(num) if num.fract() == 0.0 => {
212212
if (-16_777_215.0..16_777_216.0).contains(num) {

clippy_lints/src/manual_float_methods.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ impl<'tcx> LateLintPass<'tcx> for ManualFloatMethods {
202202
}
203203
}
204204

205-
fn is_infinity(constant: &Constant<'_>) -> bool {
205+
fn is_infinity(constant: &Constant) -> bool {
206206
match constant {
207207
// FIXME(f16_f128): add f16 and f128 when constants are available
208208
Constant::F32(float) => *float == f32::INFINITY,
@@ -211,7 +211,7 @@ fn is_infinity(constant: &Constant<'_>) -> bool {
211211
}
212212
}
213213

214-
fn is_neg_infinity(constant: &Constant<'_>) -> bool {
214+
fn is_neg_infinity(constant: &Constant) -> bool {
215215
match constant {
216216
// FIXME(f16_f128): add f16 and f128 when constants are available
217217
Constant::F32(float) => *float == f32::NEG_INFINITY,

clippy_lints/src/matches/overlapping_arms.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clippy_utils::consts::{ConstEvalCtxt, FullInt, mir_to_const};
1+
use clippy_utils::consts::{ConstEvalCtxt, Constant, FullInt};
22
use clippy_utils::diagnostics::span_lint_and_note;
33
use core::cmp::Ordering;
44
use rustc_hir::{Arm, Expr, PatKind, RangeEnd};
@@ -35,12 +35,12 @@ fn all_ranges<'tcx>(cx: &LateContext<'tcx>, arms: &'tcx [Arm<'_>], ty: Ty<'tcx>)
3535
let lhs_const = if let Some(lhs) = lhs {
3636
ConstEvalCtxt::new(cx).eval_pat_expr(lhs)?
3737
} else {
38-
mir_to_const(cx.tcx, ty.numeric_min_val(cx.tcx)?)?
38+
Constant::new_numeric_min(cx.tcx, ty)?
3939
};
4040
let rhs_const = if let Some(rhs) = rhs {
4141
ConstEvalCtxt::new(cx).eval_pat_expr(rhs)?
4242
} else {
43-
mir_to_const(cx.tcx, ty.numeric_max_val(cx.tcx)?)?
43+
Constant::new_numeric_max(cx.tcx, ty)?
4444
};
4545
let lhs_val = lhs_const.int_value(cx.tcx, ty)?;
4646
let rhs_val = rhs_const.int_value(cx.tcx, ty)?;

clippy_lints/src/methods/unnecessary_min_or_max.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::cmp::Ordering;
22

33
use super::UNNECESSARY_MIN_OR_MAX;
4-
use clippy_utils::consts::{ConstEvalCtxt, Constant, ConstantSource, FullInt};
4+
use clippy_utils::consts::{ConstEvalCtxt, Constant, FullInt};
55
use clippy_utils::diagnostics::span_lint_and_sugg;
66
use clippy_utils::source::snippet;
77

@@ -26,8 +26,8 @@ pub(super) fn check<'tcx>(
2626
&& matches!(fn_name, sym::cmp_ord_min | sym::cmp_ord_max)
2727
{
2828
let ctxt = expr.span.ctxt();
29-
if let Some((left, ConstantSource::Local | ConstantSource::CoreConstant)) = ecx.eval_with_source(recv, ctxt)
30-
&& let Some((right, ConstantSource::Local | ConstantSource::CoreConstant)) = ecx.eval_with_source(arg, ctxt)
29+
if let Some(left) = ecx.eval_local(recv, ctxt)
30+
&& let Some(right) = ecx.eval_local(arg, ctxt)
3131
{
3232
let Some(ord) = Constant::partial_cmp(cx.tcx, typeck_results.expr_ty(recv), &left, &right) else {
3333
return;

clippy_lints/src/minmax.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ enum MinMax {
6161
Max,
6262
}
6363

64-
fn min_max<'a, 'tcx>(cx: &LateContext<'tcx>, expr: &'a Expr<'a>) -> Option<(MinMax, Constant<'tcx>, &'a Expr<'a>)> {
64+
fn min_max<'a, 'tcx>(cx: &LateContext<'tcx>, expr: &'a Expr<'a>) -> Option<(MinMax, Constant, &'a Expr<'a>)> {
6565
match expr.kind {
6666
ExprKind::Call(path, args) => {
6767
if let ExprKind::Path(ref qpath) = path.kind {
@@ -98,7 +98,7 @@ fn fetch_const<'a, 'tcx>(
9898
receiver: Option<&'a Expr<'a>>,
9999
args: &'a [Expr<'a>],
100100
m: MinMax,
101-
) -> Option<(MinMax, Constant<'tcx>, &'a Expr<'a>)> {
101+
) -> Option<(MinMax, Constant, &'a Expr<'a>)> {
102102
let mut args = receiver.into_iter().chain(args);
103103
let first_arg = args.next()?;
104104
let second_arg = args.next()?;

clippy_lints/src/operators/const_comparisons.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ fn comparison_to_const<'tcx>(
2222
cx: &LateContext<'tcx>,
2323
typeck: &'tcx TypeckResults<'tcx>,
2424
expr: &'tcx Expr<'tcx>,
25-
) -> Option<(CmpOp, &'tcx Expr<'tcx>, &'tcx Expr<'tcx>, Constant<'tcx>, Ty<'tcx>)> {
25+
) -> Option<(CmpOp, &'tcx Expr<'tcx>, &'tcx Expr<'tcx>, Constant, Ty<'tcx>)> {
2626
if let ExprKind::Binary(operator, left, right) = expr.kind
2727
&& let Ok(cmp_op) = CmpOp::try_from(operator.node)
2828
{

clippy_lints/src/operators/float_cmp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ fn get_lint_and_message(is_local: bool, is_comparing_arrays: bool) -> (&'static
8585
}
8686
}
8787

88-
fn is_allowed(val: &Constant<'_>) -> bool {
88+
fn is_allowed(val: &Constant) -> bool {
8989
match val {
9090
// FIXME(f16_f128): add when equality check is available on all platforms
9191
&Constant::F32(f) => f == 0.0 || f.is_infinite(),

clippy_lints/src/ranges.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -299,8 +299,8 @@ fn check_possible_range_contains(
299299
}
300300
}
301301

302-
struct RangeBounds<'a, 'tcx> {
303-
val: Constant<'tcx>,
302+
struct RangeBounds<'a> {
303+
val: Constant,
304304
expr: &'a Expr<'a>,
305305
id: HirId,
306306
name_span: Span,
@@ -312,7 +312,7 @@ struct RangeBounds<'a, 'tcx> {
312312
// Takes a binary expression such as x <= 2 as input
313313
// Breaks apart into various pieces, such as the value of the number,
314314
// hir id of the variable, and direction/inclusiveness of the operator
315-
fn check_range_bounds<'a, 'tcx>(cx: &'a LateContext<'tcx>, ex: &'a Expr<'_>) -> Option<RangeBounds<'a, 'tcx>> {
315+
fn check_range_bounds<'a, 'tcx>(cx: &'a LateContext<'tcx>, ex: &'a Expr<'_>) -> Option<RangeBounds<'a>> {
316316
if let ExprKind::Binary(ref op, l, r) = ex.kind {
317317
let (inclusive, ordering) = match op.node {
318318
BinOpKind::Gt => (false, Ordering::Greater),

0 commit comments

Comments
 (0)