This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] bugprone-use-after-move: Ctor arguments should be sequenced if ctor call is written as list-initialization.
ClosedPublic

Authored by mboehme on Apr 12 2023, 3:57 AM.

Details

Summary

See

https://timsong-cpp.github.io/cppwp/n4868/dcl.init#list-4

This eliminates a false positive in bugprone-use-after-move; this newly added
test used to be falsely classified as a use-after-move:

A a;
S3 s3{a.getInt(), std::move(a)};

Diff Detail

Event Timeline

mboehme created this revision.Apr 12 2023, 3:57 AM
mboehme requested review of this revision.Apr 12 2023, 3:57 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 12 2023, 3:57 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
njames93 added inline comments.Apr 13 2023, 11:07 AM
clang-tools-extra/test/clang-tidy/checkers/bugprone/use-after-move.cpp
1163

Whats with this namespace addition? looks unnecessary and should be removed

1204

Whats with the todo comment, surery a comment explaining that this shouldn't trigger a warning should suffice

mboehme updated this revision to Diff 513515.Apr 14 2023, 3:24 AM

Changes in response to review comments.

mboehme marked 2 inline comments as done.Apr 14 2023, 3:27 AM
mboehme added inline comments.
clang-tools-extra/test/clang-tidy/checkers/bugprone/use-after-move.cpp
1163

I'd like to avoid the definitions of the structs with short names spilling out into the global namespace and possibly conflicting with other definitions.

The structs used to be defined within the function initializerListSequences(), which is better, but I now need a class template S3, and those can't be defined with a function, so I decided to add a namespace. There are other places in this file that do the same, and I'm following that example. It's probably a better idea to actually name the namespace though, so I've done that now. WDYT?

1204

Sorry, this was a note-to-self that I neglected to address before uploading the patch. I've replaced this with a more meaningful comment.

The commit message doesn't really tell me "what" this commit is fixing, it only points to a section of the Standard. It talks about "a false positive" but it doesn't tell what this FP is about. Could you write a little bit more about what the problem is? Preferably if you can link a Github issue describing the problem. The subject of the commit message should indicate which particular check it relates to.

Document change in release notes.

Tests in lines 1195, 1201 actually tests InitListExpr.
Test in line 1207 test negative scenario of CXXConstructExpr (list), but I didn't found any positive test for CXXConstructExpr with list expr, please add such test to show that warnings are still produced for such case.

mboehme updated this revision to Diff 516390.Apr 24 2023, 6:47 AM
mboehme marked 2 inline comments as done.

Changes in response to review comments, and rebased to head.

mboehme retitled this revision from [clang-tidy] Ctor arguments are sequenced if ctor call is written as list-initialization. to [clang-tidy] bugprone-use-after-move: Ctor arguments should be sequenced if ctor call is written as list-initialization..Apr 24 2023, 6:47 AM
mboehme edited the summary of this revision. (Show Details)Apr 24 2023, 6:50 AM

The commit message doesn't really tell me "what" this commit is fixing, it only points to a section of the Standard. It talks about "a false positive" but it doesn't tell what this FP is about. Could you write a little bit more about what the problem is? Preferably if you can link a Github issue describing the problem.

I've expanded the patch description to reference the specific test added here that used to be falsely diagnosed as a use-after-free.

I hope this is acceptable in lieu of a github issue?

The subject of the commit message should indicate which particular check it relates to.

Done.

Document change in release notes.

Done.

Tests in lines 1195, 1201 actually tests InitListExpr.

That's understood. (These tests existed before this patch.)

Test in line 1207 test negative scenario of CXXConstructExpr (list), but I didn't found any positive test for CXXConstructExpr with list expr, please add such test to show that warnings are still produced for such case.

Good point, done.

PiotrZSL added inline comments.Apr 24 2023, 7:24 AM
clang-tools-extra/docs/ReleaseNotes.rst
216–220
NOTE: Consider: "Improved XYZ check by including coverage for constructor initializers, and by correcting the handling of constructor arguments when they are sequenced during a constructor call that is written as list-initialization." or "The XYZ check now covers constructor initializers and handles constructor arguments correctly during list-initialization."

I just pointing this because I don't like this "check: Also"

mboehme updated this revision to Diff 517094.Apr 26 2023, 1:45 AM

Reworded release notes.

mboehme updated this revision to Diff 517101.Apr 26 2023, 2:01 AM

Reworded release notes.

clang-tools-extra/docs/ReleaseNotes.rst
216–220

Makes sense. How about this? (I have more fixes in the pipeline, so I've gone for two separate sentences for the two fixes instead of a single sentence.)

PiotrZSL accepted this revision.Apr 26 2023, 2:35 AM

+- LGTM

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

':' -> '.'

This revision is now accepted and ready to land.Apr 26 2023, 2:35 AM
mboehme updated this revision to Diff 519437.May 4 2023, 4:36 AM

Fix typo and rebase to head

mboehme marked an inline comment as done.May 4 2023, 4:38 AM

Sorry, missed your LGTM. Will wait for pre-merge checks to complete and will then land.

PiotrZSL accepted this revision.May 4 2023, 4:44 AM