Skip to content

[clang][Sema] Warn on explicit conversion functions in C++20 with -Wc++98-compat - #217342

Merged
flash1729 merged 1 commit into
llvm:mainfrom
flash1729:gh161689-cxx98-compat-explicit-conv
Aug 20, 2026
Merged

flash1729 merged 1 commit into
llvm:mainfrom
flash1729:gh161689-cxx98-compat-explicit-conv

Conversation

@flash1729

@flash1729 flash1729 commented Aug 19, 2026 •

Copy link
Copy Markdown
Member

The explicit(bool) patch (76b9027) added a !getLangOpts().CPlusPlus20 guard here, so this -Wc++98-compat warning stopped firing in C++20 and later. C++11 through C++17 still warn, and the test even has a FIXME asking for the warning in C++20.

Drop the guard so every mode behaves the same, update the test, and remove the FIXME.

Fixes #161689

@flash1729
flash1729 requested a review from Endilll August 19, 2026 14:35
@flash1729
flash1729 marked this pull request as ready for review August 19, 2026 14:35
@llvmorg-github-actions llvmorg-github-actions Bot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Aug 19, 2026
@llvmorg-github-actions

Copy link
Copy Markdown

@llvm/pr-subscribers-clang

Author: Aditya Medhane (flash1729)

Changes

The explicit(bool) patch (76b9027) added a !getLangOpts().CPlusPlus20 guard here, so this -Wc++98-compat warning stopped firing in C++20 and later. C++11 through C++17 still warn, and the test even has a FIXME asking for the warning in C++20.

Drop the guard so every mode behaves the same, update the test, and remove the FIXME.

Fixes #161689


Full diff: https://github.com/llvm/llvm-project/pull/217342.diff

3 Files Affected:

  • (modified) clang/docs/ReleaseNotes.md (+3)
  • (modified) clang/lib/Sema/SemaDeclCXX.cpp (+1-1)
  • (modified) clang/test/SemaCXX/cxx98-compat.cpp (+1-2)
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index b585161ba3ff4..0e5dab1e61de1 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -400,6 +400,9 @@ features cannot lower the translation-unit ABI level;
   for pointer arithmetic on statically-sized arrays when the offset is a
   non-negative constant within the array bounds.
 
+- `-Wc++98-compat` now diagnoses explicit conversion functions in C++20 and
+  later, matching the behavior in C++11 through C++17. (#GH161689)
+
 ### Improvements to Clang's time-trace
 
 ### Improvements to Coverage Mapping
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index 8d5ee07c5ad49..e84d35b2b5ecb 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -11613,7 +11613,7 @@ void Sema::CheckConversionDeclarator(Declarator &D, QualType &R,
     R = Context.getFunctionType(ConvType, {}, Proto->getExtProtoInfo());
 
   // C++0x explicit conversion operators.
-  if (DS.hasExplicitSpecifier() && !getLangOpts().CPlusPlus20)
+  if (DS.hasExplicitSpecifier())
     Diag(DS.getExplicitSpecLoc(),
          getLangOpts().CPlusPlus11
              ? diag::warn_cxx98_compat_explicit_conversion_functions
diff --git a/clang/test/SemaCXX/cxx98-compat.cpp b/clang/test/SemaCXX/cxx98-compat.cpp
index 587c242271a02..265f16f514309 100644
--- a/clang/test/SemaCXX/cxx98-compat.cpp
+++ b/clang/test/SemaCXX/cxx98-compat.cpp
@@ -227,8 +227,7 @@ void TrivialButNonPODThroughEllipsis() {
 }
 
 struct HasExplicitConversion {
-  // FIXME I think we should generate this diagnostic in C++20
-  explicit operator bool(); // not-cpp20-warning {{explicit conversion functions are incompatible with C++98}}
+  explicit operator bool(); // expected-warning {{explicit conversion functions are incompatible with C++98}}
 };
 
 struct Struct {};

@flash1729
flash1729 requested a review from cor3ntin August 19, 2026 19:29
@flash1729
flash1729 merged commit 25a993c into llvm:main Aug 20, 2026
16 checks passed
kieroxide pushed a commit to kieroxide/llvm-project that referenced this pull request Aug 21, 2026
…++98-compat (llvm#217342)

The explicit(bool) patch (76b9027) added a
`!getLangOpts().CPlusPlus20` guard here, so this `-Wc++98-compat`
warning stopped firing in C++20 and later. C++11 through C++17 still
warn, and the test even has a FIXME asking for the warning in C++20.

Drop the guard so every mode behaves the same, update the test, and
remove the FIXME.

Fixes llvm#161689
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

explicit conversion functions are incompatible with C++98 not warning in C++20

3 participants