[clang-tools-extra] 33e2129 - [clang-tidy] Initialize boolean variables with `false` in cppcoreguidelines-init-variables' fix-it

Danny Mösch via cfe-commits cfe-commits at lists.llvm.org
Sat Jul 9 05:49:50 PDT 2022


Author: Danny Mösch
Date: 2022-07-09T14:48:50+02:00
New Revision: 33e212954430d6116d7743541a7e9be8cdfac196

URL: https://github.com/llvm/llvm-project/commit/33e212954430d6116d7743541a7e9be8cdfac196
DIFF: https://github.com/llvm/llvm-project/commit/33e212954430d6116d7743541a7e9be8cdfac196.diff

LOG: [clang-tidy] Initialize boolean variables with `false` in cppcoreguidelines-init-variables' fix-it

In case of a variable with a built-in boolean type, `false` is a better fit to default-initialize it.

Reviewed By: njames93

Differential Revision: https://reviews.llvm.org/D129420

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp
    clang-tools-extra/docs/ReleaseNotes.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 efe694426728e..307559a403a9a 100644
--- a/clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp
+++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp
@@ -84,6 +84,8 @@ void InitVariablesCheck::check(const MatchFinder::MatchResult &Result) {
 
   if (TypePtr->isEnumeralType())
     InitializationString = nullptr;
+  else if (TypePtr->isBooleanType())
+    InitializationString = " = false";
   else if (TypePtr->isIntegerType())
     InitializationString = " = 0";
   else if (TypePtr->isFloatingType()) {

diff  --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 1d6fd05605db7..9e3bc1554f07b 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -247,6 +247,10 @@ Changes in existing checks
   <clang-tidy/checks/readability/simplify-boolean-expr>` to simplify expressions
   using DeMorgan's Theorem.
 
+- Made the fix-it of :doc:`cppcoreguidelines-init-variables
+  <clang-tidy/checks/cppcoreguidelines/init-variables>` use ``false`` to initialize
+  boolean variables.
+
 Removed checks
 ^^^^^^^^^^^^^^
 

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 3ee1f0aaec49e..479e5f62eb8b5 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
@@ -64,7 +64,7 @@ void init_unit_tests() {
 
   bool b;
   // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: variable 'b' is not initialized [cppcoreguidelines-init-variables]
-  // CHECK-FIXES: {{^}}  bool b = 0;{{$}}
+  // CHECK-FIXES: {{^}}  bool b = false;{{$}}
   bool bval = true;
 
   const char *ptr;


        


More information about the cfe-commits mailing list