[clang] 78d404a - [clang][c++20] Fix false warning for unused private fields when a class has only defaulted comparison operators.
Hans Wennborg via cfe-commits
cfe-commits at lists.llvm.org
Tue Jun 22 09:40:48 PDT 2021
Author: Alexandru Octavian Butiu
Date: 2021-06-22T18:40:16+02:00
New Revision: 78d404a11dd33c8349fd9b6ef5876d523c457f0e
URL: https://github.com/llvm/llvm-project/commit/78d404a11dd33c8349fd9b6ef5876d523c457f0e
DIFF: https://github.com/llvm/llvm-project/commit/78d404a11dd33c8349fd9b6ef5876d523c457f0e.diff
LOG: [clang][c++20] Fix false warning for unused private fields when a class has only defaulted comparison operators.
Fixes bug 50263
When "unused-private-field" flag is on if you have a struct with private
members and only defaulted comparison operators clang will warn about
unused private fields.
If you where to write the comparison operators by hand no warning is
produced.
This is a bug since defaulting a comparison operator uses all private
members .
The fix is simple, in CheckExplicitlyDefaultedFunction just clear the
list of unused private fields if the defaulted function is a comparison
function.
Differential revision: https://reviews.llvm.org/D102186
Added:
Modified:
clang/lib/Sema/SemaDeclCXX.cpp
clang/test/SemaCXX/warn-unused-private-field.cpp
Removed:
################################################################################
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index 5109f1e877a26..6d2710f1774ce 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -7292,6 +7292,9 @@ void Sema::CheckExplicitlyDefaultedFunction(Scope *S, FunctionDecl *FD) {
return;
}
+ if (DefKind.isComparison())
+ UnusedPrivateFields.clear();
+
if (DefKind.isSpecialMember()
? CheckExplicitlyDefaultedSpecialMember(cast<CXXMethodDecl>(FD),
DefKind.asSpecialMember())
diff --git a/clang/test/SemaCXX/warn-unused-private-field.cpp b/clang/test/SemaCXX/warn-unused-private-field.cpp
index fe44122a1d051..e67603eaceae6 100644
--- a/clang/test/SemaCXX/warn-unused-private-field.cpp
+++ b/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:
More information about the cfe-commits
mailing list