Skip to content

Commit effce8a

Browse files
declanvkblyxyas
andcommitted
Apply suggestions from code review
- I applied about half of these from the UI - Made more parts of the example code visible - Added test on duration ref - Rebased over conflict - Moved to the `style` lint group Co-authored-by: Alejandra González <[email protected]>
1 parent 840858f commit effce8a

4 files changed

+29
-12
lines changed

clippy_lints/src/duration_to_float_precision_loss.rs

+11-10
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use rustc_hir::{BinOpKind, Expr, ExprKind};
88
use rustc_lint::{LateContext, LateLintPass, LintContext};
99
use rustc_middle::lint::in_external_macro;
1010
use rustc_middle::ty::{self, FloatTy};
11+
use rustc_session::impl_lint_pass;
1112
use rustc_span::{sym, Span};
1213

1314
declare_clippy_lint! {
@@ -16,50 +17,50 @@ declare_clippy_lint! {
1617
/// precision is lost.
1718
///
1819
/// ### Why is this bad?
19-
/// This can be bad if the user wanted to retain the full precision of the duration.
20+
/// Retaining the full precision of a duration is usually desired.
2021
///
2122
/// ### Example
2223
/// ```no_run
2324
/// # use std::time::Duration;
24-
/// # let duration = Duration::from_nanos(1234500000);
25+
/// let duration = Duration::from_nanos(1234500000);
2526
/// let _ = duration.as_millis() as f64;
2627
/// ```
2728
///
2829
/// Use instead:
2930
///
3031
/// ```no_run
3132
/// # use std::time::Duration;
32-
/// # let duration = Duration::from_nanos(1234500000);
33+
/// let duration = Duration::from_nanos(1234500000);
3334
/// let _ = duration.as_secs_f64() * 1000.0;
3435
/// ```
3536
///
3637
/// Another motivating example happens when calculating number of seconds as a float with millisecond precision:
3738
///
3839
/// ```no_run
3940
/// # use std::time::Duration;
40-
/// # let duration = Duration::from_nanos(1234500000);
41+
/// let duration = Duration::from_nanos(1234500000);
4142
/// let _ = duration.as_millis() as f64 / 1000.0;
4243
/// ```
4344
///
4445
/// Use instead:
4546
///
4647
/// ```no_run
4748
/// # use std::time::Duration;
48-
/// # let duration = Duration::from_nanos(1234500000);
49+
/// let duration = Duration::from_nanos(1234500000);
4950
/// let _ = duration.as_secs_f64();
5051
/// ```
51-
#[clippy::version = "1.78.0"]
52+
#[clippy::version = "1.79.0"]
5253
pub DURATION_TO_FLOAT_PRECISION_LOSS,
53-
nursery,
54+
style,
5455
"conversion from duration to float that cause loss of precision"
5556
}
5657

5758
/// This struct implements the logic needed to apply the lint
5859
#[derive(Debug)]
5960
pub struct DurationToFloatPrecisionLoss {
60-
// This vector is used to prevent applying the lint to a sub-expression
61+
/// This vector is used to prevent applying the lint to a sub-expression
6162
lint_applications: Vec<Span>,
62-
// `as_secs_f64` isn't applicable until 1.38.0
63+
/// `as_secs_f64` isn't applicable until 1.38.0
6364
msrv: Msrv,
6465
}
6566

@@ -84,7 +85,7 @@ impl DurationToFloatPrecisionLoss {
8485
}
8586
}
8687

87-
rustc_session::impl_lint_pass!(DurationToFloatPrecisionLoss => [DURATION_TO_FLOAT_PRECISION_LOSS]);
88+
impl_lint_pass!(DurationToFloatPrecisionLoss => [DURATION_TO_FLOAT_PRECISION_LOSS]);
8889

8990
impl<'tcx> LateLintPass<'tcx> for DurationToFloatPrecisionLoss {
9091
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {

tests/ui/duration_to_float_precision_loss.fixed

+5
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ fn main() {
3535
let float_micros = DURATION.as_nanos() as f64 / 1_000.0;
3636

3737
let float_millis = DURATION.as_nanos() as f64 / 1_000_000.0;
38+
39+
{
40+
let dur = &&DURATION;
41+
let float_millis = dur.as_secs_f64();
42+
}
3843
}
3944

4045
#[clippy::msrv = "1.37"]

tests/ui/duration_to_float_precision_loss.rs

+5
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ fn main() {
3535
let float_micros = DURATION.as_nanos() as f64 / 1_000.0;
3636

3737
let float_millis = DURATION.as_nanos() as f64 / 1_000_000.0;
38+
39+
{
40+
let dur = &&DURATION;
41+
let float_millis = dur.as_millis() as f64 / 1_000.0;
42+
}
3843
}
3944

4045
#[clippy::msrv = "1.37"]

tests/ui/duration_to_float_precision_loss.stderr

+8-2
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,16 @@ LL | let float_micros = DURATION.as_micros() as f64;
5050
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `DURATION.as_secs_f64() * 1e6`
5151

5252
error: calling `as_secs_f64()` is more precise than this calculation
53-
--> tests/ui/duration_to_float_precision_loss.rs:47:22
53+
--> tests/ui/duration_to_float_precision_loss.rs:41:28
54+
|
55+
LL | let float_millis = dur.as_millis() as f64 / 1_000.0;
56+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `dur.as_secs_f64()`
57+
58+
error: calling `as_secs_f64()` is more precise than this calculation
59+
--> tests/ui/duration_to_float_precision_loss.rs:52:22
5460
|
5561
LL | let float_secs = DURATION.as_secs() as f64; // should trigger lint because MSRV is satisfied
5662
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `DURATION.as_secs_f64()`
5763

58-
error: aborting due to 9 previous errors
64+
error: aborting due to 10 previous errors
5965

0 commit comments

Comments
 (0)