Skip to content

Commit 3c536bd

Browse files
committed
Apply suggestions
1 parent de53ea7 commit 3c536bd

4 files changed

Lines changed: 94 additions & 107 deletions

File tree

clippy_lints/src/duration_suboptimal_units.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ declare_clippy_lint! {
1818
///
1919
/// Checks for instances where a `std::time::Duration` is constructed using a smaller time unit
2020
/// when the value could be expressed more clearly using a larger unit.
21+
///
2122
/// For literal values that would convert to 10 or fewer of the larger unit,
2223
/// this lint does not apply.
2324
///
@@ -83,7 +84,7 @@ impl LateLintPass<'_> for DurationSuboptimalUnits {
8384
// recommend a literal value over using constants:
8485
//
8586
// let dur = Duration::from_millis(TWELVE_THOUSAND);
86-
// ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Duration::from_secs(12)`
87+
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Duration::from_secs(12)`
8788
&& let Some(Constant::Int(value)) = ConstEvalCtxt::new(cx).eval_local(arg, expr.span.ctxt())
8889
&& let Ok(value) = u64::try_from(value) // Cannot fail
8990
// There is no need to promote e.g. 0 seconds to 0 hours

tests/ui/duration_suboptimal_units.fixed

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::time::Duration;
66
const SIXTY: u64 = 60;
77

88
macro_rules! mac {
9-
(slow_rythm) => {
9+
(slow_rhythm) => {
1010
60 * 60
1111
};
1212
(duration) => {
@@ -23,26 +23,6 @@ fn main() {
2323
let dur = Duration::from_secs(42);
2424
let dur = Duration::from_hours(3);
2525

26-
let dur = Duration::from_mins(1);
27-
//~^ duration_suboptimal_units
28-
29-
// Literals with small promoted values should not lint (issue #16532)
30-
let dur = Duration::from_secs(60);
31-
let dur = Duration::from_secs(180);
32-
let dur = Duration::from_secs(180);
33-
let dur = Duration::from_millis(5_000);
34-
let dur = Duration::from_millis(1_000);
35-
let dur = Duration::from_secs(3_600);
36-
let dur = Duration::from_secs(600);
37-
38-
// Literals with larger promoted values should lint
39-
let dur = Duration::from_secs(20);
40-
//~^ duration_suboptimal_units
41-
let dur = Duration::from_hours(12);
42-
//~^ duration_suboptimal_units
43-
let dur = Duration::from_mins(11);
44-
//~^ duration_suboptimal_units
45-
4626
let dur = Duration::from_mins(3);
4727
//~^ duration_suboptimal_units
4828
let dur = Duration::from_mins(10);
@@ -99,7 +79,7 @@ fn main() {
9979
let dur = proc_macros::external! { Duration::from_secs(60 * 60) };
10080

10181
// We don't lint values coming from macros
102-
let dur = Duration::from_secs(mac!(slow_rythm));
82+
let dur = Duration::from_secs(mac!(slow_rhythm));
10383
}
10484

10585
mod my_duration {
@@ -132,3 +112,22 @@ fn sufficient_msrv() {
132112
_ = Duration::from_hours(18824455811688);
133113
//~^ duration_suboptimal_units
134114
}
115+
116+
fn issue16532() {
117+
// Literals with small promoted values should not lint (issue #16532)
118+
let dur = Duration::from_secs(60);
119+
let dur = Duration::from_secs(180);
120+
let dur = Duration::from_secs(180);
121+
let dur = Duration::from_millis(5_000);
122+
let dur = Duration::from_millis(1_000);
123+
let dur = Duration::from_secs(3_600);
124+
let dur = Duration::from_secs(600);
125+
126+
// Literals with larger promoted values should lint
127+
let dur = Duration::from_secs(20);
128+
//~^ duration_suboptimal_units
129+
let dur = Duration::from_hours(12);
130+
//~^ duration_suboptimal_units
131+
let dur = Duration::from_mins(11);
132+
//~^ duration_suboptimal_units
133+
}

tests/ui/duration_suboptimal_units.rs

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::time::Duration;
66
const SIXTY: u64 = 60;
77

88
macro_rules! mac {
9-
(slow_rythm) => {
9+
(slow_rhythm) => {
1010
60 * 60
1111
};
1212
(duration) => {
@@ -23,26 +23,6 @@ fn main() {
2323
let dur = Duration::from_secs(42);
2424
let dur = Duration::from_hours(3);
2525

26-
let dur = Duration::from_secs(59 + 1);
27-
//~^ duration_suboptimal_units
28-
29-
// Literals with small promoted values should not lint (issue #16532)
30-
let dur = Duration::from_secs(60);
31-
let dur = Duration::from_secs(180);
32-
let dur = Duration::from_secs(180);
33-
let dur = Duration::from_millis(5_000);
34-
let dur = Duration::from_millis(1_000);
35-
let dur = Duration::from_secs(3_600);
36-
let dur = Duration::from_secs(600);
37-
38-
// Literals with larger promoted values should lint
39-
let dur = Duration::from_millis(20_000);
40-
//~^ duration_suboptimal_units
41-
let dur = Duration::from_mins(720);
42-
//~^ duration_suboptimal_units
43-
let dur = Duration::from_secs(660);
44-
//~^ duration_suboptimal_units
45-
4626
let dur = Duration::from_secs(60 * 3);
4727
//~^ duration_suboptimal_units
4828
let dur = Duration::from_secs(10 * 60);
@@ -99,7 +79,7 @@ fn main() {
9979
let dur = proc_macros::external! { Duration::from_secs(60 * 60) };
10080

10181
// We don't lint values coming from macros
102-
let dur = Duration::from_secs(mac!(slow_rythm));
82+
let dur = Duration::from_secs(mac!(slow_rhythm));
10383
}
10484

10585
mod my_duration {
@@ -132,3 +112,22 @@ fn sufficient_msrv() {
132112
_ = Duration::from_secs(67_768_040_922_076_800);
133113
//~^ duration_suboptimal_units
134114
}
115+
116+
fn issue16532() {
117+
// Literals with small promoted values should not lint (issue #16532)
118+
let dur = Duration::from_secs(60);
119+
let dur = Duration::from_secs(180);
120+
let dur = Duration::from_secs(180);
121+
let dur = Duration::from_millis(5_000);
122+
let dur = Duration::from_millis(1_000);
123+
let dur = Duration::from_secs(3_600);
124+
let dur = Duration::from_secs(600);
125+
126+
// Literals with larger promoted values should lint
127+
let dur = Duration::from_millis(20_000);
128+
//~^ duration_suboptimal_units
129+
let dur = Duration::from_mins(720);
130+
//~^ duration_suboptimal_units
131+
let dur = Duration::from_secs(660);
132+
//~^ duration_suboptimal_units
133+
}

tests/ui/duration_suboptimal_units.stderr

Lines changed: 50 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,19 @@
11
error: constructing a `Duration` using a smaller unit when a larger unit would be more readable
22
--> tests/ui/duration_suboptimal_units.rs:26:15
33
|
4-
LL | let dur = Duration::from_secs(59 + 1);
4+
LL | let dur = Duration::from_secs(60 * 3);
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
77
= note: `-D clippy::duration-suboptimal-units` implied by `-D warnings`
88
= help: to override `-D warnings` add `#[allow(clippy::duration_suboptimal_units)]`
99
help: try using from_mins
1010
|
11-
LL - let dur = Duration::from_secs(59 + 1);
12-
LL + let dur = Duration::from_mins(1);
13-
|
14-
15-
error: constructing a `Duration` using a smaller unit when a larger unit would be more readable
16-
--> tests/ui/duration_suboptimal_units.rs:39:15
17-
|
18-
LL | let dur = Duration::from_millis(20_000);
19-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
20-
|
21-
help: try using from_secs
22-
|
23-
LL - let dur = Duration::from_millis(20_000);
24-
LL + let dur = Duration::from_secs(20);
25-
|
26-
27-
error: constructing a `Duration` using a smaller unit when a larger unit would be more readable
28-
--> tests/ui/duration_suboptimal_units.rs:41:15
29-
|
30-
LL | let dur = Duration::from_mins(720);
31-
| ^^^^^^^^^^^^^^^^^^^^^^^^
32-
|
33-
help: try using from_hours
34-
|
35-
LL - let dur = Duration::from_mins(720);
36-
LL + let dur = Duration::from_hours(12);
37-
|
38-
39-
error: constructing a `Duration` using a smaller unit when a larger unit would be more readable
40-
--> tests/ui/duration_suboptimal_units.rs:43:15
41-
|
42-
LL | let dur = Duration::from_secs(660);
43-
| ^^^^^^^^^^^^^^^^^^^^^^^^
44-
|
45-
help: try using from_mins
46-
|
47-
LL - let dur = Duration::from_secs(660);
48-
LL + let dur = Duration::from_mins(11);
49-
|
50-
51-
error: constructing a `Duration` using a smaller unit when a larger unit would be more readable
52-
--> tests/ui/duration_suboptimal_units.rs:46:15
53-
|
54-
LL | let dur = Duration::from_secs(60 * 3);
55-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
56-
|
57-
help: try using from_mins
58-
|
5911
LL - let dur = Duration::from_secs(60 * 3);
6012
LL + let dur = Duration::from_mins(3);
6113
|
6214

6315
error: constructing a `Duration` using a smaller unit when a larger unit would be more readable
64-
--> tests/ui/duration_suboptimal_units.rs:48:15
16+
--> tests/ui/duration_suboptimal_units.rs:28:15
6517
|
6618
LL | let dur = Duration::from_secs(10 * 60);
6719
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -73,7 +25,7 @@ LL + let dur = Duration::from_mins(10);
7325
|
7426

7527
error: constructing a `Duration` using a smaller unit when a larger unit would be more readable
76-
--> tests/ui/duration_suboptimal_units.rs:50:15
28+
--> tests/ui/duration_suboptimal_units.rs:30:15
7729
|
7830
LL | let dur = Duration::from_mins(24 * 60);
7931
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -85,7 +37,7 @@ LL + let dur = Duration::from_hours(24);
8537
|
8638

8739
error: constructing a `Duration` using a smaller unit when a larger unit would be more readable
88-
--> tests/ui/duration_suboptimal_units.rs:52:15
40+
--> tests/ui/duration_suboptimal_units.rs:32:15
8941
|
9042
LL | let dur = Duration::from_millis(5 * 1000);
9143
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -97,7 +49,7 @@ LL + let dur = Duration::from_secs(5);
9749
|
9850

9951
error: constructing a `Duration` using a smaller unit when a larger unit would be more readable
100-
--> tests/ui/duration_suboptimal_units.rs:54:15
52+
--> tests/ui/duration_suboptimal_units.rs:34:15
10153
|
10254
LL | let dur = Duration::from_nanos(13 * 60 * 60 * 1_000 * 1_000 * 1_000);
10355
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -109,7 +61,7 @@ LL + let dur = Duration::from_hours(13);
10961
|
11062

11163
error: constructing a `Duration` using a smaller unit when a larger unit would be more readable
112-
--> tests/ui/duration_suboptimal_units.rs:66:19
64+
--> tests/ui/duration_suboptimal_units.rs:46:19
11365
|
11466
LL | let dur = Duration::from_millis(1000 * 5);
11567
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -121,7 +73,7 @@ LL + let dur = Duration::from_secs(5);
12173
|
12274

12375
error: constructing a `Duration` using a smaller unit when a larger unit would be more readable
124-
--> tests/ui/duration_suboptimal_units.rs:68:19
76+
--> tests/ui/duration_suboptimal_units.rs:48:19
12577
|
12678
LL | let dur = Duration::from_millis(11000);
12779
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -133,7 +85,7 @@ LL + let dur = Duration::from_secs(11);
13385
|
13486

13587
error: constructing a `Duration` using a smaller unit when a larger unit would be more readable
136-
--> tests/ui/duration_suboptimal_units.rs:73:19
88+
--> tests/ui/duration_suboptimal_units.rs:53:19
13789
|
13890
LL | let dur = Duration::from_secs(39600);
13991
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -145,7 +97,7 @@ LL + let dur = Duration::from_hours(11);
14597
|
14698

14799
error: constructing a `Duration` using a smaller unit when a larger unit would be more readable
148-
--> tests/ui/duration_suboptimal_units.rs:75:19
100+
--> tests/ui/duration_suboptimal_units.rs:55:19
149101
|
150102
LL | let dur = Duration::from_secs(3 * 60);
151103
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -157,7 +109,7 @@ LL + let dur = Duration::from_mins(3);
157109
|
158110

159111
error: constructing a `Duration` using a smaller unit when a larger unit would be more readable
160-
--> tests/ui/duration_suboptimal_units.rs:77:19
112+
--> tests/ui/duration_suboptimal_units.rs:57:19
161113
|
162114
LL | let dur = Duration::from_mins(24 * 60);
163115
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -169,7 +121,7 @@ LL + let dur = Duration::from_hours(24);
169121
|
170122

171123
error: constructing a `Duration` using a smaller unit when a larger unit would be more readable
172-
--> tests/ui/duration_suboptimal_units.rs:84:5
124+
--> tests/ui/duration_suboptimal_units.rs:64:5
173125
|
174126
LL | std::time::Duration::from_secs(12 * 60);
175127
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -181,7 +133,7 @@ LL + std::time::Duration::from_mins(12);
181133
|
182134

183135
error: constructing a `Duration` using a smaller unit when a larger unit would be more readable
184-
--> tests/ui/duration_suboptimal_units.rs:89:16
136+
--> tests/ui/duration_suboptimal_units.rs:69:16
185137
|
186138
LL | assert_eq!(Duration::from_secs(60 * 60), Duration::from_mins(6));
187139
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -209,7 +161,7 @@ LL + Duration::from_mins(5)
209161
|
210162

211163
error: constructing a `Duration` using a smaller unit when a larger unit would be more readable
212-
--> tests/ui/duration_suboptimal_units.rs:132:9
164+
--> tests/ui/duration_suboptimal_units.rs:112:9
213165
|
214166
LL | _ = Duration::from_secs(67_768_040_922_076_800);
215167
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -220,5 +172,41 @@ LL - _ = Duration::from_secs(67_768_040_922_076_800);
220172
LL + _ = Duration::from_hours(18824455811688);
221173
|
222174

223-
error: aborting due to 18 previous errors
175+
error: constructing a `Duration` using a smaller unit when a larger unit would be more readable
176+
--> tests/ui/duration_suboptimal_units.rs:127:15
177+
|
178+
LL | let dur = Duration::from_millis(20_000);
179+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
180+
|
181+
help: try using from_secs
182+
|
183+
LL - let dur = Duration::from_millis(20_000);
184+
LL + let dur = Duration::from_secs(20);
185+
|
186+
187+
error: constructing a `Duration` using a smaller unit when a larger unit would be more readable
188+
--> tests/ui/duration_suboptimal_units.rs:129:15
189+
|
190+
LL | let dur = Duration::from_mins(720);
191+
| ^^^^^^^^^^^^^^^^^^^^^^^^
192+
|
193+
help: try using from_hours
194+
|
195+
LL - let dur = Duration::from_mins(720);
196+
LL + let dur = Duration::from_hours(12);
197+
|
198+
199+
error: constructing a `Duration` using a smaller unit when a larger unit would be more readable
200+
--> tests/ui/duration_suboptimal_units.rs:131:15
201+
|
202+
LL | let dur = Duration::from_secs(660);
203+
| ^^^^^^^^^^^^^^^^^^^^^^^^
204+
|
205+
help: try using from_mins
206+
|
207+
LL - let dur = Duration::from_secs(660);
208+
LL + let dur = Duration::from_mins(11);
209+
|
210+
211+
error: aborting due to 17 previous errors
224212

0 commit comments

Comments
 (0)