@@ -6,6 +6,7 @@ mod absurd_extreme_comparisons;
66mod assign_op_pattern;
77mod bit_mask;
88mod double_comparison;
9+ mod duration_subsec;
910mod misrefactored_assign_op;
1011mod numeric_arithmetic;
1112mod verbose_bit_mask;
@@ -271,6 +272,36 @@ declare_clippy_lint! {
271272 "unnecessary double comparisons that can be simplified"
272273}
273274
275+ declare_clippy_lint ! {
276+ /// ### What it does
277+ /// Checks for calculation of subsecond microseconds or milliseconds
278+ /// from other `Duration` methods.
279+ ///
280+ /// ### Why is this bad?
281+ /// It's more concise to call `Duration::subsec_micros()` or
282+ /// `Duration::subsec_millis()` than to calculate them.
283+ ///
284+ /// ### Example
285+ /// ```rust
286+ /// # use std::time::Duration;
287+ /// # let duration = Duration::new(5, 0);
288+ /// let micros = duration.subsec_nanos() / 1_000;
289+ /// let millis = duration.subsec_nanos() / 1_000_000;
290+ /// ```
291+ ///
292+ /// Use instead:
293+ /// ```rust
294+ /// # use std::time::Duration;
295+ /// # let duration = Duration::new(5, 0);
296+ /// let micros = duration.subsec_micros();
297+ /// let millis = duration.subsec_millis();
298+ /// ```
299+ #[ clippy:: version = "pre 1.29.0" ]
300+ pub DURATION_SUBSEC ,
301+ complexity,
302+ "checks for calculation of subsecond microseconds or milliseconds"
303+ }
304+
274305pub struct Operators {
275306 arithmetic_context : numeric_arithmetic:: Context ,
276307 verbose_bit_mask_threshold : u64 ,
@@ -285,6 +316,7 @@ impl_lint_pass!(Operators => [
285316 INEFFECTIVE_BIT_MASK ,
286317 VERBOSE_BIT_MASK ,
287318 DOUBLE_COMPARISONS ,
319+ DURATION_SUBSEC ,
288320] ) ;
289321impl Operators {
290322 pub fn new ( verbose_bit_mask_threshold : u64 ) -> Self {
@@ -305,6 +337,7 @@ impl<'tcx> LateLintPass<'tcx> for Operators {
305337 bit_mask:: check ( cx, e, op. node , lhs, rhs) ;
306338 verbose_bit_mask:: check ( cx, e, op. node , lhs, rhs, self . verbose_bit_mask_threshold ) ;
307339 double_comparison:: check ( cx, op. node , lhs, rhs, e. span ) ;
340+ duration_subsec:: check ( cx, e, op. node , lhs, rhs) ;
308341 } ,
309342 ExprKind :: AssignOp ( op, lhs, rhs) => {
310343 self . arithmetic_context . check_binary ( cx, e, op. node , lhs, rhs) ;
0 commit comments