Skip to content

Commit 118446e

Browse files
committed
Trigger absurd_extreme_comparisons on if Duration is less than zero
1 parent 689e62b commit 118446e

File tree

3 files changed

+59
-22
lines changed

3 files changed

+59
-22
lines changed

clippy_lints/src/operators/absurd_extreme_comparisons.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use clippy_utils::comparisons::{Rel, normalize_comparison};
66
use clippy_utils::consts::{ConstEvalCtxt, Constant};
77
use clippy_utils::diagnostics::span_lint_and_help;
88
use clippy_utils::source::snippet;
9-
use clippy_utils::ty::is_isize_or_usize;
10-
use clippy_utils::{clip, int_bits, unsext};
9+
use clippy_utils::ty::{is_isize_or_usize, is_type_diagnostic_item};
10+
use clippy_utils::{clip, int_bits, sym, unsext};
1111

1212
use super::ABSURD_EXTREME_COMPARISONS;
1313

@@ -119,6 +119,21 @@ fn detect_absurd_comparison<'tcx>(
119119
}
120120

121121
fn detect_extreme_expr<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> Option<ExtremeExpr<'tcx>> {
122+
// Detect comparisons with Duration::from_secs(0), which is an extreme value
123+
// because Duration's internal representation ensures it can never be less than 0
124+
if let ExprKind::Call(_, args) = &expr.kind
125+
&& args.len() == 1
126+
&& is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(expr), sym::Duration)
127+
{
128+
let const_val = ConstEvalCtxt::new(cx).eval(&args[0]);
129+
if let Some(Constant::Int(0)) = const_val {
130+
return Some(ExtremeExpr {
131+
which: ExtremeType::Minimum,
132+
expr,
133+
});
134+
}
135+
}
136+
122137
let ty = cx.typeck_results().expr_ty(expr);
123138

124139
let cv = ConstEvalCtxt::new(cx).eval(expr)?;

tests/ui/absurd-extreme-comparisons.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
clippy::needless_pass_by_value
88
)]
99

