@@ -11,6 +11,7 @@ use rustc_hir::{BinOpKind, Expr, ExprKind, PathSegment, UnOp};
11
11
use rustc_lint:: { LateContext , LateLintPass } ;
12
12
use rustc_middle:: ty;
13
13
use rustc_session:: declare_lint_pass;
14
+ use rustc_span:: SyntaxContext ;
14
15
use rustc_span:: source_map:: Spanned ;
15
16
use std:: f32:: consts as f32_consts;
16
17
use std:: f64:: consts as f64_consts;
@@ -110,8 +111,8 @@ declare_lint_pass!(FloatingPointArithmetic => [
110
111
111
112
// Returns the specialized log method for a given base if base is constant
112
113
// and is one of 2, 10 and e
113
- fn get_specialized_log_method ( cx : & LateContext < ' _ > , base : & Expr < ' _ > ) -> Option < & ' static str > {
114
- if let Some ( value) = ConstEvalCtxt :: new ( cx) . eval ( base) {
114
+ fn get_specialized_log_method ( cx : & LateContext < ' _ > , base : & Expr < ' _ > , ctxt : SyntaxContext ) -> Option < & ' static str > {
115
+ if let Some ( value) = ConstEvalCtxt :: new ( cx) . eval_local ( base, ctxt ) {
115
116
if F32 ( 2.0 ) == value || F64 ( 2.0 ) == value {
116
117
return Some ( "log2" ) ;
117
118
} else if F32 ( 10.0 ) == value || F64 ( 10.0 ) == value {
@@ -157,7 +158,7 @@ fn prepare_receiver_sugg<'a>(cx: &LateContext<'_>, mut expr: &'a Expr<'a>) -> Su
157
158
}
158
159
159
160
fn check_log_base ( cx : & LateContext < ' _ > , expr : & Expr < ' _ > , receiver : & Expr < ' _ > , args : & [ Expr < ' _ > ] ) {
160
- if let Some ( method) = get_specialized_log_method ( cx, & args[ 0 ] ) {
161
+ if let Some ( method) = get_specialized_log_method ( cx, & args[ 0 ] , expr . span . ctxt ( ) ) {
161
162
span_lint_and_sugg (
162
163
cx,
163
164
SUBOPTIMAL_FLOPS ,
@@ -205,7 +206,7 @@ fn check_ln1p(cx: &LateContext<'_>, expr: &Expr<'_>, receiver: &Expr<'_>) {
205
206
// ranges [-16777215, 16777216) for type f32 as whole number floats outside
206
207
// this range are lossy and ambiguous.
207
208
#[ expect( clippy:: cast_possible_truncation) ]
208
- fn get_integer_from_float_constant ( value : & Constant < ' _ > ) -> Option < i32 > {
209
+ fn get_integer_from_float_constant ( value : & Constant ) -> Option < i32 > {
209
210
match value {
210
211
F32 ( num) if num. fract ( ) == 0.0 => {
211
212
if ( -16_777_215.0 ..16_777_216.0 ) . contains ( num) {
@@ -517,8 +518,8 @@ fn check_mul_add(cx: &LateContext<'_>, expr: &Expr<'_>) {
517
518
fn is_testing_positive ( cx : & LateContext < ' _ > , expr : & Expr < ' _ > , test : & Expr < ' _ > ) -> bool {
518
519
if let ExprKind :: Binary ( Spanned { node : op, .. } , left, right) = expr. kind {
519
520
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) ,
522
523
_ => false ,
523
524
}
524
525
} else {
@@ -530,8 +531,8 @@ fn is_testing_positive(cx: &LateContext<'_>, expr: &Expr<'_>, test: &Expr<'_>) -
530
531
fn is_testing_negative ( cx : & LateContext < ' _ > , expr : & Expr < ' _ > , test : & Expr < ' _ > ) -> bool {
531
532
if let ExprKind :: Binary ( Spanned { node : op, .. } , left, right) = expr. kind {
532
533
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) ,
535
536
_ => false ,
536
537
}
537
538
} else {
@@ -540,8 +541,8 @@ fn is_testing_negative(cx: &LateContext<'_>, expr: &Expr<'_>, test: &Expr<'_>) -
540
541
}
541
542
542
543
/// 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 ) {
545
546
Some ( Int ( i) ) => i == 0 ,
546
547
Some ( F32 ( f) ) => f == 0.0 ,
547
548
Some ( F64 ( f) ) => f == 0.0 ,
0 commit comments