58 lines · plain
1.. title:: clang-tidy - abseil-duration-unnecessary-conversion2 3abseil-duration-unnecessary-conversion4======================================5 6Finds and fixes cases where ``absl::Duration`` values are being converted to7numeric types and back again.8 9Floating-point examples:10 11.. code-block:: c++12 13 // Original - Conversion to double and back again14 absl::Duration d1;15 absl::Duration d2 = absl::Seconds(absl::ToDoubleSeconds(d1));16 17 // Suggestion - Remove unnecessary conversions18 absl::Duration d2 = d1;19 20 // Original - Division to convert to double and back again21 absl::Duration d2 = absl::Seconds(absl::FDivDuration(d1, absl::Seconds(1)));22 23 // Suggestion - Remove division and conversion24 absl::Duration d2 = d1;25 26Integer examples:27 28.. code-block:: c++29 30 // Original - Conversion to integer and back again31 absl::Duration d1;32 absl::Duration d2 = absl::Hours(absl::ToInt64Hours(d1));33 34 // Suggestion - Remove unnecessary conversions35 absl::Duration d2 = d1;36 37 // Original - Integer division followed by conversion38 absl::Duration d2 = absl::Seconds(d1 / absl::Seconds(1));39 40 // Suggestion - Remove division and conversion41 absl::Duration d2 = d1;42 43Unwrapping scalar operations:44 45.. code-block:: c++46 47 // Original - Multiplication by a scalar48 absl::Duration d1;49 absl::Duration d2 = absl::Seconds(absl::ToInt64Seconds(d1) * 2);50 51 // Suggestion - Remove unnecessary conversion52 absl::Duration d2 = d1 * 2;53 54Note: Converting to an integer and back to an ``absl::Duration`` might be a55truncating operation if the value is not aligned to the scale of conversion.56In the rare case where this is the intended result, callers should use57``absl::Trunc`` to truncate explicitly.58