From 47293da3149269ec799f05bc446aeb7ca0133cb3 Mon Sep 17 00:00:00 2001 From: Giovanni Bajo Date: Thu, 8 Jul 2004 10:03:59 +0000 Subject: re PR c++/16169 (-Weffc++ item 15 improvements) PR c++/16169 * typeck.c (check_return_expr): Improve -Weffc++ warning: handle returning CALL_EXPR, and non-reference return type. PR c++/16169 * g++.dg/warn/effc2.C: New test. From-SVN: r84283 --- gcc/cp/ChangeLog | 6 ++++++ gcc/cp/typeck.c | 26 +++++++++++++++++++++++--- 2 files changed, 29 insertions(+), 3 deletions(-) (limited to 'gcc/cp') diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 8b0bc21..8aa6b1c 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,9 @@ +2004-07-08 Giovanni Bajo + + PR c++/16169 + * typeck.c (check_return_expr): Improve -Weffc++ warning: handle + returning CALL_EXPR, and non-reference return type. + 2004-07-08 Nathan Sidwell * name-lookup.c (push_binding): Use VEC_reserve. diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c index 1588cbc..55b3ef3 100644 --- a/gcc/cp/typeck.c +++ b/gcc/cp/typeck.c @@ -5989,9 +5989,29 @@ check_return_expr (tree retval) /* Effective C++ rule 15. See also start_function. */ if (warn_ecpp - && DECL_NAME (current_function_decl) == ansi_assopname(NOP_EXPR) - && retval != current_class_ref) - warning ("`operator=' should return a reference to `*this'"); + && DECL_NAME (current_function_decl) == ansi_assopname(NOP_EXPR)) + { + bool warn = true; + + /* The function return type must be a reference to the current + class. */ + if (TREE_CODE (valtype) == REFERENCE_TYPE + && same_type_ignoring_top_level_qualifiers_p + (TREE_TYPE (valtype), TREE_TYPE (current_class_ref))) + { + /* Returning '*this' is obviously OK. */ + if (retval == current_class_ref) + warn = false; + /* If we are calling a function whose return type is the same of + the current class reference, it is ok. */ + else if (TREE_CODE (retval) == INDIRECT_REF + && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR) + warn = false; + } + + if (warn) + warning ("`operator=' should return a reference to `*this'"); + } /* The fabled Named Return Value optimization, as per [class.copy]/15: -- cgit v1.1