1 .. title:: clang-tidy - abseil-duration-unnecessary-conversion
3 abseil-duration-unnecessary-conversion
4 ======================================
6 Finds and fixes cases where ``absl::Duration`` values are being converted to
7 numeric types and back again.
9 Floating-point examples:
13 // Original - Conversion to double and back again
15 absl::Duration d2 = absl::Seconds(absl::ToDoubleSeconds(d1));
17 // Suggestion - Remove unnecessary conversions
18 absl::Duration d2 = d1;
20 // Original - Division to convert to double and back again
21 absl::Duration d2 = absl::Seconds(absl::FDivDuration(d1, absl::Seconds(1)));
23 // Suggestion - Remove division and conversion
24 absl::Duration d2 = d1;
30 // Original - Conversion to integer and back again
32 absl::Duration d2 = absl::Hours(absl::ToInt64Hours(d1));
34 // Suggestion - Remove unnecessary conversions
35 absl::Duration d2 = d1;
37 // Original - Integer division followed by conversion
38 absl::Duration d2 = absl::Seconds(d1 / absl::Seconds(1));
40 // Suggestion - Remove division and conversion
41 absl::Duration d2 = d1;
43 Unwrapping scalar operations:
47 // Original - Multiplication by a scalar
49 absl::Duration d2 = absl::Seconds(absl::ToInt64Seconds(d1) * 2);
51 // Suggestion - Remove unnecessary conversion
52 absl::Duration d2 = d1 * 2;
54 Note: Converting to an integer and back to an ``absl::Duration`` might be a
55 truncating operation if the value is not aligned to the scale of conversion.
56 In the rare case where this is the intended result, callers should use
57 ``absl::Trunc`` to truncate explicitly.