[clang-tools-extra] [clang-tidy] Respect explicit(false) on conversion operators (PR #240521)

Roland McGrath via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 9 12:03:59 PST 2026


https://github.com/frobtech updated https://github.com/llvm/llvm-project/pull/240521

>From 35ed9d77963814fff0fcf8591d9232dc78a1070c Mon Sep 17 00:00:00 2001
From: Roland McGrath <mcgrathr at google.com>
Date: Sat, 7 Nov 2026 05:26:02 -0800
Subject: [PATCH] [clang-tidy] Respect explicit(false) on conversion operators

Commit eb3b063995d6 (#82689) taught `ExplicitConstructorCheck`
(`misc-explicit-constructor`, aliased as `google-explicit-constructor`
and `cppcoreguidelines-explicit-constructor`) to ignore C++20
`explicit(false)` and instantiation-dependent `explicit(...)`
expressions on constructors, and to diagnose non-literal `false`
expressions without emitting an invalid `explicit` fix-it.

However, the `CXXConversionDecl` branch still unconditionally warned on
any conversion operator where `isExplicit()` was false and suggested
inserting `explicit `, even when `explicit(false)` or a dependent
`explicit(...)` specifier was already written. Apply the same
`ExplicitSpecifier` inspection to conversion operators so deliberately
implicit C++20 `explicit(false)` and dependent `explicit(...)`
conversion operators are not diagnosed.

Assisted-by: Gemini
---
 .../misc/ExplicitConstructorCheck.cpp         | 18 ++++++-
 clang-tools-extra/docs/ReleaseNotes.md        |  5 ++
 .../checks/misc/explicit-constructor.rst      |  1 +
 .../misc/explicit-constructor-cxx20.cpp       | 52 ++++++++++++++++++-
 4 files changed, 73 insertions(+), 3 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/misc/ExplicitConstructorCheck.cpp b/clang-tools-extra/clang-tidy/misc/ExplicitConstructorCheck.cpp
index 2304aef84af3d9..3929f7d775523e 100644
--- a/clang-tools-extra/clang-tidy/misc/ExplicitConstructorCheck.cpp
+++ b/clang-tools-extra/clang-tidy/misc/ExplicitConstructorCheck.cpp
@@ -47,8 +47,22 @@ void ExplicitConstructorCheck::check(const MatchFinder::MatchResult &Result) {
     // gmock to define matchers).
     if (Loc.isMacroID())
       return;
-    diag(Loc, NoExpressionWarningMessage)
-        << Conversion << FixItHint::CreateInsertion(Loc, "explicit ");
+
+    // Don't complain about explicit(false) or dependent expressions.
+    const ExplicitSpecifier ExplicitSpec = Conversion->getExplicitSpecifier();
+    const Expr *ExplicitExpr = ExplicitSpec.getExpr();
+    if (ExplicitExpr) {
+      ExplicitExpr = ExplicitExpr->IgnoreImplicit();
+      if (isa<CXXBoolLiteralExpr>(ExplicitExpr) ||
+          ExplicitExpr->isInstantiationDependent())
+        return;
+    }
+
+    const auto Diag = diag(Loc, ExplicitExpr ? WithExpressionWarningMessage
+                                             : NoExpressionWarningMessage)
+                      << Conversion;
+    if (!ExplicitExpr)
+      Diag << FixItHint::CreateInsertion(Loc, "explicit ");
     return;
   }
 
diff --git a/clang-tools-extra/docs/ReleaseNotes.md b/clang-tools-extra/docs/ReleaseNotes.md
index 4cd6b4b8a82eee..b923481142734e 100644
--- a/clang-tools-extra/docs/ReleaseNotes.md
+++ b/clang-tools-extra/docs/ReleaseNotes.md
@@ -281,6 +281,11 @@ infrastructure are described first, followed by tool-specific sections.
     instantiations, where the suggested `const` would break the move for
     instantiations with real move semantics.
 
+- Improved {doc}`misc-explicit-constructor
+  <clang-tidy/checks/misc/explicit-constructor>` check to respect C++20
+  `explicit(false)` and dependent `explicit` specifiers on conversion
+  operators, matching its handling of constructors.
+
 - Improved {doc}`misc-include-cleaner
   <clang-tidy/checks/misc/include-cleaner>` check by matching
   {option}`IgnoreHeaders` patterns containing `/` against header paths on
diff --git a/clang-tools-extra/docs/clang-tidy/checks/misc/explicit-constructor.rst b/clang-tools-extra/docs/clang-tidy/checks/misc/explicit-constructor.rst
index faaae6adb17a38..9ec39ebc86e4e7 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/misc/explicit-constructor.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/misc/explicit-constructor.rst
@@ -29,6 +29,7 @@ The check will suggest inserting ``explicit`` before the constructor or
 conversion operator declaration. However, copy and move constructors should not
 be explicit, as well as constructors taking a single ``initializer_list``
 argument.
+Declarations marked ``explicit(false)`` (since C++20) are treated as deliberately implicit and are not diagnosed, whereas non-literal constant expressions evaluating to ``false`` in ``explicit(...)`` are still diagnosed.
 
 This code:
 
diff --git a/clang-tools-extra/test/clang-tidy/checkers/misc/explicit-constructor-cxx20.cpp b/clang-tools-extra/test/clang-tidy/checkers/misc/explicit-constructor-cxx20.cpp
index 1ef1721ec6352e..d482d2c70bbf0c 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/misc/explicit-constructor-cxx20.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/misc/explicit-constructor-cxx20.cpp
@@ -8,52 +8,102 @@ static constexpr bool ConstTrue = true;
 
 struct A {
   explicit(true) A(int);
+  template <typename T>
+  explicit(true) A(T *);
+  explicit(true) operator bool() const;
+  template <typename T>
+  explicit(true) operator T *() const;
 };
 
 struct B {
   explicit(false) B(int);
+  template <typename T>
+  explicit(false) B(T *);
+  explicit(false) operator bool() const;
+  template <typename T>
+  explicit(false) operator T *() const;
 };
 
 struct C {
   explicit(ConstTrue) C(int);
+  template <typename T>
+  explicit(ConstTrue) C(T *);
+  explicit(ConstTrue) operator bool() const;
+  template <typename T>
+  explicit(ConstTrue) operator T *() const;
 };
 
 struct D {
   explicit(ConstFalse) D(int);
   // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: single-argument constructors explicit expression evaluates to 'false'
+  template <typename T>
+  explicit(ConstFalse) D(T *);
+  // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: single-argument constructors explicit expression evaluates to 'false'
+  explicit(ConstFalse) operator bool() const;
+  // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: 'operator bool' explicit expression evaluates to 'false'
+  template <typename T>
+  explicit(ConstFalse) operator T *() const;
+  // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: 'operator type-parameter-0-0 *' explicit expression evaluates to 'false'
 };
 
 template <typename>
 struct E {
   explicit(true) E(int);
+  explicit(true) operator bool() const;
 };
 
 template <typename>
 struct F {
   explicit(false) F(int);
+  explicit(false) operator bool() const;
 };
 
 template <typename>
 struct G {
   explicit(ConstTrue) G(int);
+  explicit(ConstTrue) operator bool() const;
 };
 
 template <typename>
 struct H {
   explicit(ConstFalse) H(int);
   // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: single-argument constructors explicit expression evaluates to 'false'
+  explicit(ConstFalse) operator bool() const;
+  // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: 'operator bool' explicit expression evaluates to 'false'
 };
 
 template <int Val>
 struct I {
   explicit(Val > 0) I(int);
+  explicit(Val > 0) operator bool() const;
 };
 
 template <int Val>
 struct J {
   explicit(Val > 0) J(int);
+  explicit(Val > 0) operator bool() const;
+};
+
+struct K {
+  template <typename T>
+  explicit(sizeof(T) > 1) K(T *);
+  template <typename T>
+  explicit(sizeof(T) > 1) operator T *() const;
 };
 
-void useJ(J<0>, J<100>);
+void useJAndK(J<0> j0, J<100> j100, K k, char *cp, int *ip) {
+  bool b0 = j0;
+  bool b1 = static_cast<bool>(j100);
+  K k0 = cp;
+  K k1(ip);
+  char *p0 = k;
+  int *p1 = static_cast<int *>(k);
+  (void)b0;
+  (void)b1;
+  (void)k0;
+  (void)k1;
+  (void)p0;
+  (void)p1;
+}
 
 } // namespace issue_81121



More information about the cfe-commits mailing list