]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_utils/src/comparisons.rs
Auto merge of #102755 - pcc:data-local-tmp, r=Mark-Simulacrum
[rust.git] / src / tools / clippy / clippy_utils / src / 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>(
23     op: BinOpKind,
24     lhs: &'a Expr<'a>,
25     rhs: &'a Expr<'a>,
26 ) -> Option<(Rel, &'a Expr<'a>, &'a Expr<'a>)> {
27     match op {
28         BinOpKind::Lt => Some((Rel::Lt, lhs, rhs)),
29         BinOpKind::Le => Some((Rel::Le, lhs, rhs)),
30         BinOpKind::Gt => Some((Rel::Lt, rhs, lhs)),
31         BinOpKind::Ge => Some((Rel::Le, rhs, lhs)),
32         BinOpKind::Eq => Some((Rel::Eq, rhs, lhs)),
33         BinOpKind::Ne => Some((Rel::Ne, rhs, lhs)),
34         _ => None,
35     }
36 }