From 33e212954430d6116d7743541a7e9be8cdfac196 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Danny=20M=C3=B6sch?= Date: Sat, 9 Jul 2022 14:38:41 +0200 Subject: [PATCH] [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 --- .../clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp | 2 ++ clang-tools-extra/docs/ReleaseNotes.rst | 4 ++++ .../clang-tidy/checkers/cppcoreguidelines/init-variables.cpp | 2 +- 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp index efe694426728..307559a403a9 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 1d6fd05605db..9e3bc1554f07 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -247,6 +247,10 @@ Changes in existing checks ` to simplify expressions using DeMorgan's Theorem. +- Made the fix-it of :doc:`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 3ee1f0aaec49..479e5f62eb8b 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; -- 2.34.1