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