This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Add option to ignore capture default by reference in cppcoreguidelines-avoid-capture-default-when-capturing-this
ClosedPublic

Authored by carlosgalvezp on Mar 28 2023, 9:52 AM.

Details

Summary

The rule exists primarily for when using capture default
by copy "[=]", since member variables will be captured by
reference, which is against developer expectations.

However when the capture default is by reference, then there
is no doubt: everything will be captured by reference. Add
an option to allow just that.

Note: Release Notes do not need update since this check
has been introduced in the current WIP release.

A ticket has been opened at the C++ Core Guidelines repo
to consider updating the rule such that this behavior
is the default one:
https://github.com/isocpp/CppCoreGuidelines/issues/2060

Diff Detail

Event Timeline

carlosgalvezp created this revision.Mar 28 2023, 9:52 AM
Herald added a project: Restricted Project. · View Herald Transcript
carlosgalvezp requested review of this revision.Mar 28 2023, 9:52 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 28 2023, 9:52 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript

Clean up check suffix in test.

Fix typo in doc.

No comments from me, looks ok.

ccotter accepted this revision.Mar 28 2023, 1:15 PM

If https://github.com/isocpp/CppCoreGuidelines/issues/2060 is accepted to only consider [=], then I assume we'd want to change the default value of IgnoreCaptureDefaultByReference to be true? Thanks by the way for this!

This revision is now accepted and ready to land.Mar 28 2023, 1:15 PM

If https://github.com/isocpp/CppCoreGuidelines/issues/2060 is accepted to only consider [=], then I assume we'd want to change the default value of IgnoreCaptureDefaultByReference to be true? Thanks by the way for this!

Thanks for reviewing! If they do accept it (I'm not very optimistic) then I'd probably just remove this logic and exit early if the lambda capture is not LCD_ByCopy.

This revision was landed with ongoing or failed builds.Mar 28 2023, 1:37 PM
This revision was automatically updated to reflect the committed changes.