]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/utils/comparisons.rs
Merge branch 'master' into fix-4727
[rust.git] / clippy_lints / src / utils / comparisons.rs
1 //! Utility functions about comparison operators.
2
3 #![deny(clippy::missing_docs_in_private_items)]
4
5 use rustc::hir::{BinOpKind, Expr};
6
7 #[derive(PartialEq, Eq, Debug, Copy, Clone)]
8 /// Represent a normalized comparison operator.
9 pub enum Rel {
10     /// `<`
11     Lt,
12     /// `<=`
13     Le,
14     /// `==`
15     Eq,
16     /// `!=`
17     Ne,
18 }
19
20 /// Put the expression in the form  `lhs < rhs`, `lhs <= rhs`, `lhs == rhs` or
21 /// `lhs != rhs`.
22 pub fn normalize_comparison<'a>(op: BinOpKind, lhs: &'a Expr, rhs: &'a Expr) -> Option<(Rel, &'a Expr, &'a Expr)> {
23     match op {
24         BinOpKind::Lt => Some((Rel::Lt, lhs, rhs)),
25         BinOpKind::Le => Some((Rel::Le, lhs, rhs)),
26         BinOpKind::Gt => Some((Rel::Lt, rhs, lhs)),
27         BinOpKind::Ge => Some((Rel::Le, rhs, lhs)),
28         BinOpKind::Eq => Some((Rel::Eq, rhs, lhs)),
29         BinOpKind::Ne => Some((Rel::Ne, rhs, lhs)),
30         _ => None,
31     }
32 }