Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,8 @@ Improvements to Clang's diagnostics
- For an rvalue reference bound to a temporary struct with an integer member, Clang will detect constant integer overflow
in the initializer for the integer member (#GH46755).

- Fixed a false negative ``-Wunused-private-field`` diagnostic when a defaulted comparison operator is defined out of class (#GH116961).

Improvements to Clang's time-trace
----------------------------------

Expand Down
12 changes: 10 additions & 2 deletions clang/lib/Sema/SemaExprMember.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1874,8 +1874,16 @@ Sema::BuildFieldReferenceExpr(Expr *BaseExpr, bool IsArrow,
Context.getAttributedType(attr::NoDeref, MemberType, MemberType);
}

auto *CurMethod = dyn_cast<CXXMethodDecl>(CurContext);
if (!(CurMethod && CurMethod->isDefaulted()))
auto isDefaultedSpecialMember = [this](const DeclContext *Ctx) {
auto *Method = dyn_cast<CXXMethodDecl>(CurContext);
if (!Method || !Method->isDefaulted())
return false;

return getDefaultedFunctionKind(Method).isSpecialMember();
};

// Implicit special members should not mark fields as used.
if (!isDefaultedSpecialMember(CurContext))
UnusedPrivateFields.remove(Field);

ExprResult Base = PerformObjectMemberConversion(BaseExpr, SS.getScopeRep(),
Expand Down
14 changes: 14 additions & 0 deletions clang/test/SemaCXX/warn-unused-private-field.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ class SpaceShipDefaultCompare {
int operator<=>(const SpaceShipDefaultCompare &) const = default;
};

class EqDefaultCompareOutOfClass {
int used; // no warning
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please explain a touch better with 'no warning'? Something like, "Test to ensure that the defaulted operator results in this being seen as 'used' despite there not being code to do so."?

Copy link
Contributor Author

@Maetveis Maetveis Nov 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, sorry I wasn't sure what you were referring to, but I get now that from the change-set it is not obvious how / why the issue in #116961 is fixed, and why it is a special case not covered by the current testing. I expanded quite a bit on the comment, hopefully it did not become too verbose now.

bool operator==(const EqDefaultCompareOutOfClass &) const;
};

bool EqDefaultCompareOutOfClass::operator==(const EqDefaultCompareOutOfClass &) const = default;

class FriendEqDefaultCompareOutOfClass {
int used; // no warning
friend bool operator==(const FriendEqDefaultCompareOutOfClass &, const FriendEqDefaultCompareOutOfClass &);
};

bool operator==(const FriendEqDefaultCompareOutOfClass &, const FriendEqDefaultCompareOutOfClass &) = default;

#endif

class NotFullyDefined {
Expand Down
Loading