10+
use std::time::Duration;
11+
1012
#[rustfmt::skip]
1113
fn main() {
1214
const Z: u32 = 0;
@@ -69,7 +71,11 @@ fn main() {
6971
() < {};
7072
//~^ unit_cmp
7173

72-
74+
Duration::from_secs(0) > Duration::new(5, 0);
75+
//~^ absurd_extreme_comparisons
76+
Duration::new(5, 0) < Duration::from_secs(0);
77+
//~^ absurd_extreme_comparisons
78+
Duration::from_secs(0) < Duration::new(5, 0); // ok
7379
}
7480

7581
use std::cmp::{Ordering, PartialEq, PartialOrd};
Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: this comparison involving the minimum or maximum element for this type contains a case that is always true or always false
2-
--> tests/ui/absurd-extreme-comparisons.rs:14:5
2+
--> tests/ui/absurd-extreme-comparisons.rs:16:5
33
|
44
LL | u <= 0;
55
| ^^^^^^
@@ -9,140 +9,156 @@ LL | u <= 0;
99
= help: to override `-D warnings` add `#[allow(clippy::absurd_extreme_comparisons)]`
1010

1111
error: this comparison involving the minimum or maximum element for this type contains a case that is always true or always false
12-
--> tests/ui/absurd-extreme-comparisons.rs:17:5
12+
--> tests/ui/absurd-extreme-comparisons.rs:19:5
1313
|
1414
LL | u <= Z;
1515
| ^^^^^^
1616
|
1717
= help: because `Z` is the minimum value for this type, the case where the two sides are not equal never occurs, consider using `u == Z` instead
1818

1919
error: this comparison involving the minimum or maximum element for this type contains a case that is always true or always false
20-
--> tests/ui/absurd-extreme-comparisons.rs:20:5
20+
--> tests/ui/absurd-extreme-comparisons.rs:22:5
2121
|
2222
LL | u < Z;
2323
| ^^^^^
2424
|
2525
= help: because `Z` is the minimum value for this type, this comparison is always false
2626

2727
error: this comparison involving the minimum or maximum element for this type contains a case that is always true or always false
28-
--> tests/ui/absurd-extreme-comparisons.rs:23:5
28+
--> tests/ui/absurd-extreme-comparisons.rs:25:5
2929
|
3030
LL | Z >= u;
3131
| ^^^^^^
3232
|
3333
= help: because `Z` is the minimum value for this type, the case where the two sides are not equal never occurs, consider using `Z == u` instead
3434

3535
error: this comparison involving the minimum or maximum element for this type contains a case that is always true or always false
36-
--> tests/ui/absurd-extreme-comparisons.rs:26:5
36+
--> tests/ui/absurd-extreme-comparisons.rs:28:5
3737
|
3838
LL | Z > u;
3939
| ^^^^^
4040
|
4141
= help: because `Z` is the minimum value for this type, this comparison is always false
4242

4343
error: this comparison involving the minimum or maximum element for this type contains a case that is always true or always false
44-
--> tests/ui/absurd-extreme-comparisons.rs:29:5
44+
--> tests/ui/absurd-extreme-comparisons.rs:31:5
4545
|
4646
LL | u > u32::MAX;
4747
| ^^^^^^^^^^^^
4848
|
4949
= help: because `u32::MAX` is the maximum value for this type, this comparison is always false
5050

5151
error: this comparison involving the minimum or maximum element for this type contains a case that is always true or always false
52-
--> tests/ui/absurd-extreme-comparisons.rs:32:5
52+
--> tests/ui/absurd-extreme-comparisons.rs:34:5
5353
|
5454
LL | u >= u32::MAX;
5555
| ^^^^^^^^^^^^^
5656
|
5757
= help: because `u32::MAX` is the maximum value for this type, the case where the two sides are not equal never occurs, consider using `u == u32::MAX` instead
5858

5959
error: this comparison involving the minimum or maximum element for this type contains a case that is always true or always false
60-
--> tests/ui/absurd-extreme-comparisons.rs:35:5
60+
--> tests/ui/absurd-extreme-comparisons.rs:37:5
6161
|
6262
LL | u32::MAX < u;
6363
| ^^^^^^^^^^^^
6464
|
6565
= help: because `u32::MAX` is the maximum value for this type, this comparison is always false
6666

6767
error: this comparison involving the minimum or maximum element for this type contains a case that is always true or always false
68-
--> tests/ui/absurd-extreme-comparisons.rs:38:5
68+
--> tests/ui/absurd-extreme-comparisons.rs:40:5
6969
|
7070
LL | u32::MAX <= u;
7171
| ^^^^^^^^^^^^^
7272
|
7373
= help: because `u32::MAX` is the maximum value for this type, the case where the two sides are not equal never occurs, consider using `u32::MAX == u` instead
7474

7575
error: this comparison involving the minimum or maximum element for this type contains a case that is always true or always false
76-
--> tests/ui/absurd-extreme-comparisons.rs:41:5
76+
--> tests/ui/absurd-extreme-comparisons.rs:43:5
7777
|
7878
LL | 1-1 > u;
7979
| ^^^^^^^
8080
|
8181
= help: because `1-1` is the minimum value for this type, this comparison is always false
8282

8383
error: this comparison involving the minimum or maximum element for this type contains a case that is always true or always false
84-
--> tests/ui/absurd-extreme-comparisons.rs:44:5
84+
--> tests/ui/absurd-extreme-comparisons.rs:46:5
8585
|
8686
LL | u >= !0;
8787
| ^^^^^^^
8888
|
8989
= help: because `!0` is the maximum value for this type, the case where the two sides are not equal never occurs, consider using `u == !0` instead
9090

9191
error: this comparison involving the minimum or maximum element for this type contains a case that is always true or always false
92-
--> tests/ui/absurd-extreme-comparisons.rs:47:5
92+
--> tests/ui/absurd-extreme-comparisons.rs:49:5
9393
|
9494
LL | u <= 12 - 2*6;
9595
| ^^^^^^^^^^^^^
9696
|
9797
= help: because `12 - 2*6` is the minimum value for this type, the case where the two sides are not equal never occurs, consider using `u == 12 - 2*6` instead
9898

9999
error: this comparison involving the minimum or maximum element for this type contains a case that is always true or always false
100-
--> tests/ui/absurd-extreme-comparisons.rs:51:5
100+
--> tests/ui/absurd-extreme-comparisons.rs:53:5
101101
|
102102
LL | i < -127 - 1;
103103
| ^^^^^^^^^^^^
104104
|
105105
= help: because `-127 - 1` is the minimum value for this type, this comparison is always false
106106

107107
error: this comparison involving the minimum or maximum element for this type contains a case that is always true or always false
108-
--> tests/ui/absurd-extreme-comparisons.rs:54:5
108+
--> tests/ui/absurd-extreme-comparisons.rs:56:5
109109
|
110110
LL | i8::MAX >= i;
111111
| ^^^^^^^^^^^^
112112
|
113113
= help: because `i8::MAX` is the maximum value for this type, this comparison is always true
114114

115115
error: this comparison involving the minimum or maximum element for this type contains a case that is always true or always false
116-
--> tests/ui/absurd-extreme-comparisons.rs:57:5
116+
--> tests/ui/absurd-extreme-comparisons.rs:59:5
117117
|
118118
LL | 3-7 < i32::MIN;
119119
| ^^^^^^^^^^^^^^
120120
|
121121
= help: because `i32::MIN` is the minimum value for this type, this comparison is always false
122122

123123
error: this comparison involving the minimum or maximum element for this type contains a case that is always true or always false
124-
--> tests/ui/absurd-extreme-comparisons.rs:61:5
124+
--> tests/ui/absurd-extreme-comparisons.rs:63:5
125125
|
126126
LL | b >= true;
127127
| ^^^^^^^^^
128128
|
129129
= help: because `true` is the maximum value for this type, the case where the two sides are not equal never occurs, consider using `b == true` instead
130130

131131
error: this comparison involving the minimum or maximum element for this type contains a case that is always true or always false
132-
--> tests/ui/absurd-extreme-comparisons.rs:64:5
132+
--> tests/ui/absurd-extreme-comparisons.rs:66:5
133133
|
134134
LL | false > b;
135135
| ^^^^^^^^^
136136
|
137137
= help: because `false` is the minimum value for this type, this comparison is always false
138138

139139
error: <-comparison of unit values detected. This will always be false
140-
--> tests/ui/absurd-extreme-comparisons.rs:69:5
140+
--> tests/ui/absurd-extreme-comparisons.rs:71:5
141141
|
142142
LL | () < {};
143143
| ^^^^^^^
144144
|
145145
= note: `#[deny(clippy::unit_cmp)]` on by default
146146

147-
error: aborting due to 18 previous errors
147+
error: this comparison involving the minimum or maximum element for this type contains a case that is always true or always false
148+
--> tests/ui/absurd-extreme-comparisons.rs:74:5
149+
|
150+
LL | Duration::from_secs(0) > Duration::new(5, 0);
151+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
152+
|
153+
= help: because `Duration::from_secs(0)` is the minimum value for this type, this comparison is always false
154+
155+
error: this comparison involving the minimum or maximum element for this type contains a case that is always true or always false
156+
--> tests/ui/absurd-extreme-comparisons.rs:76:5
157+
|
158+
LL | Duration::new(5, 0) < Duration::from_secs(0);
159+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
160+
|
161+
= help: because `Duration::from_secs(0)` is the minimum value for this type, this comparison is always false
162+
163+
error: aborting due to 20 previous errors
148164

0 commit comments

Comments
 (0)