llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.8k stars 11.91k forks source link

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

Open JonasToth opened 7 years ago

JonasToth commented 7 years ago
Bugzilla Link 34609
Version unspecified
OS All
Attachments Example code that isn't diagnosed correctly, Output with only the move diagnosed
CC @davidstone

Extended Description

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

davidstone 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.