Clippy lints / Pedantic
Clippy lint · Pedantic · checked with Clippy 0.1.99
clippy::duration_suboptimal_units: constructing a `Duration` using a smaller unit when a larger unit would be more readable
What it meansChecks for instances where a std::time::Duration is constructed using a smaller time unit when the value could be expressed more clearly using a larger unit. For literal values that would convert to 10 or fewer of the larger unit, this lint does not apply. It is in the pedantic group and off by default.
What you see
warning: constructing a `Duration` using a smaller unit when a larger unit would be more readable --> main.rs:5:11 | 5 | let dur = Duration::from_millis(50_000); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/stable/index.html#duration_suboptimal_units = note: `-W clippy::duration-suboptimal-units` implied by `-W clippy::pedantic` help: try using `Duration::from_secs` | 5 - let dur = Duration::from_millis(50_000); 5 + let dur = Duration::from_secs(50); |
Real output of Clippy 0.1.99 (Rust 1.99) on the example below. Names and line numbers in yours will differ.
Does it matter?
Using a smaller unit for a duration that is evenly divisible by a larger unit reduces readability. Readers have to mentally convert values, which can be error-prone and makes the code less clear.
Stricter lints with some false positives. Off by default; opt in with clippy::pedantic.
How to fix it
Clippy can fix this one for you: run cargo clippy --fix (add --allow-dirty if you have uncommitted changes). Review the diff before you commit.
Flagged
use std::time::Duration;
let dur = Duration::from_millis(50_000);
let dur = Duration::from_secs(1800);
let dur = Duration::from_mins(10 * 60);
Write this instead
use std::time::Duration;
let dur = Duration::from_secs(50);
let dur = Duration::from_mins(30);
let dur = Duration::from_hours(10);
Turn it off, or on
It is off by default. To use it, set it to warn or deny:
Whole crate (Cargo.toml)
[lints.clippy]
duration_suboptimal_units = "warn"In code
#![warn(clippy::duration_suboptimal_units)]One run
cargo clippy -- -W clippy::duration-suboptimal-unitsThe [lints] table needs Rust 1.74 or newer. In a workspace, put it under [workspace.lints.clippy] and add [lints] workspace = true to each member.
Clippy's own documentation
What it does
Checks for instances where a std::time::Duration is constructed using a smaller time unit
when the value could be expressed more clearly using a larger unit.
For literal values that would convert to 10 or fewer of the larger unit, this lint does not apply.
Why is this bad?
Using a smaller unit for a duration that is evenly divisible by a larger unit reduces readability. Readers have to mentally convert values, which can be error-prone and makes the code less clear.
Example
use std::time::Duration;
let dur = Duration::from_millis(50_000);
let dur = Duration::from_secs(1800);
let dur = Duration::from_mins(10 * 60);
Use instead:
use std::time::Duration;
let dur = Duration::from_secs(50);
let dur = Duration::from_mins(30);
let dur = Duration::from_hours(10);
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- too_many_linesChecks for functions with a large amount of lines
- missing_errors_docChecks the doc comments of publicly visible functions that return a Result type and warns if there is no # Errors section
- must_use_candidateChecks for public functions that have no #[must_use] attribute, but return something not already marked must-use, have no mutable arg and mutate no statics
- doc_comment_double_space_linebreaksDetects doc comments that use double spaces as hard line break, instead of backslash (\)
- doc_link_with_quotespossible intra-doc link using quotes instead of backticks
- doc_markdownitem in documentation is missing backticks
- elidable_lifetime_namesChecks for lifetime annotations which can be replaced with anonymous lifetimes ('_)
- enum_glob_useChecks for use Enum::*
- expl_impl_clone_on_copyChecks for explicit Clone implementations for Copy types
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::too_many_linesChecks for functions with a large amount of linesRead it next →Did this answer your question?