[PATCH] D102186: [clang][c++20] Fix false warning for unused private fields when a class has only defaulted comparison operators.

Alexandru Octavian Buțiu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Jun 16 05:11:16 PDT 2021


predator5047 updated this revision to Diff 352400.
predator5047 added a comment.

Updated the test case so it is clearer.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D102186/new/

https://reviews.llvm.org/D102186

Files:
  clang/lib/Sema/SemaDeclCXX.cpp
  clang/test/SemaCXX/warn-unused-private-field.cpp


Index: clang/test/SemaCXX/warn-unused-private-field.cpp
===================================================================
--- clang/test/SemaCXX/warn-unused-private-field.cpp
+++ clang/test/SemaCXX/warn-unused-private-field.cpp
@@ -1,5 +1,26 @@
 // RUN: %clang_cc1 -fsyntax-only -Wunused-private-field -Wused-but-marked-unused -Wno-uninitialized -verify -std=c++11 %s
 // RUN: %clang_cc1 -fsyntax-only -Wunused-private-field -Wused-but-marked-unused -Wno-uninitialized -verify -std=c++17 %s
+// RUN: %clang_cc1 -fsyntax-only -Wunused-private-field -Wused-but-marked-unused -Wno-uninitialized -verify -std=c++20 %s
+
+#if __cplusplus >= 202002L
+
+class EqDefaultCompare {
+  int used;
+
+public:
+  EqDefaultCompare(int x) : used(x) {}
+  bool operator==(const EqDefaultCompare &) const = default;
+};
+
+class SpaceShipDefaultCompare {
+  int used;
+
+public:
+  SpaceShipDefaultCompare(int x) : used(x) {}
+  int operator<=>(const SpaceShipDefaultCompare &) const = default;
+};
+
+#endif
 
 class NotFullyDefined {
  public:
Index: clang/lib/Sema/SemaDeclCXX.cpp
===================================================================
--- clang/lib/Sema/SemaDeclCXX.cpp
+++ clang/lib/Sema/SemaDeclCXX.cpp
@@ -7292,6 +7292,9 @@
     return;
   }
 
+  if (DefKind.isComparison())
+    UnusedPrivateFields.clear();
+
   if (DefKind.isSpecialMember()
           ? CheckExplicitlyDefaultedSpecialMember(cast<CXXMethodDecl>(FD),
                                                   DefKind.asSpecialMember())


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D102186.352400.patch
Type: text/x-patch
Size: 1502 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210616/5d83c8b6/attachment.bin>


More information about the cfe-commits mailing list