Skip to content

Commit 3e24d50

Browse files
committed
Rename eval_simple to eval_local and take the SyntaxContext as an argument.
1 parent f084864 commit 3e24d50

21 files changed

+134
-82
lines changed

clippy_lints/src/assertions_on_constants.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ impl<'tcx> LateLintPass<'tcx> for AssertionsOnConstants {
5050
_ => return,
5151
}
5252
&& let Some((condition, _)) = find_assert_args(cx, e, macro_call.expn)
53-
&& let Some((Constant::Bool(assert_val), const_src)) = ConstEvalCtxt::new(cx).eval_with_source(condition)
53+
&& let Some((Constant::Bool(assert_val), const_src)) =
54+
ConstEvalCtxt::new(cx).eval_with_source(condition, macro_call.span.ctxt())
5455
&& let in_const_context = is_inside_always_const_context(cx.tcx, e.hir_id)
5556
&& (const_src.is_local() || !in_const_context)
5657
&& !(is_debug && as_bool_lit(condition) == Some(false))

clippy_lints/src/floating_point_arithmetic.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use rustc_hir::{BinOpKind, Expr, ExprKind, PathSegment, UnOp};
1111
use rustc_lint::{LateContext, LateLintPass};
1212
use rustc_middle::ty;
1313
use rustc_session::declare_lint_pass;
14+
use rustc_span::SyntaxContext;
1415
use rustc_span::source_map::Spanned;
1516
use std::f32::consts as f32_consts;
1617
use std::f64::consts as f64_consts;
@@ -517,8 +518,8 @@ fn check_mul_add(cx: &LateContext<'_>, expr: &Expr<'_>) {
517518
fn is_testing_positive(cx: &LateContext<'_>, expr: &Expr<'_>, test: &Expr<'_>) -> bool {
518519
if let ExprKind::Binary(Spanned { node: op, .. }, left, right) = expr.kind {
519520
match op {
520-
BinOpKind::Gt | BinOpKind::Ge => is_zero(cx, right) && eq_expr_value(cx, left, test),
521-
BinOpKind::Lt | BinOpKind::Le => is_zero(cx, left) && eq_expr_value(cx, right, test),
521+
BinOpKind::Gt | BinOpKind::Ge => is_zero(cx, right, expr.span.ctxt()) && eq_expr_value(cx, left, test),
522+
BinOpKind::Lt | BinOpKind::Le => is_zero(cx, left, expr.span.ctxt()) && eq_expr_value(cx, right, test),
522523
_ => false,
523524
}
524525
} else {
@@ -530,8 +531,8 @@ fn is_testing_positive(cx: &LateContext<'_>, expr: &Expr<'_>, test: &Expr<'_>) -
530531
fn is_testing_negative(cx: &LateContext<'_>, expr: &Expr<'_>, test: &Expr<'_>) -> bool {
531532
if let ExprKind::Binary(Spanned { node: op, .. }, left, right) = expr.kind {
532533
match op {
533-
BinOpKind::Gt | BinOpKind::Ge => is_zero(cx, left) && eq_expr_value(cx, right, test),
534-
BinOpKind::Lt | BinOpKind::Le => is_zero(cx, right) && eq_expr_value(cx, left, test),
534+
BinOpKind::Gt | BinOpKind::Ge => is_zero(cx, left, expr.span.ctxt()) && eq_expr_value(cx, right, test),
535+
BinOpKind::Lt | BinOpKind::Le => is_zero(cx, right, expr.span.ctxt()) && eq_expr_value(cx, left, test),
535536
_ => false,
536537
}
537538
} else {
@@ -540,8 +541,8 @@ fn is_testing_negative(cx: &LateContext<'_>, expr: &Expr<'_>, test: &Expr<'_>) -
540541
}
541542

542543
/// Returns true iff expr is some zero literal
543-
fn is_zero(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
544-
match ConstEvalCtxt::new(cx).eval_simple(expr) {
544+
fn is_zero(cx: &LateContext<'_>, expr: &Expr<'_>, ctxt: SyntaxContext) -> bool {
545+
match ConstEvalCtxt::new(cx).eval_local(expr, ctxt) {
545546
Some(Int(i)) => i == 0,
546547
Some(F32(f)) => f == 0.0,
547548
Some(F64(f)) => f == 0.0,

clippy_lints/src/if_not_else.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,14 @@ impl LateLintPass<'_> for IfNotElse {
6060
),
6161
// Don't lint on `… != 0`, as these are likely to be bit tests.
6262
// For example, `if foo & 0x0F00 != 0 { … } else { … }` is already in the "proper" order.
63-
ExprKind::Binary(op, _, rhs) if op.node == BinOpKind::Ne && !is_zero_integer_const(cx, rhs) => (
64-
"unnecessary `!=` operation",
65-
"change to `==` and swap the blocks of the `if`/`else`",
66-
),
63+
ExprKind::Binary(op, _, rhs)
64+
if op.node == BinOpKind::Ne && !is_zero_integer_const(cx, rhs, e.span.ctxt()) =>
65+
{
66+
(
67+
"unnecessary `!=` operation",
68+
"change to `==` and swap the blocks of the `if`/`else`",
69+
)
70+
},
6771
_ => return,
6872
};
6973

clippy_lints/src/invalid_upcast_comparisons.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ fn upcast_comparison_bounds_err<'tcx>(
101101
invert: bool,
102102
) {
103103
if let Some((lb, ub)) = lhs_bounds
104-
&& let Some(norm_rhs_val) = ConstEvalCtxt::new(cx).eval_full_int(rhs)
104+
&& let Some(norm_rhs_val) = ConstEvalCtxt::new(cx).eval_full_int(rhs, span.ctxt())
105105
{
106106
if rel == Rel::Eq || rel == Rel::Ne {
107107
if norm_rhs_val < lb || norm_rhs_val > ub {

clippy_lints/src/manual_rem_euclid.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use rustc_errors::Applicability;
88
use rustc_hir::{BinOpKind, Expr, ExprKind, Node, TyKind};
99
use rustc_lint::{LateContext, LateLintPass, LintContext};
1010
use rustc_session::impl_lint_pass;
11+
use rustc_span::SyntaxContext;
1112

1213
declare_clippy_lint! {
1314
/// ### What it does
@@ -58,13 +59,13 @@ impl<'tcx> LateLintPass<'tcx> for ManualRemEuclid {
5859
&& add_lhs.span.ctxt() == ctxt
5960
&& add_rhs.span.ctxt() == ctxt
6061
&& !expr.span.in_external_macro(cx.sess().source_map())
61-
&& let Some(const1) = check_for_unsigned_int_constant(cx, rem_rhs)
62-
&& let Some((const2, add_other)) = check_for_either_unsigned_int_constant(cx, add_lhs, add_rhs)
62+
&& let Some(const1) = check_for_unsigned_int_constant(cx, ctxt, rem_rhs)
63+
&& let Some((const2, add_other)) = check_for_either_unsigned_int_constant(cx, ctxt, add_lhs, add_rhs)
6364
&& let ExprKind::Binary(rem2_op, rem2_lhs, rem2_rhs) = add_other.kind
6465
&& rem2_op.node == BinOpKind::Rem
6566
&& const1 == const2
6667
&& let Some(hir_id) = path_to_local(rem2_lhs)
67-
&& let Some(const3) = check_for_unsigned_int_constant(cx, rem2_rhs)
68+
&& let Some(const3) = check_for_unsigned_int_constant(cx, ctxt, rem2_rhs)
6869
// Also ensures the const is nonzero since zero can't be a divisor
6970
&& const2 == const3
7071
&& rem2_lhs.span.ctxt() == ctxt
@@ -103,16 +104,21 @@ impl<'tcx> LateLintPass<'tcx> for ManualRemEuclid {
103104
// constant along with the other expression unchanged if so
104105
fn check_for_either_unsigned_int_constant<'a>(
105106
cx: &'a LateContext<'_>,
107+
ctxt: SyntaxContext,
106108
left: &'a Expr<'_>,
107109
right: &'a Expr<'_>,
108110
) -> Option<(u128, &'a Expr<'a>)> {
109-
check_for_unsigned_int_constant(cx, left)
111+
check_for_unsigned_int_constant(cx, ctxt, left)
110112
.map(|int_const| (int_const, right))
111-
.or_else(|| check_for_unsigned_int_constant(cx, right).map(|int_const| (int_const, left)))
113+
.or_else(|| check_for_unsigned_int_constant(cx, ctxt, right).map(|int_const| (int_const, left)))
112114
}
113115

114-
fn check_for_unsigned_int_constant<'a>(cx: &'a LateContext<'_>, expr: &'a Expr<'_>) -> Option<u128> {
115-
let int_const = ConstEvalCtxt::new(cx).eval_full_int(expr)?;
116+
fn check_for_unsigned_int_constant<'a>(
117+
cx: &'a LateContext<'_>,
118+
ctxt: SyntaxContext,
119+
expr: &'a Expr<'_>,
120+
) -> Option<u128> {
121+
let int_const = ConstEvalCtxt::new(cx).eval_full_int(expr, ctxt)?;
116122
match int_const {
117123
FullInt::S(s) => s.try_into().ok(),
118124
FullInt::U(u) => Some(u),

clippy_lints/src/matches/manual_unwrap_or.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ fn handle(
155155
&& cx.typeck_results().expr_adjustments(body_some).is_empty()
156156
&& let Some(or_body_snippet) = peel_blocks(body_none).span.get_source_text(cx)
157157
&& let Some(indent) = indent_of(cx, expr.span)
158-
&& ConstEvalCtxt::new(cx).eval_simple(body_none).is_some()
158+
&& ConstEvalCtxt::new(cx).eval_local(body_none, expr.span.ctxt()).is_some()
159159
{
160160
let reindented_or_body = reindent_multiline(&or_body_snippet, true, Some(indent));
161161
let mut app = Applicability::MachineApplicable;

clippy_lints/src/methods/is_digit_ascii_radix.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub(super) fn check<'tcx>(
1818
return;
1919
}
2020

21-
if let Some(radix_val) = ConstEvalCtxt::new(cx).eval_full_int(radix) {
21+
if let Some(radix_val) = ConstEvalCtxt::new(cx).eval_full_int(radix, expr.span.ctxt()) {
2222
let (num, replacement) = match radix_val {
2323
FullInt::S(10) | FullInt::U(10) => (10, "is_ascii_digit"),
2424
FullInt::S(16) | FullInt::U(16) => (16, "is_ascii_hexdigit"),

clippy_lints/src/methods/unnecessary_min_or_max.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ pub(super) fn check<'tcx>(
2525
&& let Some(fn_name) = cx.tcx.get_diagnostic_name(id)
2626
&& matches!(fn_name, sym::cmp_ord_min | sym::cmp_ord_max)
2727
{
28-
if let Some((left, ConstantSource::Local | ConstantSource::CoreConstant)) = ecx.eval_with_source(recv)
29-
&& let Some((right, ConstantSource::Local | ConstantSource::CoreConstant)) = ecx.eval_with_source(arg)
28+
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)
3031
{
3132
let Some(ord) = Constant::partial_cmp(cx.tcx, typeck_results.expr_ty(recv), &left, &right) else {
3233
return;

clippy_lints/src/minmax.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use clippy_utils::{is_trait_method, sym};
44
use rustc_hir::{Expr, ExprKind};
55
use rustc_lint::{LateContext, LateLintPass};
66
use rustc_session::declare_lint_pass;
7+
use rustc_span::SyntaxContext;
78
use std::cmp::Ordering::{Equal, Greater, Less};
89

910
declare_clippy_lint! {
@@ -68,8 +69,8 @@ fn min_max<'a, 'tcx>(cx: &LateContext<'tcx>, expr: &'a Expr<'a>) -> Option<(MinM
6869
.qpath_res(qpath, path.hir_id)
6970
.opt_def_id()
7071
.and_then(|def_id| match cx.tcx.get_diagnostic_name(def_id) {
71-
Some(sym::cmp_min) => fetch_const(cx, None, args, MinMax::Min),
72-
Some(sym::cmp_max) => fetch_const(cx, None, args, MinMax::Max),
72+
Some(sym::cmp_min) => fetch_const(cx, expr.span.ctxt(), None, args, MinMax::Min),
73+
Some(sym::cmp_max) => fetch_const(cx, expr.span.ctxt(), None, args, MinMax::Max),
7374
_ => None,
7475
})
7576
} else {
@@ -79,8 +80,8 @@ fn min_max<'a, 'tcx>(cx: &LateContext<'tcx>, expr: &'a Expr<'a>) -> Option<(MinM
7980
ExprKind::MethodCall(path, receiver, args @ [_], _) => {
8081
if cx.typeck_results().expr_ty(receiver).is_floating_point() || is_trait_method(cx, expr, sym::Ord) {
8182
match path.ident.name {
82-
sym::max => fetch_const(cx, Some(receiver), args, MinMax::Max),
83-
sym::min => fetch_const(cx, Some(receiver), args, MinMax::Min),
83+
sym::max => fetch_const(cx, expr.span.ctxt(), Some(receiver), args, MinMax::Max),
84+
sym::min => fetch_const(cx, expr.span.ctxt(), Some(receiver), args, MinMax::Min),
8485
_ => None,
8586
}
8687
} else {
@@ -93,6 +94,7 @@ fn min_max<'a, 'tcx>(cx: &LateContext<'tcx>, expr: &'a Expr<'a>) -> Option<(MinM
9394

9495
fn fetch_const<'a, 'tcx>(
9596
cx: &LateContext<'tcx>,
97+
ctxt: SyntaxContext,
9698
receiver: Option<&'a Expr<'a>>,
9799
args: &'a [Expr<'a>],
98100
m: MinMax,
@@ -104,7 +106,7 @@ fn fetch_const<'a, 'tcx>(
104106
return None;
105107
}
106108
let ecx = ConstEvalCtxt::new(cx);
107-
match (ecx.eval_simple(first_arg), ecx.eval_simple(second_arg)) {
109+
match (ecx.eval_local(first_arg, ctxt), ecx.eval_local(second_arg, ctxt)) {
108110
(Some(c), None) => Some((m, c, second_arg)),
109111
(None, Some(c)) => Some((m, c, first_arg)),
110112
// otherwise ignore

clippy_lints/src/operators/arithmetic_side_effects.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ impl ArithmeticSideEffects {
190190
lhs: &'tcx hir::Expr<'_>,
191191
rhs: &'tcx hir::Expr<'_>,
192192
) {
193-
if ConstEvalCtxt::new(cx).eval_simple(expr).is_some() {
193+
if ConstEvalCtxt::new(cx).eval_local(expr, expr.span.ctxt()).is_some() {
194194
return;
195195
}
196196
if !matches!(
@@ -283,7 +283,7 @@ impl ArithmeticSideEffects {
283283
let Some(arg) = args.first() else {
284284
return;
285285
};
286-
if ConstEvalCtxt::new(cx).eval_simple(receiver).is_some() {
286+
if ConstEvalCtxt::new(cx).eval_local(receiver, expr.span.ctxt()).is_some() {
287287
return;
288288
}
289289
let instance_ty = cx.typeck_results().expr_ty_adjusted(receiver);

0 commit comments

Comments
 (0)