@@ -11,6 +11,7 @@ use rustc_hir::{BinOpKind, Expr, ExprKind, PathSegment, UnOp};
1111use rustc_lint:: { LateContext , LateLintPass } ;
1212use rustc_middle:: ty;
1313use rustc_session:: declare_lint_pass;
14+ use rustc_span:: SyntaxContext ;
1415use rustc_span:: source_map:: Spanned ;
1516use std:: f32:: consts as f32_consts;
1617use std:: f64:: consts as f64_consts;
@@ -110,8 +111,8 @@ declare_lint_pass!(FloatingPointArithmetic => [
110111
111112// Returns the specialized log method for a given base if base is constant
112113// 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 ) {
115116 if F32 ( 2.0 ) == value || F64 ( 2.0 ) == value {
116117 return Some ( "log2" ) ;
117118 } 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
157158}
158159
159160fn 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 ( ) ) {
161162 span_lint_and_sugg (
162163 cx,
163164 SUBOPTIMAL_FLOPS ,
@@ -205,7 +206,7 @@ fn check_ln1p(cx: &LateContext<'_>, expr: &Expr<'_>, receiver: &Expr<'_>) {
205206// ranges [-16777215, 16777216) for type f32 as whole number floats outside
206207// this range are lossy and ambiguous.
207208#[ 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 > {
209210 match value {
210211 F32 ( num) if num. fract ( ) == 0.0 => {
211212 if ( -16_777_215.0 ..16_777_216.0 ) . contains ( num) {
@@ -517,8 +518,8 @@ fn check_mul_add(cx: &LateContext<'_>, expr: &Expr<'_>) {
517518fn 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<'_>) -
530531fn 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 ,
0 commit comments