Skip to content

Commit 90febd4

Browse files
committed
[Clang] Only ignore special methods for unused private fields in BuildFieldReferenceExpr
The original code assumed that only special methods might be defined as defaulted. Since C++20 comparison operators might be defaulted too, and we *do* want to consider those as using the fields of the class. Fixes: #116961
1 parent d88ed93 commit 90febd4

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

clang/lib/Sema/SemaExprMember.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1874,8 +1874,16 @@ Sema::BuildFieldReferenceExpr(Expr *BaseExpr, bool IsArrow,
18741874
Context.getAttributedType(attr::NoDeref, MemberType, MemberType);
18751875
}
18761876

1877-
auto *CurMethod = dyn_cast<CXXMethodDecl>(CurContext);
1878-
if (!(CurMethod && CurMethod->isDefaulted()))
1877+
auto isDefaultedSpecialMember = [this](const DeclContext *Ctx) {
1878+
auto *Method = dyn_cast<CXXMethodDecl>(CurContext);
1879+
if (!Method || !Method->isDefaulted())
1880+
return false;
1881+
1882+
return getDefaultedFunctionKind(Method).isSpecialMember();
1883+
};
1884+
1885+
// Implicit special members should not mark fields as used.
1886+
if (!isDefaultedSpecialMember(CurContext))
18791887
UnusedPrivateFields.remove(Field);
18801888

18811889
ExprResult Base = PerformObjectMemberConversion(BaseExpr, SS.getScopeRep(),

clang/test/SemaCXX/warn-unused-private-field.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,20 @@ class SpaceShipDefaultCompare {
2020
int operator<=>(const SpaceShipDefaultCompare &) const = default;
2121
};
2222

23+
class EqDefaultCompareOutOfClass {
24+
int used;
25+
bool operator==(const EqDefaultCompareOutOfClass &) const;
26+
};
27+
28+
bool EqDefaultCompareOutOfClass::operator==(const EqDefaultCompareOutOfClass &) const = default;
29+
30+
class FriendEqDefaultCompareOutOfClass {
31+
int used;
32+
friend bool operator==(const FriendEqDefaultCompareOutOfClass &, const FriendEqDefaultCompareOutOfClass &);
33+
};
34+
35+
bool operator==(const FriendEqDefaultCompareOutOfClass &, const FriendEqDefaultCompareOutOfClass &) = default;
36+
2337
#endif
2438

2539
class NotFullyDefined {

0 commit comments

Comments
 (0)