Warning when a variable declared by rvalue goes out of scope not moved from

Is there a way to make a compiler (clang ++ or g ++) to report the following situations:

static_assert(!std::is_lvalue_reference< T >::value); void g(T); void f(T x) { T y = x; #if 0 // turns warning off g(std::move(x)); #endif } // warning here: x goes out of scope, but not moved from 

or

 static_assert(!std::is_reference< T >::value); T f() { T x; T y = x; #if 0 // turns warning off return x; #else return {}; #endif } // warning here: x goes out of scope, but not moved from 

Another warning class must be activated when moving from a variable again:

 void f() { T x; T y = std::move(x); T z = x; // warning here: x is used since it has been moved from } 

Variables of non- std::unique_ptr classes such as std::unique_ptr rarely reused after their contents have been stolen.

It would be great if the warnings described above were available as a variable called through #pragma or an attribute, or globally for all variables that are classes with a user-defined operator and an assignment constructor.

It is sometimes difficult to track the lifetime of a variable.

There is an SSA form (a static form of single assignment) of an intermediate representation (for example, AST) used in modern compilers. Therefore, I believe that it is not so difficult for compilers to detect the above situations.

+2
c ++ c ++ 11 move-semantics warnings
source share
1 answer

No, today with GCC or Clang there is no way to do this.

The new [[nodiscard]] attribute coming in C ++ 17 is vaguely related, but not the same and cannot be used for any of your cases.

+2
source share

All Articles