Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
est31 opened this issue Sep 20, 2020 · 4 comments
Open
Assignees
Labels
C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages good-first-issue These issues are a good way to get started with Clippy

Comments

@est31
Copy link
Member

est31 commented Sep 20, 2020

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();
@est31 est31 added the A-lint Area: New lints label Sep 20, 2020
@est31
Copy link
Member Author

est31 commented Sep 20, 2020

@ghost
Copy link

ghost commented Sep 22, 2020

I think duration_subsec should be renamed to manual_duration_calcs and extended to handle cases.

I think the minimum supported rust version is an issue an that needs to be handled globally for this project. I wouldn't worry about it while implementing this.

@flip1995 flip1995 added good-first-issue These issues are a good way to get started with Clippy hacktoberfest C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages and removed A-lint Area: New lints labels Oct 5, 2020
@himanoa
Copy link

himanoa commented Oct 26, 2020

Hi, Can I give it a try?

@llogiq
Copy link
Contributor

llogiq commented Oct 26, 2020

It's all yours!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages good-first-issue These issues are a good way to get started with Clippy
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants