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.