clippy_utils/
comparisons.rs

1//! Utility functions for comparison operators.
2
3#![deny(clippy::missing_docs_in_private_items)]
4
5use rustc_hir::{BinOpKind, Expr};
6
7#[derive(PartialEq, Eq, Debug, Copy, Clone)]
8/// Represents a normalized comparison operator.
9pub 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`.
22pub 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}