[clang] [Clang] Fix -Wunused-private-field false negative with defaulted comparison operators (PR #116871)
Mészáros Gergely via cfe-commits
cfe-commits at lists.llvm.org
Wed Nov 20 02:48:21 PST 2024
================
@@ -7535,7 +7535,7 @@ void Sema::CheckExplicitlyDefaultedFunction(Scope *S, FunctionDecl *FD) {
return;
}
- if (DefKind.isComparison())
+ if (DefKind.isComparison() && isa<CXXRecordDecl>(FD->getDeclContext()))
UnusedPrivateFields.clear();
----------------
Maetveis wrote:
If I read this correctly what you did here is you stopped clearing `UnusedPrivateFields` on comparison functions declared as a `friend` function.
It is not a problem for `friend` functions to mark fields as used. The problem in #116270 is that an unrelated class (`C` in the example) causes the warning to be skipped for another class (`A`).
Instead of clearing all fields from `UnusedPrivateFields` (which contain fields of both `A` and `C` in the example) only `C`'s fields should be cleared.
You should loop over `C`'s field and remove all of them from `UnusedPrivateFields` using `UnusedFunctions.remove(Field)`. You find the declaration of `C` by looking at the first parameter of the function being checked, like it is done here [SemaDeclCXX.cpp:9055-9058](https://github.com/llvm/llvm-project/blob/main/clang/lib/Sema/SemaDeclCXX.cpp#L9055-L9058). The fields can then be queried using `Decl.fields()` which gives you a range of `FieldDecl` pointers, you can pass each of those to `UnusedFunctions.remove`.
If you have questions or if I'm not clear enough feel free to ask :). Thanks for contributing to LLVM!
https://github.com/llvm/llvm-project/pull/116871
More information about the cfe-commits
mailing list