Skip to content

Commit 9e47698

Browse files
committed
[clang-tidy][NFC] swap cppcoreguidelines-narrowing-conversions and bugprone-narrowing-conversions
According to llvm#116591. > Coding guidelines should "cherry-pick" (and posddsibly configure/harden/make more strict) base checks. We should move narrowing conversion to bugprone and keep alias in cppcoreguidelines
1 parent 8b02d80 commit 9e47698

20 files changed

+240
-238
lines changed

clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
#include "../ClangTidy.h"
1010
#include "../ClangTidyModule.h"
1111
#include "../ClangTidyModuleRegistry.h"
12-
#include "../cppcoreguidelines/NarrowingConversionsCheck.h"
1312
#include "ArgumentCommentCheck.h"
1413
#include "AssertSideEffectCheck.h"
1514
#include "AssignmentInIfConditionCheck.h"
1615
#include "BadSignalToKillThreadCheck.h"
1716
#include "BitwisePointerCastCheck.h"
1817
#include "BoolPointerImplicitConversionCheck.h"
1918
#include "BranchCloneCheck.h"
19+
#include "NarrowingConversionsCheck.h"
2020
#include "CastingThroughVoidCheck.h"
2121
#include "ChainedComparisonCheck.h"
2222
#include "ComparePointerToMemberVirtualFunctionCheck.h"
@@ -183,7 +183,7 @@ class BugproneModule : public ClangTidyModule {
183183
"bugprone-pointer-arithmetic-on-polymorphic-object");
184184
CheckFactories.registerCheck<RedundantBranchConditionCheck>(
185185
"bugprone-redundant-branch-condition");
186-
CheckFactories.registerCheck<cppcoreguidelines::NarrowingConversionsCheck>(
186+
CheckFactories.registerCheck<NarrowingConversionsCheck>(
187187
"bugprone-narrowing-conversions");
188188
CheckFactories.registerCheck<NoEscapeCheck>("bugprone-no-escape");
189189
CheckFactories.registerCheck<NonZeroEnumToBoolConversionCheck>(

clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ add_clang_library(clangTidyBugproneModule STATIC
4242
MultiLevelImplicitPointerConversionCheck.cpp
4343
MultipleNewInOneExpressionCheck.cpp
4444
MultipleStatementMacroCheck.cpp
45+
NarrowingConversionsCheck.cpp
4546
NoEscapeCheck.cpp
4647
NonZeroEnumToBoolConversionCheck.cpp
4748
NondeterministicPointerIterationOrderCheck.cpp

clang-tools-extra/clang-tidy/cppcoreguidelines/NarrowingConversionsCheck.cpp renamed to clang-tools-extra/clang-tidy/bugprone/NarrowingConversionsCheck.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
using namespace clang::ast_matchers;
2424

25-
namespace clang::tidy::cppcoreguidelines {
25+
namespace clang::tidy::bugprone {
2626

2727
namespace {
2828

clang-tools-extra/clang-tidy/cppcoreguidelines/NarrowingConversionsCheck.h renamed to clang-tools-extra/clang-tidy/bugprone/NarrowingConversionsCheck.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
#include "../ClangTidyCheck.h"
1313

14-
namespace clang::tidy::cppcoreguidelines {
14+
namespace clang::tidy::bugprone {
1515

1616
/// Checks for narrowing conversions, e.g:
1717
/// int i = 0;

clang-tools-extra/clang-tidy/cppcoreguidelines/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ add_clang_library(clangTidyCppCoreGuidelinesModule STATIC
1616
MacroUsageCheck.cpp
1717
MisleadingCaptureDefaultByValueCheck.cpp
1818
MissingStdForwardCheck.cpp
19-
NarrowingConversionsCheck.cpp
2019
NoMallocCheck.cpp
2120
NoSuspendWithLockCheck.cpp
2221
OwningMemoryCheck.cpp

clang-tools-extra/clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
#include "MacroUsageCheck.h"
3131
#include "MisleadingCaptureDefaultByValueCheck.h"
3232
#include "MissingStdForwardCheck.h"
33-
#include "NarrowingConversionsCheck.h"
33+
#include "../bugprone/NarrowingConversionsCheck.h"
3434
#include "NoMallocCheck.h"
3535
#include "NoSuspendWithLockCheck.h"
3636
#include "OwningMemoryCheck.h"
@@ -87,7 +87,7 @@ class CppCoreGuidelinesModule : public ClangTidyModule {
8787
"cppcoreguidelines-misleading-capture-default-by-value");
8888
CheckFactories.registerCheck<MissingStdForwardCheck>(
8989
"cppcoreguidelines-missing-std-forward");
90-
CheckFactories.registerCheck<NarrowingConversionsCheck>(
90+
CheckFactories.registerCheck<bugprone::NarrowingConversionsCheck>(
9191
"cppcoreguidelines-narrowing-conversions");
9292
CheckFactories.registerCheck<NoMallocCheck>("cppcoreguidelines-no-malloc");
9393
CheckFactories.registerCheck<NoSuspendWithLockCheck>(
Lines changed: 88 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,93 @@
11
.. title:: clang-tidy - bugprone-narrowing-conversions
2-
.. meta::
3-
:http-equiv=refresh: 5;URL=../cppcoreguidelines/narrowing-conversions.html
42

53
bugprone-narrowing-conversions
64
==============================
75

8-
The bugprone-narrowing-conversions check is an alias, please see
9-
:doc:`cppcoreguidelines-narrowing-conversions <../cppcoreguidelines/narrowing-conversions>`
10-
for more information.
6+
`cppcoreguidelines-narrowing-conversions` redirects here as an alias for this check.
7+
8+
Checks for silent narrowing conversions, e.g: ``int i = 0; i += 0.1;``. While
9+
the issue is obvious in this former example, it might not be so in the
10+
following: ``void MyClass::f(double d) { int_member_ += d; }``.
11+
12+
We enforce only part of the guideline, more specifically, we flag narrowing conversions from:
13+
- an integer to a narrower integer (e.g. ``char`` to ``unsigned char``)
14+
if WarnOnIntegerNarrowingConversion Option is set,
15+
- an integer to a narrower floating-point (e.g. ``uint64_t`` to ``float``)
16+
if WarnOnIntegerToFloatingPointNarrowingConversion Option is set,
17+
- a floating-point to an integer (e.g. ``double`` to ``int``),
18+
- a floating-point to a narrower floating-point (e.g. ``double`` to ``float``)
19+
if WarnOnFloatingPointNarrowingConversion Option is set.
20+
21+
This check will flag:
22+
- All narrowing conversions that are not marked by an explicit cast (c-style or
23+
``static_cast``). For example: ``int i = 0; i += 0.1;``,
24+
``void f(int); f(0.1);``,
25+
- All applications of binary operators with a narrowing conversions.
26+
For example: ``int i; i+= 0.1;``.
27+
28+
29+
Options
30+
-------
31+
32+
.. option:: WarnOnIntegerNarrowingConversion
33+
34+
When `true`, the check will warn on narrowing integer conversion
35+
(e.g. ``int`` to ``size_t``). `true` by default.
36+
37+
.. option:: WarnOnIntegerToFloatingPointNarrowingConversion
38+
39+
When `true`, the check will warn on narrowing integer to floating-point
40+
conversion (e.g. ``size_t`` to ``double``). `true` by default.
41+
42+
.. option:: WarnOnFloatingPointNarrowingConversion
43+
44+
When `true`, the check will warn on narrowing floating point conversion
45+
(e.g. ``double`` to ``float``). `true` by default.
46+
47+
.. option:: WarnWithinTemplateInstantiation
48+
49+
When `true`, the check will warn on narrowing conversions within template
50+
instantiations. `false` by default.
51+
52+
.. option:: WarnOnEquivalentBitWidth
53+
54+
When `true`, the check will warn on narrowing conversions that arise from
55+
casting between types of equivalent bit width. (e.g.
56+
`int n = uint(0);` or `long long n = double(0);`) `true` by default.
57+
58+
.. option:: IgnoreConversionFromTypes
59+
60+
Narrowing conversions from any type in this semicolon-separated list will be
61+
ignored. This may be useful to weed out commonly occurring, but less commonly
62+
problematic assignments such as `int n = std::vector<char>().size();` or
63+
`int n = std::difference(it1, it2);`. The default list is empty, but one
64+
suggested list for a legacy codebase would be
65+
`size_t;ptrdiff_t;size_type;difference_type`.
66+
67+
.. option:: PedanticMode
68+
69+
When `true`, the check will warn on assigning a floating point constant
70+
to an integer value even if the floating point value is exactly
71+
representable in the destination type (e.g. ``int i = 1.0;``).
72+
`false` by default.
73+
74+
FAQ
75+
---
76+
77+
- What does "narrowing conversion from 'int' to 'float'" mean?
78+
79+
An IEEE754 Floating Point number can represent all integer values in the range
80+
[-2^PrecisionBits, 2^PrecisionBits] where PrecisionBits is the number of bits in
81+
the mantissa.
82+
83+
For ``float`` this would be [-2^23, 2^23], where ``int`` can represent values in
84+
the range [-2^31, 2^31-1].
85+
86+
- What does "implementation-defined" mean?
87+
88+
You may have encountered messages like "narrowing conversion from 'unsigned int'
89+
to signed type 'int' is implementation-defined".
90+
The C/C++ standard does not mandate two's complement for signed integers, and so
91+
the compiler is free to define what the semantics are for converting an unsigned
92+
integer to signed integer. Clang's implementation uses the two's complement
93+
format.
Lines changed: 5 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,14 @@
11
.. title:: clang-tidy - cppcoreguidelines-narrowing-conversions
2+
.. meta::
3+
:http-equiv=refresh: 5;URL=../cppcoreguidelines/narrowing-conversions.html
24

35
cppcoreguidelines-narrowing-conversions
46
=======================================
57

6-
Checks for silent narrowing conversions, e.g: ``int i = 0; i += 0.1;``. While
7-
the issue is obvious in this former example, it might not be so in the
8-
following: ``void MyClass::f(double d) { int_member_ += d; }``.
9-
108
This check implements `ES.46
119
<https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#es46-avoid-lossy-narrowing-truncating-arithmetic-conversions>`_
1210
from the C++ Core Guidelines.
1311

14-
We enforce only part of the guideline, more specifically, we flag narrowing conversions from:
15-
- an integer to a narrower integer (e.g. ``char`` to ``unsigned char``)
16-
if WarnOnIntegerNarrowingConversion Option is set,
17-
- an integer to a narrower floating-point (e.g. ``uint64_t`` to ``float``)
18-
if WarnOnIntegerToFloatingPointNarrowingConversion Option is set,
19-
- a floating-point to an integer (e.g. ``double`` to ``int``),
20-
- a floating-point to a narrower floating-point (e.g. ``double`` to ``float``)
21-
if WarnOnFloatingPointNarrowingConversion Option is set.
22-
23-
This check will flag:
24-
- All narrowing conversions that are not marked by an explicit cast (c-style or
25-
``static_cast``). For example: ``int i = 0; i += 0.1;``,
26-
``void f(int); f(0.1);``,
27-
- All applications of binary operators with a narrowing conversions.
28-
For example: ``int i; i+= 0.1;``.
29-
30-
31-
Options
32-
-------
33-
34-
.. option:: WarnOnIntegerNarrowingConversion
35-
36-
When `true`, the check will warn on narrowing integer conversion
37-
(e.g. ``int`` to ``size_t``). `true` by default.
38-
39-
.. option:: WarnOnIntegerToFloatingPointNarrowingConversion
40-
41-
When `true`, the check will warn on narrowing integer to floating-point
42-
conversion (e.g. ``size_t`` to ``double``). `true` by default.
43-
44-
.. option:: WarnOnFloatingPointNarrowingConversion
45-
46-
When `true`, the check will warn on narrowing floating point conversion
47-
(e.g. ``double`` to ``float``). `true` by default.
48-
49-
.. option:: WarnWithinTemplateInstantiation
50-
51-
When `true`, the check will warn on narrowing conversions within template
52-
instantiations. `false` by default.
53-
54-
.. option:: WarnOnEquivalentBitWidth
55-
56-
When `true`, the check will warn on narrowing conversions that arise from
57-
casting between types of equivalent bit width. (e.g.
58-
`int n = uint(0);` or `long long n = double(0);`) `true` by default.
59-
60-
.. option:: IgnoreConversionFromTypes
61-
62-
Narrowing conversions from any type in this semicolon-separated list will be
63-
ignored. This may be useful to weed out commonly occurring, but less commonly
64-
problematic assignments such as `int n = std::vector<char>().size();` or
65-
`int n = std::difference(it1, it2);`. The default list is empty, but one
66-
suggested list for a legacy codebase would be
67-
`size_t;ptrdiff_t;size_type;difference_type`.
68-
69-
.. option:: PedanticMode
70-
71-
When `true`, the check will warn on assigning a floating point constant
72-
to an integer value even if the floating point value is exactly
73-
representable in the destination type (e.g. ``int i = 1.0;``).
74-
`false` by default.
75-
76-
FAQ
77-
---
78-
79-
- What does "narrowing conversion from 'int' to 'float'" mean?
80-
81-
An IEEE754 Floating Point number can represent all integer values in the range
82-
[-2^PrecisionBits, 2^PrecisionBits] where PrecisionBits is the number of bits in
83-
the mantissa.
84-
85-
For ``float`` this would be [-2^23, 2^23], where ``int`` can represent values in
86-
the range [-2^31, 2^31-1].
87-
88-
- What does "implementation-defined" mean?
89-
90-
You may have encountered messages like "narrowing conversion from 'unsigned int'
91-
to signed type 'int' is implementation-defined".
92-
The C/C++ standard does not mandate two's complement for signed integers, and so
93-
the compiler is free to define what the semantics are for converting an unsigned
94-
integer to signed integer. Clang's implementation uses the two's complement
95-
format.
12+
The cppcoreguidelines-narrowing-conversions check is an alias, please see
13+
:doc:`bugprone-narrowing-conversions <../bugprone/narrowing-conversions>`
14+
for more information.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %check_clang_tidy %s cppcoreguidelines-narrowing-conversions %t \
1+
// RUN: %check_clang_tidy %s bugprone-narrowing-conversions %t \
22
// RUN: -std=c++17 -- -target x86_64-unknown-linux
33

44
#define CHAR_BITS 8
@@ -31,7 +31,7 @@ struct CompleteBitfield {
3131
};
3232

3333
int example_warning(unsigned x) {
34-
// CHECK-MESSAGES: :[[@LINE+1]]:10: warning: narrowing conversion from 'unsigned int' to signed type 'int' is implementation-defined [cppcoreguidelines-narrowing-conversions]
34+
// CHECK-MESSAGES: :[[@LINE+1]]:10: warning: narrowing conversion from 'unsigned int' to signed type 'int' is implementation-defined [bugprone-narrowing-conversions]
3535
return x;
3636
}
3737

Original file line numberDiff line numberDiff line change
@@ -1,42 +1,42 @@
11
// RUN: %check_clang_tidy -check-suffix=DEFAULT %s \
2-
// RUN: cppcoreguidelines-narrowing-conversions %t --
2+
// RUN: bugprone-narrowing-conversions %t --
33

44
// RUN: %check_clang_tidy -check-suffix=DISABLED %s \
5-
// RUN: cppcoreguidelines-narrowing-conversions %t -- \
5+
// RUN: bugprone-narrowing-conversions %t -- \
66
// RUN: -config='{CheckOptions: { \
7-
// RUN: cppcoreguidelines-narrowing-conversions.WarnOnEquivalentBitWidth: 0}}'
7+
// RUN: bugprone-narrowing-conversions.WarnOnEquivalentBitWidth: 0}}'
88

99
void narrowing_equivalent_bitwidth() {
1010
int i;
1111
unsigned int ui;
1212
i = ui;
13-
// CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:7: warning: narrowing conversion from 'unsigned int' to signed type 'int' is implementation-defined [cppcoreguidelines-narrowing-conversions]
13+
// CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:7: warning: narrowing conversion from 'unsigned int' to signed type 'int' is implementation-defined [bugprone-narrowing-conversions]
1414
// DISABLED: Warning disabled with WarnOnEquivalentBitWidth=0.
1515

1616
float f;
1717
i = f;
18-
// CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:7: warning: narrowing conversion from 'float' to 'int' [cppcoreguidelines-narrowing-conversions]
18+
// CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:7: warning: narrowing conversion from 'float' to 'int' [bugprone-narrowing-conversions]
1919
// DISABLED: Warning disabled with WarnOnEquivalentBitWidth=0.
2020

2121
f = i;
22-
// CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:7: warning: narrowing conversion from 'int' to 'float' [cppcoreguidelines-narrowing-conversions]
22+
// CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:7: warning: narrowing conversion from 'int' to 'float' [bugprone-narrowing-conversions]
2323
// DISABLED: Warning disabled with WarnOnEquivalentBitWidth=0.
2424

2525
long long ll;
2626
double d;
2727
ll = d;
28-
// CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:8: warning: narrowing conversion from 'double' to 'long long' [cppcoreguidelines-narrowing-conversions]
28+
// CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:8: warning: narrowing conversion from 'double' to 'long long' [bugprone-narrowing-conversions]
2929
// DISABLED: Warning disabled with WarnOnEquivalentBitWidth=0.
3030

3131
d = ll;
32-
// CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:7: warning: narrowing conversion from 'long long' to 'double' [cppcoreguidelines-narrowing-conversions]
32+
// CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:7: warning: narrowing conversion from 'long long' to 'double' [bugprone-narrowing-conversions]
3333
// DISABLED: Warning disabled with WarnOnEquivalentBitWidth=0.
3434
}
3535

3636
void most_narrowing_is_not_ok() {
3737
int i;
3838
long long ui;
3939
i = ui;
40-
// CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:7: warning: narrowing conversion from 'long long' to signed type 'int' is implementation-defined [cppcoreguidelines-narrowing-conversions]
41-
// CHECK-MESSAGES-DISABLED: :[[@LINE-2]]:7: warning: narrowing conversion from 'long long' to signed type 'int' is implementation-defined [cppcoreguidelines-narrowing-conversions]
40+
// CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:7: warning: narrowing conversion from 'long long' to signed type 'int' is implementation-defined [bugprone-narrowing-conversions]
41+
// CHECK-MESSAGES-DISABLED: :[[@LINE-2]]:7: warning: narrowing conversion from 'long long' to signed type 'int' is implementation-defined [bugprone-narrowing-conversions]
4242
}

0 commit comments

Comments
 (0)