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

Danny Mösch via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Jul 9 04:35:12 PDT 2022


SimplyDanny created this revision.
SimplyDanny added a reviewer: aaron.ballman.
Herald added subscribers: carlosgalvezp, shchenz, kbarton, xazax.hun, nemanjai.
Herald added a project: All.
SimplyDanny requested review of this revision.
Herald added a project: clang-tools-extra.
Herald added a subscriber: cfe-commits.

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


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D129420

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
@@ -64,7 +64,7 @@
 
   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;
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
@@ -84,6 +84,8 @@
 
   if (TypePtr->isEnumeralType())
     InitializationString = nullptr;
+  else if (TypePtr->isBooleanType())
+    InitializationString = " = false";
   else if (TypePtr->isIntegerType())
     InitializationString = " = 0";
   else if (TypePtr->isFloatingType()) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D129420.443429.patch
Type: text/x-patch
Size: 1178 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220709/e0689389/attachment.bin>


More information about the cfe-commits mailing list