This is an archive of the discontinued LLVM Phabricator instance.

[clang][c++20] Fix false warning for unused private fields when a class has only defaulted comparison operators.
ClosedPublic

Authored by predator5047 on May 10 2021, 11:32 AM.

Details

Summary

Fixes bug 50263

When "unused-private-field" flag is on if you have a struct with private members and only defaulted comparison operators clang will warn about unused private fields.

If you where to write the comparison operators by hand no warning is produced.

This is a bug since defaulting a comparison operator uses all private members .

The fix is simple, in CheckExplicitlyDefaultedFunction just clear the list of unused private fields if the defaulted function is a comparison function.

Diff Detail

Event Timeline

predator5047 requested review of this revision.May 10 2021, 11:32 AM
predator5047 created this revision.
predator5047 edited the summary of this revision. (Show Details)Jun 10 2021, 12:36 PM
predator5047 set the repository for this revision to rG LLVM Github Monorepo.
predator5047 added a project: Restricted Project.
hans added inline comments.Jun 14 2021, 1:50 AM
clang/test/SemaCXX/warn-unused-private-field.cpp
7

The test case feels a little strange to me, I mean the member variable is still pretty unused even though I suppose the comparison function technically uses it.. At least the test case in the bug report had a constructor which set the value. Perhaps with that it makes more sense.

Updated the test case so it is clearer.

predator5047 marked an inline comment as done.Jun 16 2021, 5:12 AM
predator5047 added inline comments.
clang/test/SemaCXX/warn-unused-private-field.cpp
7

Thanks for your feedback I updated the test case based on your comments.

hans accepted this revision.Jun 16 2021, 9:07 AM
This revision is now accepted and ready to land.Jun 16 2021, 9:07 AM
predator5047 marked an inline comment as done.Jun 22 2021, 4:37 AM

@hans Can you commit on my behalf?
Email: Alexandru Octavian Butiu alexandru.octavian.butiu at gmail.com

This revision was landed with ongoing or failed builds.Jun 22 2021, 9:40 AM
This revision was automatically updated to reflect the committed changes.
hans added a comment.Jun 22 2021, 9:41 AM

@hans Can you commit on my behalf?
Email: Alexandru Octavian Butiu alexandru.octavian.butiu at gmail.com

Sure! Pushed as https://github.com/llvm/llvm-project/commit/78d404a11dd33c8349fd9b6ef5876d523c457f0e