Quuxplusone / LLVMBugzillaTest

0 stars 0 forks source link

misc-use-after-move does not recognize std::forward and manual casting #33581

Open Quuxplusone opened 7 years ago

Quuxplusone commented 7 years ago
Bugzilla Link PR34609
Status CONFIRMED
Importance P normal
Reported by Jonas Toth (development@jonas-toth.eu)
Reported on 2017-09-14 12:23:41 -0700
Last modified on 2021-03-15 12:26:35 -0700
Version unspecified
Hardware All All
CC alexfh@google.com, davidfromonline@gmail.com, djasper@google.com, klimek@google.com
Fixed by commit(s)
Attachments test_moving.cpp (579 bytes, text/x-c++src)
output.txt (398 bytes, text/plain)
Blocks
Blocked by
See also

Created attachment 19156 Example code that isn't diagnosed correctly

This bugreport is result of a short discussion on the mailing list.

std::forward is a utitlity similar to std::move, which moves the argument conditionally. It is possible to initiate a move by manual casting as well.

An example that shows all of these possibility is added with the output, that only diagnoses std::move

Quuxplusone commented 7 years ago

Attached test_moving.cpp (579 bytes, text/x-c++src): Example code that isn't diagnosed correctly

Quuxplusone commented 7 years ago

Attached output.txt (398 bytes, text/plain): Output with only the move diagnosed

Quuxplusone commented 3 years ago

Reduced test case:

#include <utility>

void a() {
    int x;
    std::move(x);
    x;
}

void b() {
    int x;
    std::forward<int>(x);
    x;
}

void c() {
    int x;
    static_cast<int &&>(x);
    x;
}

when checked with

clang-tidy -checks=bugprone-use-after-move,-clang-diagnostic-unused-value use-after-move.cpp --

the only output is

5 warnings generated.
/home/david/test/use-after-move.cpp:6:5: warning: 'x' used after it was moved [bugprone-use-after-move]
    x;
    ^
/home/david/test/use-after-move.cpp:5:5: note: move occurred here
    std::move(x);
    ^
Suppressed 4 warnings (4 with check filters).

I agree that this check should catch both b and c, as both of them use x after moving from it.