This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Warn only for copyable/movable classes in cppcoreguidelines-avoid-const-or-ref-members
ClosedPublic

Authored by carlosgalvezp on Jul 18 2023, 10:18 AM.

Details

Summary

Since that's what the guidelines require.

Fixes #63733

Diff Detail

Event Timeline

carlosgalvezp created this revision.Jul 18 2023, 10:18 AM
Herald added a project: Restricted Project. · View Herald Transcript
carlosgalvezp requested review of this revision.Jul 18 2023, 10:18 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 18 2023, 10:18 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript

Remove confusing comment.

Fix typo in release notes.

Remove extra newline

Split getInfo function into 2 functions, remove
structured bindings.

PiotrZSL accepted this revision.Jul 18 2023, 1:11 PM

LGTM, but I'm not sure if isCopyableOrMovable will always work correctly, for a user defined special members it looks ok, but for some implicit ones I worry it may not always work. Probably things like "(hasSimpleCopyAssigment()) || (hasUserDeclaredCopyAssigment() && check here if its not deleted)" would be needed.
Thing is that CXXRecordDecl got most info (in confused way), there are things like DefaultedMoveConstructorIsDeleted that could be used to verify somehow base class.

clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidConstOrRefDataMembersCheck.cpp
91–102

Check first type, should be cheaper and consider mering those two.

clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/avoid-const-or-ref-data-members.rst
6
This revision is now accepted and ready to land.Jul 18 2023, 1:11 PM

LGTM, but I'm not sure if isCopyableOrMovable will always work correctly, for a user defined special members it looks ok, but for some implicit ones I worry it may not always work. Probably things like "(hasSimpleCopyAssigment()) || (hasUserDeclaredCopyAssigment() && check here if its not deleted)" would be needed.
Thing is that CXXRecordDecl got most info (in confused way), there are things like DefaultedMoveConstructorIsDeleted that could be used to verify somehow base class.

Yeah I spent a lot of time trying to figure this out, somehow I expected this logic to already exist somewhere in Sema! Thanks for the pointers, I will experiment with those functions as well.

clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidConstOrRefDataMembersCheck.cpp
91–102

Thanks for the tip! I'm not familiar with having multiple binds in the same addMatcher call. Do I still need to keep the bind("ref") at the end?

PiotrZSL added inline comments.Jul 18 2023, 11:37 PM
clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidConstOrRefDataMembersCheck.cpp
91–102

No, bind at the end need to be removed (I forgot about that).

carlosgalvezp marked 3 inline comments as done.

Address review comments

Using hasSimpleCopyConstructor and so on greatly simplifies the logic, great! Let me know if you are happy with it or I should go ahead and merge.

clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidConstOrRefDataMembersCheck.cpp
91–102

Somehow I didn't manage to get it to work with your proposed change and removing bind, I feel I'm walking on thin ice here :)

Merge matchers.

carlosgalvezp added inline comments.Jul 19 2023, 5:01 AM
clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidConstOrRefDataMembersCheck.cpp
91–102

Doing it from scratch helped, probably some parenthesis that was off!

PiotrZSL accepted this revision.Jul 19 2023, 5:03 AM

Looks fine.

This revision was landed with ongoing or failed builds.Jul 19 2023, 5:06 AM
This revision was automatically updated to reflect the committed changes.