This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Fix bug in bugprone-use-after-move check
ClosedPublic

Authored by ymandel on Sep 6 2019, 10:48 AM.

Details

Summary

The bugprone-use-after-move check exhibits false positives for certain uses of
the C++17 if/switch init statements. These false positives are caused by a bug
in the ExprSequence calculations.

This revision adds tests for the false positives and fixes the corresponding
sequence calculation.

Patch by Shuai Wang.

Diff Detail

Repository
rL LLVM

Event Timeline

ymandel created this revision.Sep 6 2019, 10:48 AM
Herald added a project: Restricted Project. · View Herald TranscriptSep 6 2019, 10:48 AM
Herald added a subscriber: xazax.hun. · View Herald Transcript
ymandel edited the summary of this revision. (Show Details)Sep 6 2019, 11:01 AM
gribozavr accepted this revision.Sep 6 2019, 11:34 AM

Thanks for the quick fix!

clang-tools-extra/test/clang-tidy/bugprone-use-after-move.cpp
1198 ↗(On Diff #219140)

Unless you think it is redundant, could you also add

if (A a1; A(std::move(a2)).getInt() > 0) {}

Also some true positive tests would be good:

if (A a1; A(std::move(a2)).getInt() > A(std::move(a2)).getInt()) {}
A a1;
if (A a2 = std::move(a1); A(std::move(a1)) > 0) {}
This revision is now accepted and ready to land.Sep 6 2019, 11:34 AM
ymandel updated this revision to Diff 219152.Sep 6 2019, 12:05 PM

Added tests.

ymandel marked 2 inline comments as done.Sep 6 2019, 12:07 PM
ymandel added inline comments.
clang-tools-extra/test/clang-tidy/bugprone-use-after-move.cpp
1198 ↗(On Diff #219140)

Done, but any idea why everything in this function is placed inside a loop? Looks like its just for scoping, but then why not just a compound statement, as is done above? This feels very odd.

gribozavr added inline comments.Sep 6 2019, 1:49 PM
clang-tools-extra/test/clang-tidy/bugprone-use-after-move.cpp
1198 ↗(On Diff #219140)

I think it is to ensure that the checker understands the sequencing. If it didn't, then the loop would trigger the "moved twice" logic.

mboehme added inline comments.
clang-tools-extra/test/clang-tidy/bugprone-use-after-move.cpp
1198 ↗(On Diff #219140)

(Original author of the test here.)

Correct. I should probably have added a comment explaining this when I wrote the test. Feel free to add such a comment.

This revision was automatically updated to reflect the committed changes.
ymandel marked an inline comment as done.
Herald added a project: Restricted Project. · View Herald TranscriptSep 9 2019, 5:57 AM
ymandel marked 3 inline comments as done.Sep 9 2019, 5:58 AM
ymandel added inline comments.
clang-tools-extra/test/clang-tidy/bugprone-use-after-move.cpp
1198 ↗(On Diff #219140)

Thanks for clarifying. I added a sentence to the comment as you suggested.