Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Clang] Only ignore special methods for unused private fields in BuildFieldReferenceExpr #116965

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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."?

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