[clang-tidy] Initialize boolean variables with `false` in cppcoreguidelines-init...
authorDanny Mösch <danny.moesch@icloud.com>
Sat, 9 Jul 2022 12:38:41 +0000 (14:38 +0200)
committerDanny Mösch <danny.moesch@icloud.com>
Sat, 9 Jul 2022 12:48:50 +0000 (14:48 +0200)
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

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

index efe6944..307559a 100644 (file)
@@ -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()) {
index 1d6fd05..9e3bc15 100644 (file)
@@ -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
 ^^^^^^^^^^^^^^
 
index 3ee1f0a..479e5f6 100644 (file)
@@ -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;