| 1 |
.. title:: clang-tidy - abseil-duration-subtraction |
| 2 |
|
| 3 |
abseil-duration-subtraction |
| 4 |
=========================== |
| 5 |
|
| 6 |
Checks for cases where subtraction should be performed in the |
| 7 |
``absl::Duration`` domain. When subtracting two values, and the first one is |
| 8 |
known to be a conversion from ``absl::Duration``, we can infer that the second |
| 9 |
should also be interpreted as an ``absl::Duration``, and make that inference |
| 10 |
explicit. |
| 11 |
|
| 12 |
Examples: |
| 13 |
|
| 14 |
.. code-block:: c++ |
| 15 |
|
| 16 |
// Original - Subtraction in the double domain |
| 17 |
double x; |
| 18 |
absl::Duration d; |
| 19 |
double result = absl::ToDoubleSeconds(d) - x; |
| 20 |
|
| 21 |
// Suggestion - Subtraction in the absl::Duration domain instead |
| 22 |
double result = absl::ToDoubleSeconds(d - absl::Seconds(x)); |
| 23 |
|
| 24 |
// Original - Subtraction of two Durations in the double domain |
| 25 |
absl::Duration d1, d2; |
| 26 |
double result = absl::ToDoubleSeconds(d1) - absl::ToDoubleSeconds(d2); |
| 27 |
|
| 28 |
// Suggestion - Subtraction in the absl::Duration domain instead |
| 29 |
double result = absl::ToDoubleSeconds(d1 - d2); |
| 30 |
|
| 31 |
|
| 32 |
Note: As with other ``clang-tidy`` checks, it is possible that multiple fixes |
| 33 |
may overlap (as in the case of nested expressions), so not all occurrences can |
| 34 |
be transformed in one run. In particular, this may occur for nested subtraction |
| 35 |
expressions. Running ``clang-tidy`` multiple times will find and fix these |
| 36 |
overlaps. |