[PATCH] D106431: [clang-tidy] Fix cppcoreguidelines-init-variables by removing the enum FixIt, and add support for initialization check of scoped enum.
gehry via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Wed Jul 28 01:06:48 PDT 2021
Sockke updated this revision to Diff 362291.
Sockke retitled this revision from "[clang-tidy] Fix cppcoreguidelines-init-variables with enum judgement" to "[clang-tidy] Fix cppcoreguidelines-init-variables by removing the enum FixIt, and add support for initialization check of scoped enum.".
Sockke edited the summary of this revision.
Sockke added a comment.
Update!
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D106431/new/
https://reviews.llvm.org/D106431
Files:
clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp
clang-tools-extra/docs/ReleaseNotes.rst
clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines-init-variables.rst
clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-init-variables.cpp
Index: clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-init-variables.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-init-variables.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-init-variables.cpp
@@ -92,3 +92,26 @@
} catch (int X) {
}
}
+
+enum Color { Red, Green, Blue };
+
+enum Car { Benz, BMW = 20, Audi = BMW + 2 };
+
+enum Gender : char { Male, Female };
+
+enum class Direction { Up, Down, Left, Right };
+
+enum class Fruit : int { Apple, Orange };
+
+void uninitialized_enum() {
+ Color color;
+ // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: variable 'color' is not initialized [cppcoreguidelines-init-variables]
+ Car car;
+ // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: variable 'car' is not initialized [cppcoreguidelines-init-variables]
+ Gender gender;
+ // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: variable 'gender' is not initialized [cppcoreguidelines-init-variables]
+ Direction direction;
+ // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: variable 'direction' is not initialized [cppcoreguidelines-init-variables]
+ Fruit fruit;
+ // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: variable 'fruit' is not initialized [cppcoreguidelines-init-variables]
+}
Index: clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines-init-variables.rst
===================================================================
--- clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines-init-variables.rst
+++ clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines-init-variables.rst
@@ -37,6 +37,21 @@
// Rest of the function.
}
+It warns for the uninitialized enum case, but without a FixIt:
+
+.. code-block:: c++
+
+ enum A {A1, A2, A3};
+ enum A_c : char { A_c1, A_c2, A_c3 };
+ enum class B { B1, B2, B3 };
+ enum class B_i : int { B_i1, B_i2, B_i3 };
+ void function() {
+ A a; // Warning: variable 'a' is not initialized
+ A_c a_c; // Warning: variable 'a_c' is not initialized
+ B b; // Warning: variable 'b' is not initialized
+ B_i b_i; // Warning: variable 'b_i' is not initialized
+ }
+
Options
-------
Index: clang-tools-extra/docs/ReleaseNotes.rst
===================================================================
--- clang-tools-extra/docs/ReleaseNotes.rst
+++ clang-tools-extra/docs/ReleaseNotes.rst
@@ -149,6 +149,12 @@
function or assignment to ``nullptr``.
Added support for pointers to ``std::unique_ptr``.
+- Improved :doc:`cppcoreguidelines-init-variables<clang-tidy/checks/cppcoreguidelines-init-variables>` check.
+
+ Removed the FixIt related to enum in this check because the rule it checks will initialize the enum as an integer and cause an error.
+
+ Added support for initialization check of the scoped enum
+
Removed checks
^^^^^^^^^^^^^^
Index: clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp
+++ clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp
@@ -81,6 +81,11 @@
const char *InitializationString = nullptr;
bool AddMathInclude = false;
+ if (TypePtr->isEnumeralType()) {
+ diag(MatchedDecl->getLocation(), "variable %0 is not initialized")
+ << MatchedDecl;
+ return;
+ }
if (TypePtr->isIntegerType())
InitializationString = " = 0";
else if (TypePtr->isFloatingType()) {
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D106431.362291.patch
Type: text/x-patch
Size: 3535 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210728/d426356b/attachment.bin>
More information about the cfe-commits
mailing list