]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/utils/comparisons.rs
Introduce snippet_with_applicability and hir_with_applicability functions
[rust.git] / clippy_lints / src / utils / comparisons.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10
11 //! Utility functions about comparison operators.
12
13 #![deny(clippy::missing_docs_in_private_items)]
14
15 use crate::rustc::hir::{BinOpKind, Expr};
16
17 #[derive(PartialEq, Eq, Debug, Copy, Clone)]
18 /// Represent a normalized comparison operator.
19 pub enum Rel {
20     /// `<`
21     Lt,
22     /// `<=`
23     Le,
24     /// `==`
25     Eq,
26     /// `!=`
27     Ne,
28 }
29
30 /// Put the expression in the form  `lhs < rhs`, `lhs <= rhs`, `lhs == rhs` or
31 /// `lhs != rhs`.
32 pub fn normalize_comparison<'a>(op: BinOpKind, lhs: &'a Expr, rhs: &'a Expr) -> Option<(Rel, &'a Expr, &'a Expr)> {
33     match op {
34         BinOpKind::Lt => Some((Rel::Lt, lhs, rhs)),
35         BinOpKind::Le => Some((Rel::Le, lhs, rhs)),
36         BinOpKind::Gt => Some((Rel::Lt, rhs, lhs)),
37         BinOpKind::Ge => Some((Rel::Le, rhs, lhs)),
38         BinOpKind::Eq => Some((Rel::Eq, rhs, lhs)),
39         BinOpKind::Ne => Some((Rel::Ne, rhs, lhs)),
40         _ => None,
41     }
42 }