[llvm-branch-commits] [clang-tools-extra] 8dcdfc0 - Fix cppcoreguidelines-init-variables by removing the enum FixIt, and add support for initialization check of scoped enum

Tom Stellard via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Wed Aug 11 21:49:51 PDT 2021


Author: Liuke Gehry
Date: 2021-08-11T21:49:30-07:00
New Revision: 8dcdfc0de84f60b5b4af97ac5b357881af55bc6e

URL: https://github.com/llvm/llvm-project/commit/8dcdfc0de84f60b5b4af97ac5b357881af55bc6e
DIFF: https://github.com/llvm/llvm-project/commit/8dcdfc0de84f60b5b4af97ac5b357881af55bc6e.diff

LOG: 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 4a097efe7784767b7d12ffcb8f2b22b9f4d045e2)

Added: 
    

Modified: 
    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

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp
index d9be9b653e3b6..cded53487ae8f 100644
--- a/clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp
+++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp
@@ -78,10 +78,12 @@ void InitVariablesCheck::check(const MatchFinder::MatchResult &Result) {
     return;
 
   QualType TypePtr = MatchedDecl->getType();
-  const char *InitializationString = nullptr;
+  llvm::Optional<const char *> InitializationString = llvm::None;
   bool AddMathInclude = false;
 
-  if (TypePtr->isIntegerType())
+  if (TypePtr->isEnumeralType())
+    InitializationString = nullptr;
+  else if (TypePtr->isIntegerType())
     InitializationString = " = 0";
   else if (TypePtr->isFloatingType()) {
     InitializationString = " = NAN";
@@ -96,11 +98,12 @@ void InitVariablesCheck::check(const MatchFinder::MatchResult &Result) {
   if (InitializationString) {
     auto Diagnostic =
         diag(MatchedDecl->getLocation(), "variable %0 is not initialized")
-        << MatchedDecl
-        << FixItHint::CreateInsertion(
-               MatchedDecl->getLocation().getLocWithOffset(
-                   MatchedDecl->getName().size()),
-               InitializationString);
+        << MatchedDecl;
+    if (*InitializationString != nullptr)
+      Diagnostic << FixItHint::CreateInsertion(
+          MatchedDecl->getLocation().getLocWithOffset(
+              MatchedDecl->getName().size()),
+          *InitializationString);
     if (AddMathInclude) {
       Diagnostic << IncludeInserter.createIncludeInsertion(
           Source.getFileID(MatchedDecl->getBeginLoc()), MathHeader);

diff  --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 30d0319edd0bf..d0d361c5046b0 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -151,6 +151,14 @@ Changes in existing checks
 
   Added an option to choose the set of allowed functions.
 
+- Improved :doc:`cppcoreguidelines-init-variables
+  <clang-tidy/checks/cppcoreguidelines-init-variables>` check.
+
+  Removed generating fixes for enums because the code generated was broken,
+  trying to initialize the enum from an integer.
+
+  The check now also warns for uninitialized scoped enums.
+
 - Improved :doc:`readability-uniqueptr-delete-release
   <clang-tidy/checks/readability-uniqueptr-delete-release>` check.
 
@@ -158,6 +166,7 @@ Changes in existing checks
   function or assignment to ``nullptr``.
   Added support for pointers to ``std::unique_ptr``.
 
+
 Removed checks
 ^^^^^^^^^^^^^^
 

diff  --git a/clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines-init-variables.rst b/clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines-init-variables.rst
index 62af932837c15..91cf4aa0f0bb1 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines-init-variables.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines-init-variables.rst
@@ -37,6 +37,21 @@ Would be rewritten to look like this:
      // 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
 -------
 

diff  --git a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-init-variables.cpp b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-init-variables.cpp
index a2a59fe75aa28..3ee1f0aaec49e 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-init-variables.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-init-variables.cpp
@@ -92,3 +92,35 @@ void catch_variable_decl() {
   } 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]
+}


        


More information about the llvm-branch-commits mailing list