c++: Extend -Wredundant-move for const-qual objects [PR90428]
In this PR, Jon suggested extending the -Wredundant-move warning to warn when the user is moving a const object as in: struct T { }; T f(const T& t) { return std::move(t); } where the std::move is redundant, because T does not have a T(const T&&) constructor (which is very unlikely). Even with the std::move, T(T&&) would not be used because it would mean losing the const. Instead, T(const T&) will be called. I had to restructure the function a bit, but it's better now. This patch depends on my other recent patches to maybe_warn_pessimizing_move. PR c++/90428 gcc/cp/ChangeLog: * typeck.cc (can_do_rvo_p): Rename to ... (can_elide_copy_prvalue_p): ... this. (maybe_warn_pessimizing_move): Extend the -Wredundant-move warning to warn about std::move on a const-qualified object. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/Wredundant-move1.C: Adjust dg-warning. * g++.dg/cpp0x/Wredundant-move9.C: Likewise. * g++.dg/cpp0x/Wredundant-move10.C: New test.
This commit is contained in:
parent
6602a2b2de
commit
6c136d53e8
4 changed files with 162 additions and 60 deletions
|
@ -10301,7 +10301,7 @@ can_do_nrvo_p (tree retval, tree functype)
|
|||
prvalue. */
|
||||
|
||||
static bool
|
||||
can_do_rvo_p (tree retval, tree functype)
|
||||
can_elide_copy_prvalue_p (tree retval, tree functype)
|
||||
{
|
||||
if (functype == error_mark_node)
|
||||
return false;
|
||||
|
@ -10415,36 +10415,38 @@ maybe_warn_pessimizing_move (tree expr, tree type, bool return_p)
|
|||
return;
|
||||
}
|
||||
|
||||
/* We're looking for *std::move<T&> ((T &) &arg). */
|
||||
if (REFERENCE_REF_P (expr)
|
||||
&& TREE_CODE (TREE_OPERAND (expr, 0)) == CALL_EXPR)
|
||||
{
|
||||
/* First, check if this is a call to std::move. */
|
||||
if (!REFERENCE_REF_P (expr)
|
||||
|| TREE_CODE (TREE_OPERAND (expr, 0)) != CALL_EXPR)
|
||||
return;
|
||||
tree fn = TREE_OPERAND (expr, 0);
|
||||
if (is_std_move_p (fn))
|
||||
{
|
||||
if (!is_std_move_p (fn))
|
||||
return;
|
||||
tree arg = CALL_EXPR_ARG (fn, 0);
|
||||
tree moved;
|
||||
if (TREE_CODE (arg) != NOP_EXPR)
|
||||
return;
|
||||
/* If we're looking at *std::move<T&> ((T &) &arg), do the pessimizing N/RVO
|
||||
and implicitly-movable warnings. */
|
||||
if (TREE_CODE (TREE_OPERAND (arg, 0)) == ADDR_EXPR)
|
||||
{
|
||||
arg = TREE_OPERAND (arg, 0);
|
||||
if (TREE_CODE (arg) != ADDR_EXPR)
|
||||
return;
|
||||
arg = TREE_OPERAND (arg, 0);
|
||||
arg = convert_from_reference (arg);
|
||||
if (can_do_rvo_p (arg, type))
|
||||
if (can_elide_copy_prvalue_p (arg, type))
|
||||
{
|
||||
auto_diagnostic_group d;
|
||||
if (warning_at (loc, OPT_Wpessimizing_move,
|
||||
"moving a temporary object prevents copy "
|
||||
"elision"))
|
||||
"moving a temporary object prevents copy elision"))
|
||||
inform (loc, "remove %<std::move%> call");
|
||||
}
|
||||
/* The rest of the warnings is only relevant for when we are
|
||||
returning from a function. */
|
||||
else if (!return_p)
|
||||
/* The rest of the warnings is only relevant for when we are returning
|
||||
from a function. */
|
||||
if (!return_p)
|
||||
return;
|
||||
|
||||
tree moved;
|
||||
/* Warn if we could do copy elision were it not for the move. */
|
||||
else if (can_do_nrvo_p (arg, type))
|
||||
if (can_do_nrvo_p (arg, type))
|
||||
{
|
||||
auto_diagnostic_group d;
|
||||
if (!warning_suppressed_p (expr, OPT_Wpessimizing_move)
|
||||
|
@ -10470,7 +10472,15 @@ maybe_warn_pessimizing_move (tree expr, tree type, bool return_p)
|
|||
tf_none);
|
||||
/* If this worked, implicit rvalue would work, so the call to
|
||||
std::move is redundant. */
|
||||
if (t != error_mark_node)
|
||||
if (t != error_mark_node
|
||||
/* Trying to move something const will never succeed unless
|
||||
there's T(const T&&), which it almost never is, and if
|
||||
so, T wouldn't be error_mark_node now: the above convert_
|
||||
call with LOOKUP_PREFER_RVALUE returns an error if a const T&
|
||||
overload is selected. */
|
||||
|| (CP_TYPE_CONST_P (TREE_TYPE (arg))
|
||||
&& same_type_ignoring_top_level_qualifiers_p
|
||||
(TREE_TYPE (arg), type)))
|
||||
{
|
||||
auto_diagnostic_group d;
|
||||
if (warning_at (loc, OPT_Wredundant_move,
|
||||
|
@ -10479,6 +10489,35 @@ maybe_warn_pessimizing_move (tree expr, tree type, bool return_p)
|
|||
}
|
||||
}
|
||||
}
|
||||
/* Also try to warn about redundant std::move in code such as
|
||||
T f (const T& t)
|
||||
{
|
||||
return std::move(t);
|
||||
}
|
||||
for which EXPR will be something like
|
||||
*std::move<const T&> ((const struct T &) (const struct T *) t)
|
||||
and where the std::move does nothing if T does not have a T(const T&&)
|
||||
constructor, because the argument is const. It will not use T(T&&)
|
||||
because that would mean losing the const. */
|
||||
else if (TYPE_REF_P (TREE_TYPE (arg))
|
||||
&& CP_TYPE_CONST_P (TREE_TYPE (TREE_TYPE (arg))))
|
||||
{
|
||||
tree rtype = TREE_TYPE (TREE_TYPE (arg));
|
||||
if (!same_type_ignoring_top_level_qualifiers_p (rtype, type))
|
||||
return;
|
||||
/* Check for the unlikely case there's T(const T&&) (we don't care if
|
||||
it's deleted). */
|
||||
for (tree fn : ovl_range (CLASSTYPE_CONSTRUCTORS (rtype)))
|
||||
if (move_fn_p (fn))
|
||||
{
|
||||
tree t = TREE_VALUE (FUNCTION_FIRST_USER_PARMTYPE (fn));
|
||||
if (UNLIKELY (CP_TYPE_CONST_P (TREE_TYPE (t))))
|
||||
return;
|
||||
}
|
||||
auto_diagnostic_group d;
|
||||
if (warning_at (loc, OPT_Wredundant_move,
|
||||
"redundant move in return statement"))
|
||||
inform (loc, "remove %<std::move%> call");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -60,7 +60,8 @@ fn4 (const T t)
|
|||
{
|
||||
// t is const: will decay into copy despite std::move, so it's redundant.
|
||||
// We used to warn about this, but no longer since c++/87378.
|
||||
return std::move (t); // { dg-warning "redundant move" "" { target c++20 } }
|
||||
// Now we warn again since c++/90428.
|
||||
return std::move (t); // { dg-warning "redundant move" }
|
||||
}
|
||||
|
||||
int
|
||||
|
|
61
gcc/testsuite/g++.dg/cpp0x/Wredundant-move10.C
Normal file
61
gcc/testsuite/g++.dg/cpp0x/Wredundant-move10.C
Normal file
|
@ -0,0 +1,61 @@
|
|||
// PR c++/90428
|
||||
// { dg-do compile { target c++11 } }
|
||||
// { dg-options "-Wredundant-move" }
|
||||
|
||||
// Define std::move.
|
||||
namespace std {
|
||||
template<typename _Tp>
|
||||
struct remove_reference
|
||||
{ typedef _Tp type; };
|
||||
|
||||
template<typename _Tp>
|
||||
struct remove_reference<_Tp&>
|
||||
{ typedef _Tp type; };
|
||||
|
||||
template<typename _Tp>
|
||||
struct remove_reference<_Tp&&>
|
||||
{ typedef _Tp type; };
|
||||
|
||||
template<typename _Tp>
|
||||
constexpr typename std::remove_reference<_Tp>::type&&
|
||||
move(_Tp&& __t) noexcept
|
||||
{ return static_cast<typename std::remove_reference<_Tp>::type&&>(__t); }
|
||||
}
|
||||
|
||||
struct T { T(); T(const T&); T(T&&) = delete; };
|
||||
struct S : T { };
|
||||
struct W { W(const W&); W(W&&) = delete; W(const W&&); };
|
||||
|
||||
T f1(T t)
|
||||
{
|
||||
const T& rt = t;
|
||||
return std::move(rt); // { dg-warning "redundant move" }
|
||||
}
|
||||
|
||||
T f2(const T& t)
|
||||
{
|
||||
return std::move(t); // { dg-warning "redundant move" }
|
||||
}
|
||||
|
||||
W f3(const W& w)
|
||||
{
|
||||
return std::move(w);
|
||||
}
|
||||
|
||||
T f4(const S& s)
|
||||
{
|
||||
return std::move(s);
|
||||
}
|
||||
|
||||
T f5(const T t)
|
||||
{
|
||||
return std::move(t); // { dg-warning "redundant move" }
|
||||
}
|
||||
|
||||
struct S1 { S1(S1 &&) = delete; S1(const S1&); };
|
||||
struct S2: S1 {};
|
||||
|
||||
S1 f3(const S2 s)
|
||||
{
|
||||
return std::move(s); // { dg-warning "redundant move" "" { target c++20 } }
|
||||
}
|
|
@ -61,7 +61,8 @@ fn4 (const T<int> t)
|
|||
{
|
||||
// t is const: will decay into copy despite std::move, so it's redundant.
|
||||
// We used to warn about this, but no longer since c++/87378.
|
||||
return std::move (t); // { dg-warning "redundant move" "" { target c++20 } }
|
||||
// Now we warn again since c++/90428.
|
||||
return std::move (t); // { dg-warning "redundant move" }
|
||||
}
|
||||
|
||||
int
|
||||
|
|
Loading…
Add table
Reference in a new issue