|
2 | 2 |
|
3 | 3 | use super::*; |
4 | 4 | use log::warn; |
5 | | -use syn::{spanned::Spanned as _, ExprBreak, ExprIf, ExprUnary, Stmt}; |
| 5 | +use syn::{spanned::Spanned as _, ExprBinary, ExprBreak, ExprIf, ExprUnary, Stmt}; |
6 | 6 |
|
7 | 7 | use crate::rust_ast::{comment_store, set_span::SetSpan, BytePos, SpanExt}; |
8 | 8 |
|
@@ -1048,15 +1048,33 @@ impl StructureState { |
1048 | 1048 | /// Take the logical negation of an expression. |
1049 | 1049 | /// |
1050 | 1050 | /// * Negating something of the form `!<expr>` produces `<expr>` |
| 1051 | +/// * Negating a comparison operator produces the opposite operator. |
1051 | 1052 | /// |
1052 | 1053 | fn not(bool_expr: &Expr) -> Box<Expr> { |
1053 | | - use syn::UnOp; |
| 1054 | + use syn::{BinOp::*, UnOp}; |
1054 | 1055 | match *bool_expr { |
1055 | 1056 | Expr::Unary(ExprUnary { |
1056 | 1057 | op: UnOp::Not(_), |
1057 | 1058 | ref expr, |
1058 | 1059 | .. |
1059 | 1060 | }) => Box::new(unparen(expr).clone()), |
| 1061 | + Expr::Binary(ExprBinary { |
| 1062 | + ref left, |
| 1063 | + op: op @ (Eq(_) | Ne(_) | Gt(_) | Lt(_) | Ge(_) | Le(_)), |
| 1064 | + ref right, |
| 1065 | + .. |
| 1066 | + }) => { |
| 1067 | + let op = match op { |
| 1068 | + Eq(_) => Ne(Default::default()), |
| 1069 | + Ne(_) => Eq(Default::default()), |
| 1070 | + Gt(_) => Le(Default::default()), |
| 1071 | + Lt(_) => Ge(Default::default()), |
| 1072 | + Ge(_) => Lt(Default::default()), |
| 1073 | + Le(_) => Gt(Default::default()), |
| 1074 | + _ => unreachable!(), |
| 1075 | + }; |
| 1076 | + mk().binary_expr(op, left.clone(), right.clone()) |
| 1077 | + } |
1060 | 1078 | _ => mk().unary_expr(UnOp::Not(Default::default()), Box::new(bool_expr.clone())), |
1061 | 1079 | } |
1062 | 1080 | } |
|
0 commit comments