2016-08-23 17:39:36 +00:00
|
|
|
//! Utility functions about comparison operators.
|
|
|
|
|
2018-08-01 20:48:41 +00:00
|
|
|
#![deny(clippy::missing_docs_in_private_items)]
|
2016-08-23 17:39:36 +00:00
|
|
|
|
2020-01-06 16:39:50 +00:00
|
|
|
use rustc_hir::{BinOpKind, Expr};
|
2016-03-26 05:57:03 +00:00
|
|
|
|
|
|
|
#[derive(PartialEq, Eq, Debug, Copy, Clone)]
|
2016-08-23 17:39:36 +00:00
|
|
|
/// Represent a normalized comparison operator.
|
2016-03-26 05:57:03 +00:00
|
|
|
pub enum Rel {
|
2016-08-23 17:39:36 +00:00
|
|
|
/// `<`
|
2016-03-26 05:57:03 +00:00
|
|
|
Lt,
|
2016-08-23 17:39:36 +00:00
|
|
|
/// `<=`
|
2016-03-26 05:57:03 +00:00
|
|
|
Le,
|
2016-08-23 17:39:36 +00:00
|
|
|
/// `==`
|
2016-03-29 05:06:57 +00:00
|
|
|
Eq,
|
2016-08-23 17:39:36 +00:00
|
|
|
/// `!=`
|
2016-03-29 05:06:57 +00:00
|
|
|
Ne,
|
2016-03-26 05:57:03 +00:00
|
|
|
}
|
|
|
|
|
2017-08-09 07:30:56 +00:00
|
|
|
/// Put the expression in the form `lhs < rhs`, `lhs <= rhs`, `lhs == rhs` or
|
|
|
|
/// `lhs != rhs`.
|
2019-12-27 07:12:26 +00:00
|
|
|
pub fn normalize_comparison<'a>(
|
|
|
|
op: BinOpKind,
|
|
|
|
lhs: &'a Expr<'a>,
|
|
|
|
rhs: &'a Expr<'a>,
|
|
|
|
) -> Option<(Rel, &'a Expr<'a>, &'a Expr<'a>)> {
|
2016-03-26 05:57:03 +00:00
|
|
|
match op {
|
2018-07-12 07:50:09 +00:00
|
|
|
BinOpKind::Lt => Some((Rel::Lt, lhs, rhs)),
|
|
|
|
BinOpKind::Le => Some((Rel::Le, lhs, rhs)),
|
|
|
|
BinOpKind::Gt => Some((Rel::Lt, rhs, lhs)),
|
|
|
|
BinOpKind::Ge => Some((Rel::Le, rhs, lhs)),
|
|
|
|
BinOpKind::Eq => Some((Rel::Eq, rhs, lhs)),
|
|
|
|
BinOpKind::Ne => Some((Rel::Ne, rhs, lhs)),
|
2016-03-26 05:57:03 +00:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|