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