diff options
author | Hyrum Wright <hwright@google.com> | 2018-12-13 19:23:52 +0000 |
---|---|---|
committer | Hyrum Wright <hwright@google.com> | 2018-12-13 19:23:52 +0000 |
commit | d7486196796bf8ce16c55f95b8c47bd5112ddd04 (patch) | |
tree | db9b1164bf8584af905502f9632e82e0a249296e /clang-tools-extra/docs | |
parent | a8339fa59b225b07fd310bcc4f7a2e81278d04ad (diff) |
[clang-tidy] Add the abseil-duration-subtraction check
Summary:
This check uses the context of a subtraction expression as well as knowledge
about the Abseil Time types, to infer the type of the second operand of some
subtraction expressions in Duration conversions. For example:
absl::ToDoubleSeconds(duration) - foo
can become
absl::ToDoubleSeconds(duration - absl::Seconds(foo))
This ensures that time calculations are done in the proper domain, and also
makes it easier to further deduce the types of the second operands to these
expressions.
Reviewed By: JonasToth
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D55245
Diffstat (limited to 'clang-tools-extra/docs')
-rw-r--r-- | clang-tools-extra/docs/ReleaseNotes.rst | 6 | ||||
-rw-r--r-- | clang-tools-extra/docs/clang-tidy/checks/abseil-duration-subtraction.rst | 36 | ||||
-rw-r--r-- | clang-tools-extra/docs/clang-tidy/checks/list.rst | 1 |
3 files changed, 43 insertions, 0 deletions
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 9527cd49b1d..1976f788360 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -93,6 +93,12 @@ Improvements to clang-tidy Checks for cases where arguments to ``absl::Duration`` factory functions are scaled internally and could be changed to a different factory function. +- New :doc:`abseil-duration-subtraction + <clang-tidy/checks/abseil-duration-subtraction>` check. + + Checks for cases where subtraction should be performed in the + ``absl::Duration`` domain. + - New :doc:`abseil-faster-strsplit-delimiter <clang-tidy/checks/abseil-faster-strsplit-delimiter>` check. diff --git a/clang-tools-extra/docs/clang-tidy/checks/abseil-duration-subtraction.rst b/clang-tools-extra/docs/clang-tidy/checks/abseil-duration-subtraction.rst new file mode 100644 index 00000000000..884eeb2cbed --- /dev/null +++ b/clang-tools-extra/docs/clang-tidy/checks/abseil-duration-subtraction.rst @@ -0,0 +1,36 @@ +.. title:: clang-tidy - abseil-duration-subtraction + +abseil-duration-subtraction +=========================== + +Checks for cases where subtraction should be performed in the +``absl::Duration`` domain. When subtracting two values, and the first one is +known to be a conversion from ``absl::Duration``, we can infer that the second +should also be interpreted as an ``absl::Duration``, and make that inference +explicit. + +Examples: + +.. code-block:: c++ + + // Original - Subtraction in the double domain + double x; + absl::Duration d; + double result = absl::ToDoubleSeconds(d) - x; + + // Suggestion - Subtraction in the absl::Duration domain instead + double result = absl::ToDoubleSeconds(d - absl::Seconds(x)); + + + // Original - Subtraction of two Durations in the double domain + absl::Duration d1, d2; + double result = absl::ToDoubleSeconds(d1) - absl::ToDoubleSeconds(d2); + + // Suggestion - Subtraction in the absl::Duration domain instead + double result = absl::ToDoubleSeconds(d1 - d2); + +Note: As with other ``clang-tidy`` checks, it is possible that multiple fixes +may overlap (as in the case of nested expressions), so not all occurences can +be transformed in one run. In particular, this may occur for nested subtraction +expressions. Running ``clang-tidy`` multiple times will find and fix these +overlaps. diff --git a/clang-tools-extra/docs/clang-tidy/checks/list.rst b/clang-tools-extra/docs/clang-tidy/checks/list.rst index 7febb37720a..e45420823a0 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/list.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/list.rst @@ -8,6 +8,7 @@ Clang-Tidy Checks abseil-duration-division abseil-duration-factory-float abseil-duration-factory-scale + abseil-duration-subtraction abseil-faster-strsplit-delimiter abseil-no-internal-dependencies abseil-no-namespace |