mgehre / llvm-project

The home of the clang-based implementation of lifetime safety warnings.
39 stars 4 forks source link

Reference to temporary local variable not detected #91

Open DoDoENT opened 4 years ago

DoDoENT commented 4 years ago

Consider this example:

#include <iostream>
#include <type_traits>
#include <vector>

class VectorHolder {
private:
    std::vector< int > vec_;
public:
    VectorHolder( std::vector< int > && vec ): vec_( std::move( vec ) ) {}
    auto const & vec() const { return vec_; }
};

template< typename ... T >
auto packToVector( T ... args ) requires ( std::is_same_v< T, int > && ... ) {
    return VectorHolder{ std::vector< int >{ args... } };
}

int main() {
    for ( auto i : packToVector( 1, 2, 3, 4, 5 ).vec() ) {
        std::cout << i << ' ';
    }
    return 0;
}

Should this bug be auto-detectable by -Wlifetime? Or it is required to annotate vec method with [[ gsl::post( lifetime, { this } ) ]] (AFAIK, this attribute is still not supported).

Xazax-hun commented 4 years ago

Unfortunately, members are partially supported at this point. Once it is fully implemented, I think this specific example should work without annotations. Thanks for reporting this!

The gsl::post annotation is also only partially supported at this point. Unfortunately, I have hard time to find time working on this lately.