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