Skip to content

Commit ad14635

Browse files
committed
[Clang] Fix -Wunused-private-field false negative with defaulted comparison operators
Fix -Wunused-private-field suppressing warnings when defaulted comparison operators are declared as friend functions. The warning should only be suppressed for comparison operators that are class members. Fixes #116270
1 parent aa2d084 commit ad14635

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

clang/lib/Sema/SemaDeclCXX.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7535,7 +7535,7 @@ void Sema::CheckExplicitlyDefaultedFunction(Scope *S, FunctionDecl *FD) {
75357535
return;
75367536
}
75377537

7538-
if (DefKind.isComparison())
7538+
if (DefKind.isComparison() && isa<CXXRecordDecl>(FD->getDeclContext()))
75397539
UnusedPrivateFields.clear();
75407540

75417541
if (DefKind.isSpecialMember()

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

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

23+
class UnusedConstPrivateField {
24+
public:
25+
UnusedConstPrivateField() : unused_(0) {}
26+
private:
27+
const int unused_; // expected-warning{{private field 'unused_' is not used}}
28+
};
29+
30+
class FriendEqDefaultCompare {
31+
friend auto operator==(FriendEqDefaultCompare, FriendEqDefaultCompare) -> bool = default;
32+
};
33+
2334
#endif
2435

2536
class NotFullyDefined {

0 commit comments

Comments
 (0)