Skip to content

Commit f8cab30

Browse files
committed
add paths of min and max and check match_def_path
#13191
1 parent 5ccf543 commit f8cab30

File tree

4 files changed

+86
-22
lines changed

4 files changed

+86
-22
lines changed

clippy_lints/src/methods/unnecessary_min_or_max.rs

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use std::cmp::Ordering;
22

33
use super::UNNECESSARY_MIN_OR_MAX;
4-
use clippy_utils::diagnostics::span_lint_and_sugg;
5-
64
use clippy_utils::consts::{constant, constant_with_source, Constant, ConstantSource, FullInt};
5+
use clippy_utils::diagnostics::span_lint_and_sugg;
76
use clippy_utils::source::snippet;
7+
use clippy_utils::{match_def_path, paths};
88

99
use rustc_errors::Applicability;
1010
use rustc_hir::Expr;
@@ -20,28 +20,32 @@ pub(super) fn check<'tcx>(
2020
arg: &'tcx Expr<'_>,
2121
) {
2222
let typeck_results = cx.typeck_results();
23-
if let Some((left, ConstantSource::Local | ConstantSource::CoreConstant)) =
24-
constant_with_source(cx, typeck_results, recv)
25-
&& let Some((right, ConstantSource::Local | ConstantSource::CoreConstant)) =
26-
constant_with_source(cx, typeck_results, arg)
23+
if let Some(id) = typeck_results.type_dependent_def_id(expr.hir_id)
24+
&& (match_def_path(cx, id, &paths::CORE_CMP_MIN) || match_def_path(cx, id, &paths::CORE_CMP_MAX))
2725
{
28-
let Some(ord) = Constant::partial_cmp(cx.tcx, typeck_results.expr_ty(recv), &left, &right) else {
29-
return;
30-
};
26+
if let Some((left, ConstantSource::Local | ConstantSource::CoreConstant)) =
27+
constant_with_source(cx, typeck_results, recv)
28+
&& let Some((right, ConstantSource::Local | ConstantSource::CoreConstant)) =
29+
constant_with_source(cx, typeck_results, arg)
30+
{
31+
let Some(ord) = Constant::partial_cmp(cx.tcx, typeck_results.expr_ty(recv), &left, &right) else {
32+
return;
33+
};
3134

32-
lint(cx, expr, name, recv.span, arg.span, ord);
33-
} else if let Some(extrema) = detect_extrema(cx, recv) {
34-
let ord = match extrema {
35-
Extrema::Minimum => Ordering::Less,
36-
Extrema::Maximum => Ordering::Greater,
37-
};
38-
lint(cx, expr, name, recv.span, arg.span, ord);
39-
} else if let Some(extrema) = detect_extrema(cx, arg) {
40-
let ord = match extrema {
41-
Extrema::Minimum => Ordering::Greater,
42-
Extrema::Maximum => Ordering::Less,
43-
};
44-
lint(cx, expr, name, recv.span, arg.span, ord);
35+
lint(cx, expr, name, recv.span, arg.span, ord);
36+
} else if let Some(extrema) = detect_extrema(cx, recv) {
37+
let ord = match extrema {
38+
Extrema::Minimum => Ordering::Less,
39+
Extrema::Maximum => Ordering::Greater,
40+
};
41+
lint(cx, expr, name, recv.span, arg.span, ord);
42+
} else if let Some(extrema) = detect_extrema(cx, arg) {
43+
let ord = match extrema {
44+
Extrema::Minimum => Ordering::Greater,
45+
Extrema::Maximum => Ordering::Less,
46+
};
47+
lint(cx, expr, name, recv.span, arg.span, ord);
48+
}
4549
}
4650
}
4751

clippy_utils/src/paths.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ pub const BTREEMAP_CONTAINS_KEY: [&str; 6] = ["alloc", "collections", "btree", "
1717
pub const BTREEMAP_INSERT: [&str; 6] = ["alloc", "collections", "btree", "map", "BTreeMap", "insert"];
1818
pub const BTREESET_ITER: [&str; 6] = ["alloc", "collections", "btree", "set", "BTreeSet", "iter"];
1919
pub const CLONE_TRAIT_METHOD: [&str; 4] = ["core", "clone", "Clone", "clone"];
20+
pub const CORE_CMP_MIN: [&str; 4] = ["core", "cmp", "Ord", "min"];
21+
pub const CORE_CMP_MAX: [&str; 4] = ["core", "cmp", "Ord", "max"];
2022
pub const CORE_ITER_CLONED: [&str; 6] = ["core", "iter", "traits", "iterator", "Iterator", "cloned"];
2123
pub const CORE_ITER_COPIED: [&str; 6] = ["core", "iter", "traits", "iterator", "Iterator", "copied"];
2224
pub const CORE_ITER_FILTER: [&str; 6] = ["core", "iter", "traits", "iterator", "Iterator", "filter"];

tests/ui/unnecessary_min_or_max.fixed

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,32 @@ fn random_u32() -> u32 {
6565
// random number generator
6666
0
6767
}
68+
69+
struct Issue13191 {
70+
min: u16,
71+
max: u16,
72+
}
73+
74+
impl Issue13191 {
75+
fn new() -> Self {
76+
Self { min: 0, max: 0 }
77+
}
78+
79+
fn min(mut self, value: u16) -> Self {
80+
self.min = value;
81+
self
82+
}
83+
84+
fn max(mut self, value: u16) -> Self {
85+
self.max = value;
86+
self
87+
}
88+
}
89+
90+
fn issue_13191() {
91+
// should not fixed
92+
Issue13191::new().min(0);
93+
94+
// should not fixed
95+
Issue13191::new().max(0);
96+
}

tests/ui/unnecessary_min_or_max.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,32 @@ fn random_u32() -> u32 {
6565
// random number generator
6666
0
6767
}
68+
69+
struct Issue13191 {
70+
min: u16,
71+
max: u16,
72+
}
73+
74+
impl Issue13191 {
75+
fn new() -> Self {
76+
Self { min: 0, max: 0 }
77+
}
78+
79+
fn min(mut self, value: u16) -> Self {
80+
self.min = value;
81+
self
82+
}
83+
84+
fn max(mut self, value: u16) -> Self {
85+
self.max = value;
86+
self
87+
}
88+
}
89+
90+
fn issue_13191() {
91+
// should not fixed
92+
Issue13191::new().min(0);
93+
94+
// should not fixed
95+
Issue13191::new().max(0);
96+
}

0 commit comments

Comments
 (0)