Skip to content

Commit 8dcdfc0

Browse files
Sockketstellar
authored andcommitted
Fix cppcoreguidelines-init-variables by removing the enum FixIt, and add support for initialization check of scoped enum
In C++, the enumeration is never Integer, and the enumeration condition judgment is added to avoid compiling errors when it is initialized to an integer. Add support for initialization check of scope enum. As the following case show, clang-tidy will give a wrong automatic fix: enum Color {Red, Green, Blue}; enum class Gender {Male, Female}; void func() { Color color; // Color color = 0; <--- fix bug Gender gender; // <--- no warning } Reviewd By: aaron.ballman, whisperity Differential Revision: http://reviews.llvm.org/D106431 (cherry picked from commit 4a097ef)
1 parent 20eced2 commit 8dcdfc0

File tree

4 files changed

+66
-7
lines changed

4 files changed

+66
-7
lines changed

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,12 @@ void InitVariablesCheck::check(const MatchFinder::MatchResult &Result) {
7878
return;
7979

8080
QualType TypePtr = MatchedDecl->getType();
81-
const char *InitializationString = nullptr;
81+
llvm::Optional<const char *> InitializationString = llvm::None;
8282
bool AddMathInclude = false;
8383

84-
if (TypePtr->isIntegerType())
84+
if (TypePtr->isEnumeralType())
85+
InitializationString = nullptr;
86+
else if (TypePtr->isIntegerType())
8587
InitializationString = " = 0";
8688
else if (TypePtr->isFloatingType()) {
8789
InitializationString = " = NAN";
@@ -96,11 +98,12 @@ void InitVariablesCheck::check(const MatchFinder::MatchResult &Result) {
9698
if (InitializationString) {
9799
auto Diagnostic =
98100
diag(MatchedDecl->getLocation(), "variable %0 is not initialized")
99-
<< MatchedDecl
100-
<< FixItHint::CreateInsertion(
101-
MatchedDecl->getLocation().getLocWithOffset(
102-
MatchedDecl->getName().size()),
103-
InitializationString);
101+
<< MatchedDecl;
102+
if (*InitializationString != nullptr)
103+
Diagnostic << FixItHint::CreateInsertion(
104+
MatchedDecl->getLocation().getLocWithOffset(
105+
MatchedDecl->getName().size()),
106+
*InitializationString);
104107
if (AddMathInclude) {
105108
Diagnostic << IncludeInserter.createIncludeInsertion(
106109
Source.getFileID(MatchedDecl->getBeginLoc()), MathHeader);

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,22 @@ Changes in existing checks
151151

152152
Added an option to choose the set of allowed functions.
153153

154+
- Improved :doc:`cppcoreguidelines-init-variables
155+
<clang-tidy/checks/cppcoreguidelines-init-variables>` check.
156+
157+
Removed generating fixes for enums because the code generated was broken,
158+
trying to initialize the enum from an integer.
159+
160+
The check now also warns for uninitialized scoped enums.
161+
154162
- Improved :doc:`readability-uniqueptr-delete-release
155163
<clang-tidy/checks/readability-uniqueptr-delete-release>` check.
156164

157165
Added an option to choose whether to refactor by calling the ``reset`` member
158166
function or assignment to ``nullptr``.
159167
Added support for pointers to ``std::unique_ptr``.
160168

169+
161170
Removed checks
162171
^^^^^^^^^^^^^^
163172

clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines-init-variables.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,21 @@ Would be rewritten to look like this:
3737
// Rest of the function.
3838
}
3939

40+
It warns for the uninitialized enum case, but without a FixIt:
41+
42+
.. code-block:: c++
43+
44+
enum A {A1, A2, A3};
45+
enum A_c : char { A_c1, A_c2, A_c3 };
46+
enum class B { B1, B2, B3 };
47+
enum class B_i : int { B_i1, B_i2, B_i3 };
48+
void function() {
49+
A a; // Warning: variable 'a' is not initialized
50+
A_c a_c; // Warning: variable 'a_c' is not initialized
51+
B b; // Warning: variable 'b' is not initialized
52+
B_i b_i; // Warning: variable 'b_i' is not initialized
53+
}
54+
4055
Options
4156
-------
4257

clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-init-variables.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,35 @@ void catch_variable_decl() {
9292
} catch (int X) {
9393
}
9494
}
95+
96+
enum Color { Red,
97+
Green,
98+
Blue };
99+
100+
enum Car { Benz,
101+
BMW = 20,
102+
Audi = BMW + 2 };
103+
104+
enum Gender : char { Male,
105+
Female };
106+
107+
enum class Direction { Up,
108+
Down,
109+
Left,
110+
Right };
111+
112+
enum class Fruit : int { Apple,
113+
Orange };
114+
115+
void uninitialized_enum() {
116+
Color color;
117+
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: variable 'color' is not initialized [cppcoreguidelines-init-variables]
118+
Car car;
119+
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: variable 'car' is not initialized [cppcoreguidelines-init-variables]
120+
Gender gender;
121+
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: variable 'gender' is not initialized [cppcoreguidelines-init-variables]
122+
Direction direction;
123+
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: variable 'direction' is not initialized [cppcoreguidelines-init-variables]
124+
Fruit fruit;
125+
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: variable 'fruit' is not initialized [cppcoreguidelines-init-variables]
126+
}

0 commit comments

Comments
 (0)