rust-clippy/clippy_lints/src/utils/comparisons.rs

33 lines
877 B
Rust
Raw Normal View History

//! Utility functions about comparison operators.
#![deny(missing_docs_in_private_items)]
use rustc::hir::{BinOp_, Expr};
2016-03-26 05:57:03 +00:00
#[derive(PartialEq, Eq, Debug, Copy, Clone)]
/// Represent a normalized comparison operator.
2016-03-26 05:57:03 +00:00
pub enum Rel {
/// `<`
2016-03-26 05:57:03 +00:00
Lt,
/// `<=`
2016-03-26 05:57:03 +00:00
Le,
/// `==`
Eq,
/// `!=`
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`.
2016-12-20 17:21:30 +00:00
pub fn normalize_comparison<'a>(op: BinOp_, lhs: &'a Expr, rhs: &'a Expr) -> Option<(Rel, &'a Expr, &'a Expr)> {
2016-03-26 05:57:03 +00:00
match op {
BinOp_::BiLt => Some((Rel::Lt, lhs, rhs)),
BinOp_::BiLe => Some((Rel::Le, lhs, rhs)),
BinOp_::BiGt => Some((Rel::Lt, rhs, lhs)),
BinOp_::BiGe => Some((Rel::Le, rhs, lhs)),
BinOp_::BiEq => Some((Rel::Eq, rhs, lhs)),
BinOp_::BiNe => Some((Rel::Ne, rhs, lhs)),
2016-03-26 05:57:03 +00:00
_ => None,
}
}