-
Notifications
You must be signed in to change notification settings - Fork 14.1k
[Clang] Fix -Wunused-private-field false negative with defaulted comparison operators #116871
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
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-clang Author: Chris White (whiteio) ChangesFix -Wunused-private-field incorrectly suppressing warnings for friend defaulted comparison operators. The warning should only be suppressed when the defaulted comparison is a class member function. Fixes #116270 Full diff: https://github.com/llvm/llvm-project/pull/116871.diff 2 Files Affected:
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index 26041e53de5061..e115eb0a3d7e10 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -7535,7 +7535,7 @@ void Sema::CheckExplicitlyDefaultedFunction(Scope *S, FunctionDecl *FD) {
return;
}
- if (DefKind.isComparison())
+ if (DefKind.isComparison() && isa<CXXRecordDecl>(FD->getDeclContext()))
UnusedPrivateFields.clear();
if (DefKind.isSpecialMember()
diff --git a/clang/test/SemaCXX/warn-unused-private-field.cpp b/clang/test/SemaCXX/warn-unused-private-field.cpp
index 1128eacc309d9f..bdd66807dca35f 100644
--- a/clang/test/SemaCXX/warn-unused-private-field.cpp
+++ b/clang/test/SemaCXX/warn-unused-private-field.cpp
@@ -20,6 +20,17 @@ class SpaceShipDefaultCompare {
int operator<=>(const SpaceShipDefaultCompare &) const = default;
};
+class UnusedConstPrivateField {
+ public:
+ UnusedConstPrivateField() : unused_(0) {}
+ private:
+ const int unused_; // expected-warning{{private field 'unused_' is not used}}
+};
+
+class FriendEqDefaultCompare {
+ friend auto operator==(FriendEqDefaultCompare, FriendEqDefaultCompare) -> bool = default;
+};
+
#endif
class NotFullyDefined {
|
I'll update my changes to fix the other tests that are failing this evening, sorry about that, should have ran the rest of them after making the change. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM Thanks!. Can you add a note in the clang release notes here:
llvm-project/clang/docs/ReleaseNotes.rst
Line 559 in d8a1c6d
- Clang now diagnoses ``= delete("reason")`` extension warnings only in pedantic mode rather than on by default. (#GH109311). |
I think this should be good for the text:
- Fixed a bug where Clang would not emit ``-Wunused-private-field`` warnings when an unrelated class
defined a defaulted comparison operator (#GH116270).
.. code-block:: c++
class A {
private:
int a; // warning: private field 'a' is not used, no diagnostic previously
};
class C {
bool operator==(const C&) = default;
};
(If you want to build the clang documentation to check locally, you can follow https://github.com/llvm/llvm-project/blob/main/llvm/docs/README.txt#L11 and replace docs-llvm-html
with docs-clang-html
.)
Thanks for that @Maetveis! I've updated the release notes. |
Thanks! I added @AaronBallman, as a reviewer just to make sure I didn't miss anything, as I don't consider myself an expert when it comes to frontend. @AaronBallman I think you can merge if everything looks okay, I'm pretty sure @whiteio does not have commit rights. |
Hey, @whiteio I'm sorry the review is taking long, Aaron was off for the holidays. In the meantime it looks like some conflicts came up, are you comfortable resolving those or do you need help? I'll ping Aaron or find some other front-end reviewer once the conflicts are resolved. |
@Maetveis Thanks for letting me know, I'll resolve the conflicts and let you know once they're resolved. |
…arison 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 llvm#116270
Hey @Maetveis, I've addressed the conflicts 👍 |
Thanks, ping @AaronBallman or @erichkeane I would appreciate a second look from someone closer to frontend. |
@whiteio You seem to have the Keep my email addresses private setting enabled in your account. This will make the merged commit reference |
@Maetveis I've disabled that setting now. Thanks! |
@whiteio Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR. Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues. How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/30/builds/11645 Here is the relevant piece of the build log for the reference
|
Thanks for this fix! In our codebase, one example where this warning used to not fire (mostly by coincidence) was a class that did this:
While it's true that (You could make the bitfield unnamed and memset the bitfield area to 0, but then you'd have to do that from the class ctor body, meaning you'd have to initialize all member variables there too instead of in the initializer list, and it's pretty awkward, and memsetting member vars is questionable, etc.) Is there a better way to deal with this particular case that I'm not seeing? Or should this warning maybe not fire for member variables that are part of a bitfield, where other parts are used? |
There are a few ways around this: if you are using c++17 or later, there is #pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-private-field"
// your code for which the warning gets suppressed
#pragma clang diagnostic pop
// not suppressed here |
Note you can also spell the standard attribute as |
Fix -Wunused-private-field incorrectly suppressing warnings for friend defaulted comparison operators. The warning should only be suppressed when the defaulted comparison is a class member function.
Fixes #116270