-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[clang-tidy] swap cppcoreguidelines-narrowing-conversions and bugprone-narrowing-conversions #120245
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
HerrCai0907
merged 7 commits into
llvm:main
from
HerrCai0907:swap-bugprone-narrowing-conversions-cppcoreguidelines
Dec 29, 2024
Merged
[clang-tidy] swap cppcoreguidelines-narrowing-conversions and bugprone-narrowing-conversions #120245
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9e47698
[clang-tidy][NFC] swap cppcoreguidelines-narrowing-conversions and bu…
HerrCai0907 2003efa
format, swap alias table
HerrCai0907 f969b44
Merge remote-tracking branch 'origin/main' into swap-bugprone-narrowi…
HerrCai0907 593fa9c
add release note
HerrCai0907 8772716
fix doc
HerrCai0907 9ea4fba
Merge branch 'main' of github.com:llvm/llvm-project into swap-bugpron…
HerrCai0907 cc74dc6
fix
HerrCai0907 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 88 additions & 5 deletions
93
clang-tools-extra/docs/clang-tidy/checks/bugprone/narrowing-conversions.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,93 @@ | ||
.. title:: clang-tidy - bugprone-narrowing-conversions | ||
.. meta:: | ||
:http-equiv=refresh: 5;URL=../cppcoreguidelines/narrowing-conversions.html | ||
|
||
bugprone-narrowing-conversions | ||
============================== | ||
|
||
The bugprone-narrowing-conversions check is an alias, please see | ||
:doc:`cppcoreguidelines-narrowing-conversions <../cppcoreguidelines/narrowing-conversions>` | ||
for more information. | ||
`cppcoreguidelines-narrowing-conversions` redirects here as an alias for this check. | ||
|
||
Checks for silent narrowing conversions, e.g: ``int i = 0; i += 0.1;``. While | ||
the issue is obvious in this former example, it might not be so in the | ||
following: ``void MyClass::f(double d) { int_member_ += d; }``. | ||
|
||
We enforce only part of the guideline, more specifically, we flag narrowing conversions from: | ||
HerrCai0907 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
- an integer to a narrower integer (e.g. ``char`` to ``unsigned char``) | ||
if WarnOnIntegerNarrowingConversion Option is set, | ||
- an integer to a narrower floating-point (e.g. ``uint64_t`` to ``float``) | ||
if WarnOnIntegerToFloatingPointNarrowingConversion Option is set, | ||
- 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 with a narrowing conversions. | ||
For example: ``int i; i+= 0.1;``. | ||
|
||
|
||
Options | ||
------- | ||
|
||
.. option:: WarnOnIntegerNarrowingConversion | ||
|
||
When `true`, the check will warn on narrowing integer conversion | ||
(e.g. ``int`` to ``size_t``). `true` by default. | ||
|
||
.. option:: WarnOnIntegerToFloatingPointNarrowingConversion | ||
|
||
When `true`, the check will warn on narrowing integer to floating-point | ||
conversion (e.g. ``size_t`` to ``double``). `true` by default. | ||
|
||
.. option:: WarnOnFloatingPointNarrowingConversion | ||
|
||
When `true`, the check will warn on narrowing floating point conversion | ||
(e.g. ``double`` to ``float``). `true` by default. | ||
|
||
.. option:: WarnWithinTemplateInstantiation | ||
|
||
When `true`, the check will warn on narrowing conversions within template | ||
instantiations. `false` by default. | ||
|
||
.. option:: WarnOnEquivalentBitWidth | ||
|
||
When `true`, the check will warn on narrowing conversions that arise from | ||
casting between types of equivalent bit width. (e.g. | ||
`int n = uint(0);` or `long long n = double(0);`) `true` by default. | ||
|
||
.. option:: IgnoreConversionFromTypes | ||
|
||
Narrowing conversions from any type in this semicolon-separated list will be | ||
ignored. This may be useful to weed out commonly occurring, but less commonly | ||
problematic assignments such as `int n = std::vector<char>().size();` or | ||
`int n = std::difference(it1, it2);`. The default list is empty, but one | ||
suggested list for a legacy codebase would be | ||
`size_t;ptrdiff_t;size_type;difference_type`. | ||
|
||
.. option:: PedanticMode | ||
|
||
When `true`, 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;``). | ||
`false` 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. |
91 changes: 5 additions & 86 deletions
91
...-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/narrowing-conversions.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,95 +1,14 @@ | ||
.. title:: clang-tidy - cppcoreguidelines-narrowing-conversions | ||
.. meta:: | ||
:http-equiv=refresh: 5;URL=../cppcoreguidelines/narrowing-conversions.html | ||
|
||
cppcoreguidelines-narrowing-conversions | ||
======================================= | ||
|
||
Checks for silent narrowing conversions, e.g: ``int i = 0; i += 0.1;``. While | ||
the issue is obvious in this former example, it might not be so in the | ||
following: ``void MyClass::f(double d) { int_member_ += d; }``. | ||
|
||
This check implements `ES.46 | ||
<https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#es46-avoid-lossy-narrowing-truncating-arithmetic-conversions>`_ | ||
from the C++ Core Guidelines. | ||
|
||
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``) | ||
if WarnOnIntegerNarrowingConversion Option is set, | ||
- an integer to a narrower floating-point (e.g. ``uint64_t`` to ``float``) | ||
if WarnOnIntegerToFloatingPointNarrowingConversion Option is set, | ||
- 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 with a narrowing conversions. | ||
For example: ``int i; i+= 0.1;``. | ||
|
||
|
||
Options | ||
------- | ||
|
||
.. option:: WarnOnIntegerNarrowingConversion | ||
|
||
When `true`, the check will warn on narrowing integer conversion | ||
(e.g. ``int`` to ``size_t``). `true` by default. | ||
|
||
.. option:: WarnOnIntegerToFloatingPointNarrowingConversion | ||
|
||
When `true`, the check will warn on narrowing integer to floating-point | ||
conversion (e.g. ``size_t`` to ``double``). `true` by default. | ||
|
||
.. option:: WarnOnFloatingPointNarrowingConversion | ||
|
||
When `true`, the check will warn on narrowing floating point conversion | ||
(e.g. ``double`` to ``float``). `true` by default. | ||
|
||
.. option:: WarnWithinTemplateInstantiation | ||
|
||
When `true`, the check will warn on narrowing conversions within template | ||
instantiations. `false` by default. | ||
|
||
.. option:: WarnOnEquivalentBitWidth | ||
|
||
When `true`, the check will warn on narrowing conversions that arise from | ||
casting between types of equivalent bit width. (e.g. | ||
`int n = uint(0);` or `long long n = double(0);`) `true` by default. | ||
|
||
.. option:: IgnoreConversionFromTypes | ||
|
||
Narrowing conversions from any type in this semicolon-separated list will be | ||
ignored. This may be useful to weed out commonly occurring, but less commonly | ||
problematic assignments such as `int n = std::vector<char>().size();` or | ||
`int n = std::difference(it1, it2);`. The default list is empty, but one | ||
suggested list for a legacy codebase would be | ||
`size_t;ptrdiff_t;size_type;difference_type`. | ||
|
||
.. option:: PedanticMode | ||
|
||
When `true`, 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;``). | ||
`false` 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. | ||
The cppcoreguidelines-narrowing-conversions check is an alias, please see | ||
:doc:`bugprone-narrowing-conversions <../bugprone/narrowing-conversions>` | ||
for more information. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 10 additions & 10 deletions
20
...conversions-equivalentbitwidth-option.cpp → ...conversions-equivalentbitwidth-option.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,42 @@ | ||
// RUN: %check_clang_tidy -check-suffix=DEFAULT %s \ | ||
// RUN: cppcoreguidelines-narrowing-conversions %t -- | ||
// RUN: bugprone-narrowing-conversions %t -- | ||
|
||
// RUN: %check_clang_tidy -check-suffix=DISABLED %s \ | ||
// RUN: cppcoreguidelines-narrowing-conversions %t -- \ | ||
// RUN: bugprone-narrowing-conversions %t -- \ | ||
// RUN: -config='{CheckOptions: { \ | ||
// RUN: cppcoreguidelines-narrowing-conversions.WarnOnEquivalentBitWidth: 0}}' | ||
// RUN: bugprone-narrowing-conversions.WarnOnEquivalentBitWidth: 0}}' | ||
|
||
void narrowing_equivalent_bitwidth() { | ||
int i; | ||
unsigned int ui; | ||
i = ui; | ||
// CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:7: warning: narrowing conversion from 'unsigned int' to signed type 'int' is implementation-defined [cppcoreguidelines-narrowing-conversions] | ||
// CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:7: warning: narrowing conversion from 'unsigned int' to signed type 'int' is implementation-defined [bugprone-narrowing-conversions] | ||
// DISABLED: Warning disabled with WarnOnEquivalentBitWidth=0. | ||
|
||
float f; | ||
i = f; | ||
// CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:7: warning: narrowing conversion from 'float' to 'int' [cppcoreguidelines-narrowing-conversions] | ||
// CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:7: warning: narrowing conversion from 'float' to 'int' [bugprone-narrowing-conversions] | ||
// DISABLED: Warning disabled with WarnOnEquivalentBitWidth=0. | ||
|
||
f = i; | ||
// CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:7: warning: narrowing conversion from 'int' to 'float' [cppcoreguidelines-narrowing-conversions] | ||
// CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:7: warning: narrowing conversion from 'int' to 'float' [bugprone-narrowing-conversions] | ||
// DISABLED: Warning disabled with WarnOnEquivalentBitWidth=0. | ||
|
||
long long ll; | ||
double d; | ||
ll = d; | ||
// CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:8: warning: narrowing conversion from 'double' to 'long long' [cppcoreguidelines-narrowing-conversions] | ||
// CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:8: warning: narrowing conversion from 'double' to 'long long' [bugprone-narrowing-conversions] | ||
// DISABLED: Warning disabled with WarnOnEquivalentBitWidth=0. | ||
|
||
d = ll; | ||
// CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:7: warning: narrowing conversion from 'long long' to 'double' [cppcoreguidelines-narrowing-conversions] | ||
// CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:7: warning: narrowing conversion from 'long long' to 'double' [bugprone-narrowing-conversions] | ||
// DISABLED: Warning disabled with WarnOnEquivalentBitWidth=0. | ||
} | ||
|
||
void most_narrowing_is_not_ok() { | ||
int i; | ||
long long ui; | ||
i = ui; | ||
// CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:7: warning: narrowing conversion from 'long long' to signed type 'int' is implementation-defined [cppcoreguidelines-narrowing-conversions] | ||
// CHECK-MESSAGES-DISABLED: :[[@LINE-2]]:7: warning: narrowing conversion from 'long long' to signed type 'int' is implementation-defined [cppcoreguidelines-narrowing-conversions] | ||
// CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:7: warning: narrowing conversion from 'long long' to signed type 'int' is implementation-defined [bugprone-narrowing-conversions] | ||
// CHECK-MESSAGES-DISABLED: :[[@LINE-2]]:7: warning: narrowing conversion from 'long long' to signed type 'int' is implementation-defined [bugprone-narrowing-conversions] | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.