This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Apply cppcoreguidelines-avoid-capture-default-when-capturin-this only to by-value capture default
ClosedPublic

Authored by carlosgalvezp on Apr 14 2023, 7:16 AM.

Diff Detail

Event Timeline

carlosgalvezp created this revision.Apr 14 2023, 7:16 AM
Herald added a project: Restricted Project. · View Herald Transcript
carlosgalvezp requested review of this revision.Apr 14 2023, 7:16 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 14 2023, 7:16 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript

Remove excessive newlines

Eugene.Zelenko added inline comments.Apr 14 2023, 7:33 AM
clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidCaptureDefaultWhenCapturingThisCheck.h
0

Please make it single line by removing all possible = and -.

PiotrZSL added inline comments.Apr 14 2023, 7:54 AM
clang-tools-extra/clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp
54

this name is hard to understand

I asked ChatGPT about it, and here are some other proposals:

  • cppcoreguidelines-avoid-by-value-default-this-capture
  • cppcoreguidelines-avoid-this-capture-by-value-default
  • cppcoreguidelines-explicit-this-capture-by-value
  • cppcoreguidelines-implicit-this-capture-by-value
  • cppcoreguidelines-implicit-by-value-this-capture
  • cppcoreguidelines-prefer-explicit-this-capture
  • cppcoreguidelines-avoid-ambiguous-this-capture
PiotrZSL added inline comments.Apr 14 2023, 10:21 AM
clang-tools-extra/clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp
54

Probably something like this would be good:
cppcoreguidelines-avoid-implicit-this-capture-by-value

Shorten check name

carlosgalvezp marked an inline comment as done.Apr 14 2023, 12:03 PM
carlosgalvezp added inline comments.
clang-tools-extra/clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp
54

Nice suggestions! I've done a bit of mix of them, let me know if you are happy with it.

PiotrZSL accepted this revision.Apr 14 2023, 12:29 PM

Just some minor issues in documentation.
To be honest for me this check still looks too strict, for example it will warn when we capture this explicitly, and we don't use any class fields, but we use some local variables captured by value and for example call some other class method using that this pointer.
But well, i'm not fan of cppcoreguidelines, I justt fell that most of rules there are not 100% implementable in a normal project.

clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidCaptureDefaultWhenCapturingThisCheck.h
0

thats too much information for an check short description.

clang-tools-extra/clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp
54

To be honest that "default" sounds strange. Simply I check name does not read as an "sentence", more as bunch of words, but I'm fine with this.

clang-tools-extra/docs/ReleaseNotes.rst
118

maybe "Warns when a lambda captures this using the default by-value capture."

and align this with check doxygen comment and documentation, because you changed those two but not here.

clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/avoid-capture-default-when-capturing-this.rst
11

Note: I'm missing here explanation why. (yes I know that this may not be related)

Maybe:
"By-value capture-defaults in member functions can be misleading about whether data members are captured by value or reference. This occurs because specifying the capture default [=] actually captures the this pointer by value, not the data members themselves. As a result, data members are still indirectly accessed via the captured this pointer, which essentially means they are being accessed by reference. Therefore, even when using [=], data members are effectively captured by reference, which might not align with the user's expectations."

This revision is now accepted and ready to land.Apr 14 2023, 12:29 PM
carlosgalvezp added inline comments.Apr 15 2023, 1:13 AM
clang-tools-extra/clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp
54

I agree, I'm not very happy either. We had quite a lot of discussion in the original patch about this. The problem is that the Standard calls it "capture default" which doesn't read like an English sentence (should be "default capture"). But it seems "capture default" is a widely used term out there and in the repo, so that's why we went for that.

What about the following?

  • cppcoreguidelines-ambiguous-capture-default-by-value
  • cppcoreguidelines-ambiguous-by-value-capture-default
carlosgalvezp marked 3 inline comments as done.Apr 15 2023, 1:21 AM

To be honest for me this check still looks too strict, for example it will warn when we capture this explicitly, and we don't use any class fields, but we use some local variables captured by value and for example call some other class method using that this pointer.

That's a good point, I would agree that in that case it would not be confusing. Maybe you can bring it up for discussion to them? My proposal got accepted :)

clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidCaptureDefaultWhenCapturingThisCheck.h
0

Sure, I can fix in a separate NFC patch. I'd like to focus on matching functionality against the updated rules in this patch.

clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/avoid-capture-default-when-capturing-this.rst
11

Good point, I can fix in a separate patch together with your previous comment!

PiotrZSL added inline comments.Apr 15 2023, 1:41 AM
clang-tools-extra/clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp
54

GPT: "Both of these names convey the intended meaning, but the latter option, cppcoreguidelines-ambiguous-capture-default-by-value, is slightly clearer and more readable. It emphasizes that the ambiguity is related to the "capture-default" and its "by-value" aspect. This makes it easier for someone to understand the rule's purpose at a glance."

ME: "is word ambiguous correct here ?"

GPT: "The word "ambiguous" may not be the most accurate term to describe the issue in this context. A better term might be "misleading" since the behavior of by-value capture-defaults in member functions can lead to confusion or incorrect assumptions about how data members are captured.

Thus, a more appropriate name for the guideline could be cppcoreguidelines-misleading-capture-default-by-value. This name highlights that the capture-default by value can be misleading in terms of how data members are captured within member functions."

PiotrZSL added inline comments.Apr 15 2023, 1:45 AM
clang-tools-extra/clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp
54

But probably we can stay with current name, there is not much that we can do.
In future we can always change it to something else...

carlosgalvezp marked 2 inline comments as done.

Rename check to latest proposal

Fix check ordering.
Align check documentation across header, .rst file
and Release Notes.

Remove excessive whitespace

PiotrZSL accepted this revision.Apr 15 2023, 3:07 AM
Eugene.Zelenko added inline comments.Apr 15 2023, 7:34 AM
clang-tools-extra/clang-tidy/cppcoreguidelines/MisleadingCaptureDefaultByValueCheck.h
2

Split header comment.

carlosgalvezp marked an inline comment as done.Apr 15 2023, 7:49 AM
carlosgalvezp added inline comments.
clang-tools-extra/clang-tidy/cppcoreguidelines/MisleadingCaptureDefaultByValueCheck.h
2

Thanks, fixed in an NFC patch.