diff options
author | Guillaume Chatelet <gchatelet@google.com> | 2018-11-26 16:25:55 +0000 |
---|---|---|
committer | Guillaume Chatelet <gchatelet@google.com> | 2018-11-26 16:25:55 +0000 |
commit | 895bd3c12ccac52f8211ba5173fa0ca985ac763a (patch) | |
tree | db22eb2920ccde40a53ba604a90e73fe9919a266 /docs | |
parent | eeaf26656494a4dbe2bcb86a785a91154e2b97b1 (diff) | |
download | clang-tools-extra-895bd3c12ccac52f8211ba5173fa0ca985ac763a.tar.gz |
[clang-tidy] Improving narrowing conversions
Summary:
Newly flagged narrowing conversions:
- integer to narrower signed integer (this is compiler implementation defined),
- integer - floating point narrowing conversions,
- floating point - integer narrowing conversions,
- constants with narrowing conversions (even in ternary operator).
Reviewers: hokein, alexfh, aaron.ballman, JonasToth
Reviewed By: aaron.ballman, JonasToth
Subscribers: lebedev.ri, courbet, nemanjai, xazax.hun, kbarton, cfe-commits
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D53488
git-svn-id: https://llvm.org/svn/llvm-project/clang-tools-extra/trunk@347570 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'docs')
-rw-r--r-- | docs/ReleaseNotes.rst | 8 | ||||
-rw-r--r-- | docs/clang-tidy/checks/cppcoreguidelines-narrowing-conversions.rst | 56 |
2 files changed, 57 insertions, 7 deletions
diff --git a/docs/ReleaseNotes.rst b/docs/ReleaseNotes.rst index 2773e235..c7229df7 100644 --- a/docs/ReleaseNotes.rst +++ b/docs/ReleaseNotes.rst @@ -217,6 +217,14 @@ Improvements to clang-tidy <clang-tidy/checks/readability-redundant-smartptr-get>` check does not warn about calls inside macros anymore by default. +- The :doc:`cppcoreguidelines-narrowing-conversions + <clang-tidy/checks/cppcoreguidelines-narrowing-conversions>` check now + detects more narrowing conversions: + - integer to narrower signed integer (this is compiler implementation defined), + - integer - floating point narrowing conversions, + - floating point - integer narrowing conversions, + - constants with narrowing conversions (even in ternary operator). + Improvements to include-fixer ----------------------------- diff --git a/docs/clang-tidy/checks/cppcoreguidelines-narrowing-conversions.rst b/docs/clang-tidy/checks/cppcoreguidelines-narrowing-conversions.rst index b1e82cae..8c9bce16 100644 --- a/docs/clang-tidy/checks/cppcoreguidelines-narrowing-conversions.rst +++ b/docs/clang-tidy/checks/cppcoreguidelines-narrowing-conversions.rst @@ -10,13 +10,55 @@ following: ``void MyClass::f(double d) { int_member_ += d; }``. This rule is part of the "Expressions and statements" profile of the C++ Core Guidelines, corresponding to rule ES.46. See -https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Res-narrowing. +https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#es46-avoid-lossy-narrowing-truncating-arithmetic-conversions. -We enforce only part of the guideline, more specifically, we flag: - - All floating-point to integer conversions that are not marked by an explicit - cast (c-style or ``static_cast``). For example: ``int i = 0; i += 0.1;``, +We enforce only part of the guideline, more specifically, we flag narrowing conversions from: + - an integer to a narrower integer (e.g. ``char`` to ``unsigned char``), + - an integer to a narrower floating-point (e.g. ``uint64_t`` to ``float``), + - a floating-point to an integer (e.g. ``double`` to ``int``), + - a floating-point to a narrower floating-point (e.g. ``double`` to ``float``) + if WarnOnFloatingPointNarrowingConversion Option is set. + +This check will flag: + - All narrowing conversions that are not marked by an explicit cast (c-style or + ``static_cast``). For example: ``int i = 0; i += 0.1;``, ``void f(int); f(0.1);``, - - All applications of binary operators where the left-hand-side is an integer - and the right-hand-size is a floating-point. For example: - ``int i; i+= 0.1;``. + - All applications of binary operators with a narrowing conversions. + For example: ``int i; i+= 0.1;``. + + + Options + ------- + + .. option:: WarnOnFloatingPointNarrowingConversion + + When non-zero, the check will warn on narrowing floating point conversion + (e.g. ``double`` to ``float``). `1` by default. + + .. option:: PedanticMode + + When non-zero, the check will warn on assigning a floating point constant + to an integer value even if the floating point value is exactly + representable in the destination type (e.g. ``int i = 1.0;``). + `0` by default. + +FAQ +--- + + - What does "narrowing conversion from 'int' to 'float'" mean? + +An IEEE754 Floating Point number can represent all integer values in the range +[-2^PrecisionBits, 2^PrecisionBits] where PrecisionBits is the number of bits in +the mantissa. + +For ``float`` this would be [-2^23, 2^23], where ``int`` can represent values in +the range [-2^31, 2^31-1]. + + - What does "implementation-defined" mean? +You may have encountered messages like "narrowing conversion from 'unsigned int' +to signed type 'int' is implementation-defined". +The C/C++ standard does not mandate two’s complement for signed integers, and so +the compiler is free to define what the semantics are for converting an unsigned +integer to signed integer. Clang's implementation uses the two’s complement +format. |