[PATCH] D106431: [clang-tidy] Fix cppcoreguidelines-init-variables with enum judgement
gehry via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Wed Jul 21 04:49:45 PDT 2021
Sockke created this revision.
Sockke added reviewers: aaron.ballman, MTC, steven.zhang.
Herald added subscribers: shchenz, kbarton, xazax.hun, nemanjai.
Sockke requested review of this revision.
Herald added a project: clang-tools-extra.
Herald added a subscriber: cfe-commits.
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.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D106431
Files:
clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp
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
@@ -91,3 +91,18 @@
} catch (int X) {
}
}
+
+enum Color { Red, Green, Blue };
+
+enum Car { Benz, BMW = 20, Audi = BMW + 2 };
+
+enum Gender : char { Male, Female };
+
+void enum_should_not_be_initialized() {
+ // Expect no warning given here.
+ Color color;
+ // Expect no warning given here.
+ Car car;
+ // Expect no warning given here.
+ Gender gender;
+}
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
@@ -83,7 +83,9 @@
const char *InitializationString = nullptr;
bool AddMathInclude = false;
- if (TypePtr->isIntegerType())
+ if (TypePtr->isEnumeralType())
+ return;
+ else if (TypePtr->isIntegerType())
InitializationString = " = 0";
else if (TypePtr->isFloatingType()) {
InitializationString = " = NAN";
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D106431.360405.patch
Type: text/x-patch
Size: 1320 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210721/2fe54a88/attachment.bin>
More information about the cfe-commits
mailing list