[clang-tools-extra] dfa8f5b - [clang-tidy] Fix init-list handling in readability-implicit-bool-conversion

Piotr Zegar via cfe-commits cfe-commits at lists.llvm.org
Wed Apr 5 03:42:11 PDT 2023


Author: Piotr Zegar
Date: 2023-04-05T10:22:10Z
New Revision: dfa8f5b13e58cd742821860c351005e63f9ce890

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

LOG: [clang-tidy] Fix init-list handling in readability-implicit-bool-conversion

Adds support for explicit casts using initListExpr,
for example: int{boolValue} constructions.

Fixes: #47000

Reviewed By: ccotter

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

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp
    clang-tools-extra/docs/ReleaseNotes.rst
    clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp b/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp
index 7ec63d0fd9edf..863ac4dbbf4c6 100644
--- a/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp
@@ -262,7 +262,10 @@ void ImplicitBoolConversionCheck::registerMatchers(MatchFinder *Finder) {
       expr(anyOf(allOf(isMacroExpansion(), unless(isNULLMacroExpansion())),
                  has(ignoringImplicit(
                      memberExpr(hasDeclaration(fieldDecl(hasBitWidth(1)))))),
-                 hasParent(explicitCastExpr())));
+                 hasParent(explicitCastExpr()),
+                 expr(hasType(qualType().bind("type")),
+                      hasParent(initListExpr(hasParent(explicitCastExpr(
+                          hasType(qualType(equalsBoundNode("type"))))))))));
   auto ImplicitCastFromBool = implicitCastExpr(
       anyOf(hasCastKind(CK_IntegralCast), hasCastKind(CK_IntegralToFloating),
             // Prior to C++11 cast from bool literal to pointer was allowed.
@@ -290,7 +293,7 @@ void ImplicitBoolConversionCheck::registerMatchers(MatchFinder *Finder) {
                    unless(ExceptionCases), unless(has(BoolXor)),
                    // Retrieve also parent statement, to check if we need
                    // additional parens in replacement.
-                   anyOf(hasParent(stmt().bind("parentStmt")), anything()),
+                   optionally(hasParent(stmt().bind("parentStmt"))),
                    unless(isInTemplateInstantiation()),
                    unless(hasAncestor(functionTemplateDecl())))
                    .bind("implicitCastToBool")),

diff  --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 2dfc29e630624..6a87bcb3903e6 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -240,6 +240,10 @@ Changes in existing checks
   behavior of using `i` as the prefix for enum tags, set the `EnumConstantPrefix`
   option to `i` instead of using `EnumConstantHungarianPrefix`.
 
+- Fixed a false positive in :doc:`readability-implicit-bool-conversion
+  <clang-tidy/checks/readability/implicit-bool-conversion>` check warning would
+  be unnecessarily emitted for explicit cast using direct list initialization.
+
 - Added support to optionally ignore user-defined literals in
   :doc:`readability-magic-numbers<clang-tidy/checks/readability/magic-numbers>`.
 

diff  --git a/clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion.cpp
index d5e9bee4294d3..323cf813c0470 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion.cpp
@@ -471,3 +471,10 @@ bool f(S& s) {
 }
 
 } // namespace ignore_1bit_bitfields
+
+namespace PR47000 {
+  int to_int(bool x) { return int{x}; }
+
+  using IntType = int;
+  int to_int2(bool x) { return IntType{x}; }
+}


        


More information about the cfe-commits mailing list