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

Merged
merged 3 commits into from
Nov 26, 2024
Merged
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
20 changes: 20 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,26 @@ class SpaceShipDefaultCompare {
int operator<=>(const SpaceShipDefaultCompare &) const = default;
};

class EqDefaultCompareOutOfClass {
int used; // no warning, the compiler generated AST for the comparison operator
// references the fields of the class, and this should be considered
// a use.
// This test case is needed because clang does not emit the body
// of the defaulted operator when it is defined in-class until it
// finds a call to it. `-Wunused-private-field` is suppressed in
// a different way in that case.
bool operator==(const EqDefaultCompareOutOfClass &) const;
};

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

class FriendEqDefaultCompareOutOfClass {
int used; // no warning, same reasoning just tested via a friend declaration.
friend bool operator==(const FriendEqDefaultCompareOutOfClass &, const FriendEqDefaultCompareOutOfClass &);
};

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

#endif

class NotFullyDefined {
Expand Down
Loading