1
1
use std:: cmp:: Ordering ;
2
2
3
3
use super :: UNNECESSARY_MIN_OR_MAX ;
4
- use clippy_utils:: diagnostics:: span_lint_and_sugg;
5
-
6
4
use clippy_utils:: consts:: { ConstEvalCtxt , Constant , ConstantSource , FullInt } ;
5
+ use clippy_utils:: diagnostics:: span_lint_and_sugg;
7
6
use clippy_utils:: source:: snippet;
8
7
9
8
use rustc_errors:: Applicability ;
10
9
use rustc_hir:: Expr ;
11
10
use rustc_lint:: LateContext ;
12
11
use rustc_middle:: ty;
13
- use rustc_span:: Span ;
12
+ use rustc_span:: { sym , Span } ;
14
13
15
14
pub ( super ) fn check < ' tcx > (
16
15
cx : & LateContext < ' tcx > ,
@@ -21,26 +20,30 @@ pub(super) fn check<'tcx>(
21
20
) {
22
21
let typeck_results = cx. typeck_results ( ) ;
23
22
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 ) )
26
25
{
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
+ } ;
30
32
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
+ }
44
47
}
45
48
}
46
49
0 commit comments