]> git.ipfire.org Git - thirdparty/gcc.git/commit
c++: Add diagnostic when operator= is used as truth cond [PR25689]
authorZhao Wei Liew <zhaoweiliew@gmail.com>
Tue, 15 Feb 2022 09:44:29 +0000 (17:44 +0800)
committerJason Merrill <jason@redhat.com>
Fri, 29 Apr 2022 02:57:54 +0000 (22:57 -0400)
commit654f6978cdc85a3970ff2c478d4df3e55cf4d3ab
tree0fc2caf7ab45e3612473ca30b0f76b852fa6ee9c
parent6b6f53d8afdb3744530a93e1f8dc00de69052493
c++: Add diagnostic when operator= is used as truth cond [PR25689]

When compiling the following code with g++ -Wparentheses, GCC does not
warn on the if statement. For example, there is no warning for this code:

struct A {
A& operator=(int);
operator bool();
};

void f(A a) {
if (a = 0); // no warning
}

This is because a = 0 is a call to operator=, which GCC does not handle.

This patch fixes this issue by handling calls to operator= when deciding
to warn.

Bootstrapped and regression tested on x86_64-pc-linux-gnu.

PR c++/25689

gcc/cp/ChangeLog:

* call.cc (extract_call_expr): Return a NULL_TREE on failure
instead of asserting.
(build_new_method_call): Suppress -Wparentheses diagnostic for
MODIFY_EXPR.
* semantics.cc (is_assignment_op_expr_p): Add function to check
if an expression is a call to an op= operator expression.
(maybe_convert_cond): Handle the case of a op= operator expression
for the -Wparentheses diagnostic.

gcc/testsuite/ChangeLog:

* g++.dg/warn/Wparentheses-31.C: New test.

Signed-off-by: Zhao Wei Liew <zhaoweiliew@gmail.com>
gcc/cp/call.cc
gcc/cp/semantics.cc
gcc/testsuite/g++.dg/warn/Wparentheses-31.C [new file with mode: 0644]