rust-lang / rust-lang/rust-clippy

Manual Duration::{as_nanos,as_milis,as_secs_f64,as_secs_f32} implementation

Open
#6,068 4 comments 0 reactions 1 assignee View on GitHub

@himanoa is already working on this.

Since Oct 26, 2020.

C-enhancement good first issue
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

What it does

Recognize cases of manual re-implementation of the builtin as_nanos and as_secs_f64 and as_secs_f32 functions on Duration.

Categories
  • Kind: complexity, sometimes also correctness
Drawbacks

MSRV issues, implementing the suggestion might increase the MSRV.

Example
let nanos = diff.as_secs() * 1_000_000_000 + diff.subsec_nanos() as u64;
let milis = diff.as_secs() * 1_000 + diff.subsec_milis() as u64;
let secs_f64 = (diff.as_secs() as f64 * 1_000.0 + diff.subsec_milis() as f64) / 1000.0;
let secs_f64 = diff.as_secs() as f64 + diff.subsec_milis() as f64 / 1_000.0;
let secs_f64 = diff.as_secs() as f64 + diff.subsec_nanos() as f64 / 1_000_000_000.0;

Could be written as:

let nanos = diff.as_nanos() as u64;
let milis = diff.as_millis() as u64;
let secs_f64 = diff.as_secs_f64();
let secs_f64 = diff.as_secs_f64();
let secs_f64 = diff.as_secs_f64();

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.