[clang-tools-extra] [clang-tidy] Migrate explicit-constructor check from google to misc and add relative aliases (PR #194807)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Apr 30 01:02:56 PDT 2026
================
@@ -0,0 +1,56 @@
+.. title:: clang-tidy - misc-explicit-constructor
+
+misc-explicit-constructor
+===========================
+
+
+Checks that constructors callable with a single argument and conversion
+operators are marked explicit to avoid the risk of unintentional implicit
+conversions.
+
+Consider this example:
+
+.. code-block:: c++
+
+ struct S {
+ int x;
+ operator bool() const { return true; }
+ };
+
+ bool f() {
+ S a{1};
+ S b{2};
+ return a == b;
+ }
+
+The function will return ``true``, since the objects are implicitly converted
+to ``bool`` before comparison, which is unlikely to be the intent.
+
+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.
+
+This code:
+
+.. code-block:: c++
+
+ struct S {
+ S(int a);
+ explicit S(const S&);
+ operator bool() const;
+ ...
+
+will become
+
+.. code-block:: c++
+
+ struct S {
+ explicit S(int a);
+ S(const S&);
+ explicit operator bool() const;
+ ...
+
+
+
----------------
EugeneZelenko wrote:
```suggestion
```
https://github.com/llvm/llvm-project/pull/194807
More information about the cfe-commits
mailing list