Skip to content

Commit 826631a

Browse files
committed
Move DurationSubsec into Operators lint pass
1 parent 3d9bee8 commit 826631a

File tree

7 files changed

+80
-80
lines changed

7 files changed

+80
-80
lines changed

clippy_lints/src/duration_subsec.rs

Lines changed: 0 additions & 75 deletions
This file was deleted.

clippy_lints/src/lib.register_all.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
5656
LintId::of(drop_forget_ref::FORGET_REF),
5757
LintId::of(drop_forget_ref::UNDROPPED_MANUALLY_DROPS),
5858
LintId::of(duplicate_mod::DUPLICATE_MOD),
59-
LintId::of(duration_subsec::DURATION_SUBSEC),
6059
LintId::of(entry::MAP_ENTRY),
6160
LintId::of(enum_clike::ENUM_CLIKE_UNPORTABLE_VARIANT),
6261
LintId::of(enum_variants::ENUM_VARIANT_NAMES),
@@ -253,6 +252,7 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
253252
LintId::of(operators::ASSIGN_OP_PATTERN),
254253
LintId::of(operators::BAD_BIT_MASK),
255254
LintId::of(operators::DOUBLE_COMPARISONS),
255+
LintId::of(operators::DURATION_SUBSEC),
256256
LintId::of(operators::INEFFECTIVE_BIT_MASK),
257257
LintId::of(operators::MISREFACTORED_ASSIGN_OP),
258258
LintId::of(option_env_unwrap::OPTION_ENV_UNWRAP),

clippy_lints/src/lib.register_complexity.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ store.register_group(true, "clippy::complexity", Some("clippy_complexity"), vec!
1111
LintId::of(casts::UNNECESSARY_CAST),
1212
LintId::of(derivable_impls::DERIVABLE_IMPLS),
1313
LintId::of(double_parens::DOUBLE_PARENS),
14-
LintId::of(duration_subsec::DURATION_SUBSEC),
1514
LintId::of(explicit_write::EXPLICIT_WRITE),
1615
LintId::of(format::USELESS_FORMAT),
1716
LintId::of(functions::TOO_MANY_ARGUMENTS),
@@ -69,6 +68,7 @@ store.register_group(true, "clippy::complexity", Some("clippy_complexity"), vec!
6968
LintId::of(no_effect::NO_EFFECT),
7069
LintId::of(no_effect::UNNECESSARY_OPERATION),
7170
LintId::of(operators::DOUBLE_COMPARISONS),
71+
LintId::of(operators::DURATION_SUBSEC),
7272
LintId::of(overflow_check_conditional::OVERFLOW_CHECK_CONDITIONAL),
7373
LintId::of(partialeq_ne_impl::PARTIALEQ_NE_IMPL),
7474
LintId::of(precedence::PRECEDENCE),

clippy_lints/src/lib.register_lints.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ store.register_lints(&[
128128
drop_forget_ref::FORGET_REF,
129129
drop_forget_ref::UNDROPPED_MANUALLY_DROPS,
130130
duplicate_mod::DUPLICATE_MOD,
131-
duration_subsec::DURATION_SUBSEC,
132131
else_if_without_else::ELSE_IF_WITHOUT_ELSE,
133132
empty_drop::EMPTY_DROP,
134133
empty_enum::EMPTY_ENUM,
@@ -425,6 +424,7 @@ store.register_lints(&[
425424
operators::ASSIGN_OP_PATTERN,
426425
operators::BAD_BIT_MASK,
427426
operators::DOUBLE_COMPARISONS,
427+
operators::DURATION_SUBSEC,
428428
operators::FLOAT_ARITHMETIC,
429429
operators::INEFFECTIVE_BIT_MASK,
430430
operators::INTEGER_ARITHMETIC,

clippy_lints/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,6 @@ mod doc_link_with_quotes;
210210
mod double_parens;
211211
mod drop_forget_ref;
212212
mod duplicate_mod;
213-
mod duration_subsec;
214213
mod else_if_without_else;
215214
mod empty_drop;
216215
mod empty_enum;
@@ -703,7 +702,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
703702
store.register_late_pass(|| Box::new(inherent_impl::MultipleInherentImpl));
704703
store.register_late_pass(|| Box::new(neg_cmp_op_on_partial_ord::NoNegCompOpForPartialOrd));
705704
store.register_late_pass(|| Box::new(unwrap::Unwrap));
706-
store.register_late_pass(|| Box::new(duration_subsec::DurationSubsec));
707705
store.register_late_pass(|| Box::new(indexing_slicing::IndexingSlicing));
708706
store.register_late_pass(|| Box::new(non_copy_const::NonCopyConst));
709707
store.register_late_pass(|| Box::new(ptr_offset_with_cast::PtrOffsetWithCast));
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
use clippy_utils::consts::{constant, Constant};
2+
use clippy_utils::diagnostics::span_lint_and_sugg;
3+
use clippy_utils::source::snippet_with_applicability;
4+
use clippy_utils::ty::is_type_diagnostic_item;
5+
use rustc_errors::Applicability;
6+
use rustc_hir::{BinOpKind, Expr, ExprKind};
7+
use rustc_lint::LateContext;
8+
use rustc_span::sym;
9+
10+
use super::DURATION_SUBSEC;
11+
12+
pub(crate) fn check<'tcx>(
13+
cx: &LateContext<'tcx>,
14+
expr: &'tcx Expr<'_>,
15+
op: BinOpKind,
16+
left: &'tcx Expr<'_>,
17+
right: &'tcx Expr<'_>,
18+
) {
19+
if op == BinOpKind::Div
20+
&& let ExprKind::MethodCall(method_path, [self_arg], _) = left.kind
21+
&& is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(self_arg).peel_refs(), sym::Duration)
22+
&& let Some((Constant::Int(divisor), _)) = constant(cx, cx.typeck_results(), right)
23+
{
24+
let suggested_fn = match (method_path.ident.as_str(), divisor) {
25+
("subsec_micros", 1_000) | ("subsec_nanos", 1_000_000) => "subsec_millis",
26+
("subsec_nanos", 1_000) => "subsec_micros",
27+
_ => return,
28+
};
29+
let mut applicability = Applicability::MachineApplicable;
30+
span_lint_and_sugg(
31+
cx,
32+
DURATION_SUBSEC,
33+
expr.span,
34+
&format!("calling `{}()` is more concise than this calculation", suggested_fn),
35+
"try",
36+
format!(
37+
"{}.{}()",
38+
snippet_with_applicability(cx, self_arg.span, "_", &mut applicability),
39+
suggested_fn
40+
),
41+
applicability,
42+
);
43+
}
44+
}

clippy_lints/src/operators/mod.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ mod absurd_extreme_comparisons;
66
mod assign_op_pattern;
77
mod bit_mask;
88
mod double_comparison;
9+
mod duration_subsec;
910
mod misrefactored_assign_op;
1011
mod numeric_arithmetic;
1112
mod 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+
274305
pub 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
]);
289321
impl 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

Comments
 (0)