This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Do not trigger cppcoreguidelines-avoid-const-or-ref-data-members on lambda captures
ClosedPublic

Authored by carlosgalvezp on Aug 12 2022, 7:56 AM.

Details

Summary

Lambdas are implemented as regular classes internally,
and the captured variables end up as members there.
Do not diagnose those - the check should cover only
regular classes and structs.

Diff Detail

Event Timeline

carlosgalvezp created this revision.Aug 12 2022, 7:56 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 12 2022, 7:56 AM
carlosgalvezp requested review of this revision.Aug 12 2022, 7:56 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 12 2022, 7:56 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
Eugene.Zelenko added a subscriber: Eugene.Zelenko.

Please mention changes in Release Notes.

Please mention changes in Release Notes.

Thanks for the quick review, @Eugene.Zelenko !

This check was newly added a couple of days ago (as per the "New checks" section in the Release Notes). I thought the "Changes to exiting checks" section was only meant for checks already existing prior to the new release, to avoid adding excessive clutter to the documentation. New checks typically take a few iterations to polish when testing on larger projects.

What do you think?

Test also capture by reference.

Please mention changes in Release Notes.

Thanks for the quick review, @Eugene.Zelenko !

This check was newly added a couple of days ago (as per the "New checks" section in the Release Notes). I thought the "Changes to exiting checks" section was only meant for checks already existing prior to the new release, to avoid adding excessive clutter to the documentation. New checks typically take a few iterations to polish when testing on larger projects.

What do you think?

Sorry, I didn't know history. Sure, it doesn't make sense for checks added in main.

njames93 added inline comments.Aug 17 2022, 3:45 AM
clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/avoid-const-or-ref-data-members.cpp
191

Can you add some cases with implicit capture (using [=] and [&])

Added unit test for implicit capture (ref and value)

carlosgalvezp marked an inline comment as done.Aug 17 2022, 7:03 AM
njames93 added inline comments.Aug 17 2022, 8:15 AM
clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/avoid-const-or-ref-data-members.cpp
191

I should have been more clear, you need to actually use the variables inside the lambda to implicitly capture them.

Update implicit lambda captures to use the captured variables
inside the lambdas.

carlosgalvezp marked an inline comment as done.Aug 17 2022, 11:37 PM
carlosgalvezp added inline comments.
clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/avoid-const-or-ref-data-members.cpp
191

Thanks, didn't know that!

It's actually quite interesting, implicit lambda captures never trigger an error:
https://godbolt.org/z/cErf4jv8E

But it's probably good to keep the test anyway in case the lambda implementation changes.

njames93 accepted this revision.Aug 19 2022, 12:05 AM
njames93 added inline comments.
clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/avoid-const-or-ref-data-members.cpp
191

What's more interesting about that is the error message that was emitted.

<source>:6:16: warning: member '' of type 'const int' is const qualified [cppcoreguidelines-avoid-const-or-ref-data-members]
    auto y3 = [x]{};

However this fix should accidentally fix the issue of unnamed members.

This revision is now accepted and ready to land.Aug 19 2022, 12:05 AM

Thanks for the review!