Author: Jean-Christian CÎRSTEA
Date: 2026-03-22T21:37:12Z
New Revision: 8d64f56892b539b312140e204e51fa16e747962d

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

LOG: [Clang] Honour [[maybe_unused]] on private fields (#187940)

Before this commit, [[maybe_unused]] on private fields was ignored. In
conjunction with `-Wunused-private-field`, false warnings were emitted
by clang. This commit fixes this by checking if an unused private field
is annotated with [[maybe_unused]].

Added: 
    clang/test/SemaCXX/warn-maybe-unused-private-field.cpp

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/lib/Sema/Sema.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index b92f1ab34aa51..804aebe0cc06c 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -309,6 +309,10 @@ Improvements to Clang's diagnostics
   (``-fimplicit-module-maps``). This does not affect module maps specified
   explicitly via ``-fmodule-map-file=``.
 
+- Honour ``[[maybe_unused]]`` attribute on private fields.
+  ``-Wunused-private-field`` no longer emits a warning for annotated private
+  fields.
+
 Improvements to Clang's time-trace
 ----------------------------------
 

diff  --git a/clang/lib/Sema/Sema.cpp b/clang/lib/Sema/Sema.cpp
index 8b1d0398cf65d..826277c445a1d 100644
--- a/clang/lib/Sema/Sema.cpp
+++ b/clang/lib/Sema/Sema.cpp
@@ -1611,7 +1611,7 @@ void Sema::ActOnEndOfTranslationUnit() {
     RecordCompleteMap MNCComplete;
     for (const NamedDecl *D : UnusedPrivateFields) {
       const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D->getDeclContext());
-      if (RD && !RD->isUnion() &&
+      if (RD && !RD->isUnion() && !D->hasAttr<UnusedAttr>() &&
           IsRecordFullyDefined(RD, RecordsComplete, MNCComplete)) {
         Diag(D->getLocation(), diag::warn_unused_private_field)
               << D->getDeclName();

diff  --git a/clang/test/SemaCXX/warn-maybe-unused-private-field.cpp 
b/clang/test/SemaCXX/warn-maybe-unused-private-field.cpp
new file mode 100644
index 0000000000000..505c950a9fb81
--- /dev/null
+++ b/clang/test/SemaCXX/warn-maybe-unused-private-field.cpp
@@ -0,0 +1,11 @@
+// RUN: %clang_cc1 -fsyntax-only -Wunused-private-field -verify -std=c++17 %s
+// RUN: %clang_cc1 -fsyntax-only -Wunused-private-field -verify -std=c++20 %s
+// RUN: %clang_cc1 -fsyntax-only -Wunused-private-field -verify -std=c++23 %s
+
+class MyClass {
+       // Marking an unused field with [[maybe_unused]] shouldn't result in a
+       // warning
+       [[maybe_unused]]
+       unsigned field1;
+       signed field2; // expected-warning{{private field 'field2' is not used}}
+};


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to