Skip to content

Commit 04bded5

Browse files
committed
Auto merge of rust-lang#13235 - kyoto7250:fix_issue_13191, r=llogiq
Use `is_diagnostic_item` for checking a def_id in `unnecessary_min_or_max`. close rust-lang#13191 This PR fixes the false positives in `unnecessary_min_or_max `. We should use `is_diagnostic_item` for checking def_ids in this lint. ---- changelog: fix false positive in `unnecessary_min_or_max `.
2 parents 9e260ff + c2ed04b commit 04bded5

File tree

3 files changed

+82
-21
lines changed

3 files changed

+82
-21
lines changed

clippy_lints/src/methods/unnecessary_min_or_max.rs

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
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::{ConstEvalCtxt, Constant, ConstantSource, FullInt};
5+
use clippy_utils::diagnostics::span_lint_and_sugg;
76
use clippy_utils::source::snippet;
87

98
use rustc_errors::Applicability;
109
use rustc_hir::Expr;
1110
use rustc_lint::LateContext;
1211
use rustc_middle::ty;
13-
use rustc_span::Span;
12+
use rustc_span::{sym, Span};
1413

1514
pub(super) fn check<'tcx>(
1615
cx: &LateContext<'tcx>,
@@ -21,26 +20,30 @@ pub(super) fn check<'tcx>(
2120
) {
2221
let typeck_results = cx.typeck_results();
2322
let ecx = ConstEvalCtxt::with_env(cx.tcx, cx.param_env, typeck_results);
24-
if let Some((left, ConstantSource::Local | ConstantSource::CoreConstant)) = ecx.eval_with_source(recv)
25-
&& let Some((right, ConstantSource::Local | ConstantSource::CoreConstant)) = ecx.eval_with_source(arg)
23+
if let Some(id) = typeck_results.type_dependent_def_id(expr.hir_id)
24+
&& (cx.tcx.is_diagnostic_item(sym::cmp_ord_min, id) || cx.tcx.is_diagnostic_item(sym::cmp_ord_max, id))
2625
{
27-
let Some(ord) = Constant::partial_cmp(cx.tcx, typeck_results.expr_ty(recv), &left, &right) else {
28-
return;
29-
};
26+
if let Some((left, ConstantSource::Local | ConstantSource::CoreConstant)) = ecx.eval_with_source(recv)
27+
&& let Some((right, ConstantSource::Local | ConstantSource::CoreConstant)) = ecx.eval_with_source(arg)
28+
{
29+
let Some(ord) = Constant::partial_cmp(cx.tcx, typeck_results.expr_ty(recv), &left, &right) else {
30+
return;
31+
};
3032

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

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)