@@ -14,11 +14,15 @@ use super::SUBOPTIMAL_FLOPS;
1414/// test is positive or an expression which tests whether or not test
1515/// is nonnegative.
1616/// Used for check-custom-abs function below
17- fn is_testing_positive ( cx : & LateContext < ' _ > , expr : & Expr < ' _ > , test : & Expr < ' _ > ) -> bool {
17+ fn is_testing_positive ( cx : & LateContext < ' _ > , ctxt : SyntaxContext , expr : & Expr < ' _ > , test : & Expr < ' _ > ) -> bool {
1818 if let ExprKind :: Binary ( Spanned { node : op, .. } , left, right) = expr. kind {
1919 match op {
20- BinOpKind :: Gt | BinOpKind :: Ge => is_zero ( cx, right, expr. span . ctxt ( ) ) && eq_expr_value ( cx, left, test) ,
21- BinOpKind :: Lt | BinOpKind :: Le => is_zero ( cx, left, expr. span . ctxt ( ) ) && eq_expr_value ( cx, right, test) ,
20+ BinOpKind :: Gt | BinOpKind :: Ge => {
21+ is_zero ( cx, right, expr. span . ctxt ( ) ) && eq_expr_value ( cx, ctxt, left, test)
22+ } ,
23+ BinOpKind :: Lt | BinOpKind :: Le => {
24+ is_zero ( cx, left, expr. span . ctxt ( ) ) && eq_expr_value ( cx, ctxt, right, test)
25+ } ,
2226 _ => false ,
2327 }
2428 } else {
@@ -27,11 +31,15 @@ fn is_testing_positive(cx: &LateContext<'_>, expr: &Expr<'_>, test: &Expr<'_>) -
2731}
2832
2933/// See [`is_testing_positive`]
30- fn is_testing_negative ( cx : & LateContext < ' _ > , expr : & Expr < ' _ > , test : & Expr < ' _ > ) -> bool {
34+ fn is_testing_negative ( cx : & LateContext < ' _ > , ctxt : SyntaxContext , expr : & Expr < ' _ > , test : & Expr < ' _ > ) -> bool {
3135 if let ExprKind :: Binary ( Spanned { node : op, .. } , left, right) = expr. kind {
3236 match op {
33- BinOpKind :: Gt | BinOpKind :: Ge => is_zero ( cx, left, expr. span . ctxt ( ) ) && eq_expr_value ( cx, right, test) ,
34- BinOpKind :: Lt | BinOpKind :: Le => is_zero ( cx, right, expr. span . ctxt ( ) ) && eq_expr_value ( cx, left, test) ,
37+ BinOpKind :: Gt | BinOpKind :: Ge => {
38+ is_zero ( cx, left, expr. span . ctxt ( ) ) && eq_expr_value ( cx, ctxt, right, test)
39+ } ,
40+ BinOpKind :: Lt | BinOpKind :: Le => {
41+ is_zero ( cx, right, expr. span . ctxt ( ) ) && eq_expr_value ( cx, ctxt, left, test)
42+ } ,
3543 _ => false ,
3644 }
3745 } else {
@@ -55,14 +63,21 @@ fn is_zero(cx: &LateContext<'_>, expr: &Expr<'_>, ctxt: SyntaxContext) -> bool {
5563/// one of the two expressions
5664/// If the two expressions are not negations of each other, then it
5765/// returns None.
58- fn are_negated < ' a > ( cx : & LateContext < ' _ > , expr1 : & ' a Expr < ' a > , expr2 : & ' a Expr < ' a > ) -> Option < ( bool , & ' a Expr < ' a > ) > {
66+ fn are_negated < ' a > (
67+ cx : & LateContext < ' _ > ,
68+ ctxt : SyntaxContext ,
69+ expr1 : & ' a Expr < ' a > ,
70+ expr2 : & ' a Expr < ' a > ,
71+ ) -> Option < ( bool , & ' a Expr < ' a > ) > {
5972 if let ExprKind :: Unary ( UnOp :: Neg , expr1_negated) = expr1. kind
60- && eq_expr_value ( cx, expr1_negated, expr2)
73+ && expr1_negated. span . ctxt ( ) == ctxt
74+ && eq_expr_value ( cx, ctxt, expr1_negated, expr2)
6175 {
6276 return Some ( ( false , expr2) ) ;
6377 }
6478 if let ExprKind :: Unary ( UnOp :: Neg , expr2_negated) = expr2. kind
65- && eq_expr_value ( cx, expr1, expr2_negated)
79+ && expr2_negated. span . ctxt ( ) == ctxt
80+ && eq_expr_value ( cx, ctxt, expr1, expr2_negated)
6681 {
6782 return Some ( ( true , expr1) ) ;
6883 }
@@ -77,11 +92,12 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>) {
7792 } ) = higher:: If :: hir ( expr)
7893 && let if_body_expr = peel_blocks ( then)
7994 && let else_body_expr = peel_blocks ( r#else)
80- && let Some ( ( if_expr_positive, body) ) = are_negated ( cx, if_body_expr, else_body_expr)
95+ && let ctxt = expr. span . ctxt ( )
96+ && let Some ( ( if_expr_positive, body) ) = are_negated ( cx, ctxt, if_body_expr, else_body_expr)
8197 {
82- let sugg_positive_abs = if is_testing_positive ( cx, cond, body) {
98+ let sugg_positive_abs = if is_testing_positive ( cx, ctxt , cond, body) {
8399 if_expr_positive
84- } else if is_testing_negative ( cx, cond, body) {
100+ } else if is_testing_negative ( cx, ctxt , cond, body) {
85101 !if_expr_positive
86102 } else {
87103 return ;
0 commit comments