Bug 87029 - Add -Wredundant-move warning
Summary: Add -Wredundant-move warning
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: unknown
: P3 enhancement
Target Milestone: ---
Assignee: Marek Polacek
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2018-08-20 14:14 UTC by Marek Polacek
Modified: 2023-12-30 02:25 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2018-08-20 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Marek Polacek 2018-08-20 14:14:04 UTC
Similarly to 86981, we could also warn here about the redundant (but not pessimizing) move here:

#include <utility>

struct S { };

S
foo (S s)
{
  return std::move (s);
}

the compiler will try a move operation regardless the std::move (if the move ctor is present and accessible).
Unless the parameter is const, of course.
Comment 1 Marek Polacek 2018-08-20 14:14:59 UTC
This should be a fairly easy extension of -Wpessimizing-move.
Comment 2 Eric Gallager 2018-08-20 19:29:01 UTC
See also: bug 67906 and bug 81159
Comment 3 Marek Polacek 2018-08-26 16:46:24 UTC
Author: mpolacek
Date: Sun Aug 26 16:45:51 2018
New Revision: 263863

URL: https://gcc.gnu.org/viewcvs?rev=263863&root=gcc&view=rev
Log:
	PR c++/87029, Implement -Wredundant-move.
	* c.opt (Wredundant-move): New option.

	* typeck.c (treat_lvalue_as_rvalue_p): New function.
	(maybe_warn_pessimizing_move): Call convert_from_reference.
	Warn about redundant moves.

	* doc/invoke.texi: Document -Wredundant-move.

	* g++.dg/cpp0x/Wredundant-move1.C: New test.
	* g++.dg/cpp0x/Wredundant-move2.C: New test.
	* g++.dg/cpp0x/Wredundant-move3.C: New test.
	* g++.dg/cpp0x/Wredundant-move4.C: New test.

Added:
    trunk/gcc/testsuite/g++.dg/cpp0x/Wredundant-move1.C
    trunk/gcc/testsuite/g++.dg/cpp0x/Wredundant-move2.C
    trunk/gcc/testsuite/g++.dg/cpp0x/Wredundant-move3.C
    trunk/gcc/testsuite/g++.dg/cpp0x/Wredundant-move4.C
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/c-family/ChangeLog
    trunk/gcc/c-family/c.opt
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/typeck.c
    trunk/gcc/doc/invoke.texi
    trunk/gcc/testsuite/ChangeLog
Comment 4 Marek Polacek 2018-08-26 16:49:16 UTC
Implemented for GCC 9.  This warning should still be extended to handle initializers, where the std::move can also be redundant.