On Fri, 25 Mar 2022 at 05:58, Jason Merrill wrote: > > > >>> + if (current_function_decl > >>> + && (DECL_CONSTRUCTOR_P (current_function_decl) > >>> + || DECL_DESTRUCTOR_P (current_function_decl)) > >>> + && TREE_CODE (expr) == NOP_EXPR > >>> + && is_this_parameter (TREE_OPERAND (expr, 0))) > >> > >> I think the resolves_to_fixed_type_p function would be useful here; > >> you're testing for a subset of the cases it handles. > > > > Does the new patch look like how you intended it to? The new patch > > passes all regression tests and newly added tests. However, I don't > > fully understand the code for resolves_to_fixed_type_p() and > > fixed_type_or_null(), except that I see that they contain logic to > > return -1 when within a ctor/dtor. > > > + if (TREE_CODE (expr) == NOP_EXPR > > + && resolves_to_fixed_type_p (TREE_OPERAND (expr, 0)) == -1) > > Why not just pass expr itself to resolves_to_fixed_type_p? You're right. I didn't realise that fixed_type_or_null handles the NOP_EXPR case as part of CASE_CONVERT. > We might as well also warn if it returns >0, e.g. > > struct A { } a; > struct B: A { }; > auto p = static_cast(&a); // undefined > > You should also handle reference casts. I've made some changes in v3 to handle reference casts and the case where resolves_to_fixed_type_p > 0. I've also slightly modified the patch title to reflect the new changes. Thanks as always! v2: https://gcc.gnu.org/pipermail/gcc-patches/2022-March/591894.html Changes since v2: - Handle reference casts and add new tests. - Warn if resolves_to_fixed_type_p > 0 to handle more general cases. v1: https://gcc.gnu.org/pipermail/gcc-patches/2022-March/591683.html - Add new warning flag. - Use resolves_to_fixed_type_p.