use std::cmp::Ordering::*;
use consts::{Constant, constant_simple};
-use utils::{match_path, span_lint};
+use utils::{match_def_path, span_lint};
use self::MinMax::{Min, Max};
declare_lint!(pub MIN_MAX, Warn,
impl LateLintPass for MinMaxPass {
fn check_expr(&mut self, cx: &LateContext, expr: &Expr) {
- if let Some((outer_max, outer_c, oe)) = min_max(expr) {
- if let Some((inner_max, inner_c, _)) = min_max(oe) {
+ if let Some((outer_max, outer_c, oe)) = min_max(cx, expr) {
+ if let Some((inner_max, inner_c, _)) = min_max(cx, oe) {
if outer_max == inner_max { return; }
match (outer_max, outer_c.partial_cmp(&inner_c)) {
(_, None) | (Max, Some(Less)) | (Min, Some(Greater)) => (),
Max,
}
-fn min_max(expr: &Expr) -> Option<(MinMax, Constant, &Expr)> {
+fn min_max<'a>(cx: &LateContext, expr: &'a Expr) -> Option<(MinMax, Constant, &'a Expr)> {
if let ExprCall(ref path, ref args) = expr.node {
- if let ExprPath(None, ref path) = path.node {
- if match_path(path, &["std", "cmp", "min"]) {
+ if let ExprPath(None, _) = path.node {
+ let def_id = cx.tcx.def_map.borrow()[&path.id].def_id();
+
+ if match_def_path(cx, def_id, &["core", "cmp", "min"]) {
fetch_const(args, Min)
} else {
- if match_path(path, &["std", "cmp", "max"]) {
+ if match_def_path(cx, def_id, &["core", "cmp", "max"]) {
fetch_const(args, Max)
} else {
None
#![deny(clippy)]
use std::cmp::{min, max};
+use std::cmp::min as my_min;
+use std::cmp::max as my_max;
const LARGE : usize = 3;
max(min(x, 1), 3); //~ERROR this min/max combination leads to constant result
max(3, min(x, 1)); //~ERROR this min/max combination leads to constant result
+ my_max(3, my_min(x, 1)); //~ERROR this min/max combination leads to constant result
+
min(3, max(1, x)); // ok, could be 1, 2 or 3 depending on x
min(1, max(LARGE, x)); // no error, we don't lookup consts here