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";
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";
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits