]> git.ipfire.org Git - thirdparty/gcc.git/commit
c++: fix -Wparentheses for bool-like class types
authorPatrick Palka <ppalka@redhat.com>
Thu, 21 Dec 2023 20:00:55 +0000 (15:00 -0500)
committerPatrick Palka <ppalka@redhat.com>
Thu, 21 Dec 2023 20:00:55 +0000 (15:00 -0500)
commit619a9539ee378e635ba3a26300dff746a9ff4ba2
tree4280bb426067ffac6f13db8d007d421aecf94a26
parent9a65c8ee659042babdb05ef15fea9910fa8d6e62
c++: fix -Wparentheses for bool-like class types

Since r14-4977-g0f2e2080685e75 we now issue a -Wparentheses warning for

  extern std::vector<bool> v;
  bool b = v[0] = true; // warning: suggest parentheses around assignment used as truth value [-Wparentheses]

I intended for that commit to just allow the existing diagnostics to
happen in a template context as well, but the refactoring of
is_assignment_op_expr_p caused us for this -Wparentheses warning from
convert_for_assignment to now consider user-defined operator= expressions
instead of just built-in operator=.  And since std::vector<bool> is really
a bitset, whose operator[] returns a class type with such a user-defined
operator= (taking bool), we now warn here when we didn't use to.

That we now accept user-defined operator= expressions is generally good,
but arguably "boolish" class types should be treated like ordinary bool
as far as the warning is concerned.  To that end this patch suppresses
the warning for such types, specifically when the class type can be
implicitly converted to and assigned from bool.  This criterion captures
the std::vector<bool>::reference of libstdc++ at least.

gcc/cp/ChangeLog:

* cp-tree.h (maybe_warn_unparenthesized_assignment): Add
'nested_p' bool parameter.
* semantics.cc (boolish_class_type_p_cache): Define.
(boolish_class_type_p): Define.
(maybe_warn_unparenthesized_assignment): Add 'nested_p'
bool parameter.  Suppress the warning for nested assignments
to bool and bool-like class types.
(maybe_convert_cond): Pass nested_p=false to
maybe_warn_unparenthesized_assignment.
* typeck.cc (convert_for_assignment): Pass nested_p=true to
maybe_warn_unparenthesized_assignment.  Remove now redundant
check for 'rhs' having bool type.

gcc/testsuite/ChangeLog:

* g++.dg/warn/Wparentheses-34.C: New test.
gcc/cp/cp-tree.h
gcc/cp/semantics.cc
gcc/cp/typeck.cc
gcc/testsuite/g++.dg/warn/Wparentheses-34.C [new file with mode: 0644]