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