]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/minmax.rs
Rustup
[rust.git] / clippy_lints / src / minmax.rs
1 use consts::{Constant, constant_simple};
2 use rustc::lint::*;
3 use rustc::hir::*;
4 use std::cmp::{PartialOrd, Ordering};
5 use utils::{match_def_path, paths, span_lint};
6
7 /// **What it does:** Checks for expressions where `std::cmp::min` and `max` are
8 /// used to clamp values, but switched so that the result is constant.
9 ///
10 /// **Why is this bad?** This is in all probability not the intended outcome. At
11 /// the least it hurts readability of the code.
12 ///
13 /// **Known problems:** None
14 ///
15 /// **Example:**
16 /// ```rust
17 /// min(0, max(100, x))
18 /// ```
19 /// It will always be equal to `0`. Probably the author meant to clamp the value
20 /// between 0 and 100, but has erroneously swapped `min` and `max`.
21 declare_lint! {
22     pub MIN_MAX,
23     Warn,
24     "`min(_, max(_, _))` (or vice versa) with bounds clamping the result to a constant"
25 }
26
27 #[allow(missing_copy_implementations)]
28 pub struct MinMaxPass;
29
30 impl LintPass for MinMaxPass {
31     fn get_lints(&self) -> LintArray {
32         lint_array!(MIN_MAX)
33     }
34 }
35
36 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MinMaxPass {
37     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
38         if let Some((outer_max, outer_c, oe)) = min_max(cx, expr) {
39             if let Some((inner_max, inner_c, _)) = min_max(cx, oe) {
40                 if outer_max == inner_max {
41                     return;
42                 }
43                 match (outer_max, outer_c.partial_cmp(&inner_c)) {
44                     (_, None) |
45                     (MinMax::Max, Some(Ordering::Less)) |
46                     (MinMax::Min, Some(Ordering::Greater)) => (),
47                     _ => {
48                         span_lint(cx, MIN_MAX, expr.span, "this min/max combination leads to constant result");
49                     },
50                 }
51             }
52         }
53     }
54 }
55
56 #[derive(PartialEq, Eq, Debug)]
57 enum MinMax {
58     Min,
59     Max,
60 }
61
62 fn min_max<'a>(cx: &LateContext, expr: &'a Expr) -> Option<(MinMax, Constant, &'a Expr)> {
63     if let ExprCall(ref path, ref args) = expr.node {
64         if let ExprPath(ref qpath) = path.node {
65             let def_id = cx.tables.qpath_def(qpath, path.hir_id).def_id();
66
67             if match_def_path(cx.tcx, def_id, &paths::CMP_MIN) {
68                 fetch_const(cx, args, MinMax::Min)
69             } else if match_def_path(cx.tcx, def_id, &paths::CMP_MAX) {
70                 fetch_const(cx, args, MinMax::Max)
71             } else {
72                 None
73             }
74         } else {
75             None
76         }
77     } else {
78         None
79     }
80 }
81
82 fn fetch_const<'a>(cx: &LateContext, args: &'a [Expr], m: MinMax) -> Option<(MinMax, Constant, &'a Expr)> {
83     if args.len() != 2 {
84         return None;
85     }
86     if let Some(c) = constant_simple(cx, &args[0]) {
87         if constant_simple(cx, &args[1]).is_none() {
88             // otherwise ignore
89             Some((m, c, &args[1]))
90         } else {
91             None
92         }
93     } else if let Some(c) = constant_simple(cx, &args[1]) {
94         Some((m, c, &args[0]))
95     } else {
96         None
97     }
98 }