-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add duration_subsec lint #2839
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
Merged
Merged
Add duration_subsec lint #2839
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
use rustc::hir::*; | ||
use rustc::lint::*; | ||
use syntax::codemap::Spanned; | ||
|
||
use crate::consts::{constant, Constant}; | ||
use crate::utils::paths; | ||
use crate::utils::{match_type, snippet, span_lint_and_sugg, walk_ptrs_ty}; | ||
|
||
/// **What it does:** Checks for calculation of subsecond microseconds or milliseconds from | ||
/// `Duration::subsec_nanos()`. | ||
/// | ||
/// **Why is this bad?** It's more concise to call `Duration::subsec_micros()` or | ||
/// `Duration::subsec_millis()`. | ||
/// | ||
/// **Known problems:** None. | ||
/// | ||
/// **Example:** | ||
/// ```rust | ||
/// let dur = Duration::new(5, 0); | ||
/// let _micros = dur.subsec_nanos() / 1_000; | ||
/// let _millis = dur.subsec_nanos() / 1_000_000; | ||
/// ``` | ||
declare_clippy_lint! { | ||
pub DURATION_SUBSEC, | ||
complexity, | ||
"checks for `dur.subsec_nanos() / 1_000` or `dur.subsec_nanos() / 1_000_000`" | ||
} | ||
|
||
#[derive(Copy, Clone)] | ||
pub struct DurationSubsec; | ||
|
||
impl LintPass for DurationSubsec { | ||
fn get_lints(&self) -> LintArray { | ||
lint_array!(DURATION_SUBSEC) | ||
} | ||
} | ||
|
||
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for DurationSubsec { | ||
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) { | ||
if_chain! { | ||
if let ExprBinary(Spanned { node: BiDiv, .. }, ref left, ref right) = expr.node; | ||
if let ExprMethodCall(ref method_path, _ , ref args) = left.node; | ||
if method_path.name == "subsec_nanos"; | ||
if match_type(cx, walk_ptrs_ty(cx.tables.expr_ty(&args[0])), &paths::DURATION); | ||
if let Some((Constant::Int(divisor), _)) = constant(cx, cx.tables, right); | ||
then { | ||
let suggested_fn = match divisor { | ||
1_000 => "subsec_micros", | ||
1_000_000 => "subsec_millis", | ||
_ => return, | ||
}; | ||
|
||
span_lint_and_sugg( | ||
cx, | ||
DURATION_SUBSEC, | ||
expr.span, | ||
&format!("Calling `{}()` is more concise than this calculation", suggested_fn), | ||
"try", | ||
format!("{}.{}()", snippet(cx, args[0].span, "_"), suggested_fn), | ||
); | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#![warn(duration_subsec)] | ||
|
||
use std::time::Duration; | ||
|
||
fn main() { | ||
let dur = Duration::new(5, 0); | ||
|
||
let bad_micros = dur.subsec_nanos() / 1_000; | ||
let good_micros = dur.subsec_micros(); | ||
assert_eq!(bad_micros, good_micros); | ||
|
||
let bad_millis = dur.subsec_nanos() / 1_000_000; | ||
let good_millis = dur.subsec_millis(); | ||
assert_eq!(bad_millis, good_millis); | ||
|
||
// Handle refs | ||
let _ = (&dur).subsec_nanos() / 1_000; | ||
|
||
// Handle constants | ||
const NANOS_IN_MICRO: u32 = 1_000; | ||
let _ = dur.subsec_nanos() / NANOS_IN_MICRO; | ||
|
||
// Other literals aren't linted | ||
let _ = dur.subsec_nanos() / 699; | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
error: Calling `subsec_micros()` is more concise than this calculation | ||
--> $DIR/duration_subsec.rs:8:22 | ||
| | ||
8 | let bad_micros = dur.subsec_nanos() / 1_000; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `dur.subsec_micros()` | ||
| | ||
= note: `-D duration-subsec` implied by `-D warnings` | ||
|
||
error: Calling `subsec_millis()` is more concise than this calculation | ||
--> $DIR/duration_subsec.rs:12:22 | ||
| | ||
12 | let bad_millis = dur.subsec_nanos() / 1_000_000; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `dur.subsec_millis()` | ||
|
||
error: Calling `subsec_micros()` is more concise than this calculation | ||
--> $DIR/duration_subsec.rs:17:13 | ||
| | ||
17 | let _ = (&dur).subsec_nanos() / 1_000; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `(&dur).subsec_micros()` | ||
|
||
error: Calling `subsec_micros()` is more concise than this calculation | ||
--> $DIR/duration_subsec.rs:21:13 | ||
| | ||
21 | let _ = dur.subsec_nanos() / NANOS_IN_MICRO; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `dur.subsec_micros()` | ||
|
||
error: aborting due to 4 previous errors | ||
|
Empty file.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we could also complain about
subsec_micros() / 1000
and similar other usages that have a dedicated method. You should be able to do this nicely by matching on(method_path.name.as_str(), divisor)
to check all combinations that we want to lint