diff --git a/clang-tools-extra/clang-tidy/google/ExplicitConstructorCheck.cpp b/clang-tools-extra/clang-tidy/google/ExplicitConstructorCheck.cpp --- a/clang-tools-extra/clang-tidy/google/ExplicitConstructorCheck.cpp +++ b/clang-tools-extra/clang-tidy/google/ExplicitConstructorCheck.cpp @@ -88,6 +88,8 @@ Result.Nodes.getNodeAs("conversion")) { if (Conversion->isOutOfLine()) return; + if (Conversion->getExplicitSpecifier().isSpecified()) + return; SourceLocation Loc = Conversion->getLocation(); // Ignore all macros until we learn to ignore specific ones (e.g. used in // gmock to define matchers). @@ -133,7 +135,7 @@ } if (Ctor->isExplicit() || Ctor->isCopyOrMoveConstructor() || - TakesInitializerList) + TakesInitializerList || Ctor->getExplicitSpecifier().isSpecified()) return; bool SingleArgument = diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -156,6 +156,10 @@ - Removed default setting ``cppcoreguidelines-explicit-virtual-functions.IgnoreDestructors = "true"``, to match the current state of the C++ Core Guidelines. +- Updated :doc:`google-explicit-constructor + ` check to ignore constructors + or conversion operators where they are marked ``explicit(false)``. + - Updated :doc:`google-readability-casting ` to diagnose and fix functional casts, to achieve feature parity with the corresponding ``cpplint.py`` check. diff --git a/clang-tools-extra/test/clang-tidy/checkers/google-explicit-constructor-cxx20.cpp b/clang-tools-extra/test/clang-tidy/checkers/google-explicit-constructor-cxx20.cpp new file mode 100644 --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/google-explicit-constructor-cxx20.cpp @@ -0,0 +1,17 @@ +// RUN: %check_clang_tidy -std=c++20 %s google-explicit-constructor %t + +struct Foo { + Foo(int) {} + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: single-argument constructors must be marked explicit to avoid unintentional implicit conversions + // CHECK-FIXES: {{^}} explicit Foo(int) {} + explicit Foo(long) {} + explicit(false) Foo(float) {} + explicit(true) Foo(double) {} + + operator int() const; + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: 'operator int' must be marked explicit to avoid unintentional implicit conversions + // CHECK-FIXES: {{^}} explicit operator int() const; + explicit operator long() const; + explicit(false) operator float() const; + explicit(true) operator double() const; +};