[Bug c++/85043] -Wuseless-cast false positive for temporary objects; add separate -Wcast-to-the-same-type to cover that case instead

cvs-commit at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Wed Oct 19 19:30:33 GMT 2022


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85043

--- Comment #15 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The trunk branch has been updated by Marek Polacek <mpolacek@gcc.gnu.org>:

https://gcc.gnu.org/g:b3c98d6a59a6dcd5b0b52bd5676b586ef4fe785f

commit r13-3388-gb3c98d6a59a6dcd5b0b52bd5676b586ef4fe785f
Author: Marek Polacek <polacek@redhat.com>
Date:   Tue Oct 18 12:20:14 2022 -0400

    c++: Mitigate -Wuseless-cast with classes [PR85043]

    -Wuseless-cast (not part of -Wall/-Wextra) warns here:

      struct S { };
      void g (S&&);
      void f (S&& arg)
      {
        g (S(arg)); // warning: useless cast to type 'struct S'
      }

    which is wrong: the code will not compile without the cast because
    "arg" is an lvalue which cannot bind to S&&.

    This patch disables the warning when an object that isn't a prvalue
    is cast to a non-reference type.  Therefore we still warn about the
    useless cast in "X(X{})".

            PR c++/85043

    gcc/cp/ChangeLog:

            * typeck.cc (maybe_warn_about_useless_cast): Don't warn when
            a glvalue is cast to a non-reference type.

    gcc/ChangeLog:

            * doc/invoke.texi: Update documentation of -Wuseless-cast.

    gcc/testsuite/ChangeLog:

            * g++.dg/warn/Wuseless-cast.C: Remove dg-warning.
            * g++.dg/warn/Wuseless-cast3.C: New test.


More information about the Gcc-bugs mailing list