https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80544
Jonathan Wakely <redi at gcc dot gnu.org> changed: What |Removed |Added ---------------------------------------------------------------------------- See Also| |https://gcc.gnu.org/bugzill | |a/show_bug.cgi?id=49395 --- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> --- I tried to add a warning like this: --- a/gcc/cp/typeck.c +++ b/gcc/cp/typeck.c @@ -6633,6 +6633,10 @@ maybe_warn_about_useless_cast (tree type, tree expr, tsubst_flags_t complain) || same_type_p (TREE_TYPE (expr), type)) warning (OPT_Wuseless_cast, "useless cast to type %qT", type); } + else if (complain & tf_warning && !CLASS_TYPE_P (type) + && (TREE_CODE (type) != ARRAY_TYPE) + && (CP_TYPE_CONST_P (type) || CP_TYPE_VOLATILE_P (type))) + warning (0, "cv-qualifier ignored in cast"); } /* Convert EXPR (an expression with pointer-to-member type) to TYPE The warning works for the examples above, but also produces warnings for this valid C++17 code: enum class E { }; E e{1}; It also causes a dozen or so FAILs in the testsuite, which in some cases are due to useless cv-qualifiers that should be ignroed, and in some cases might need to be suppressed with a -Wno-xxx option (because the test is intentionally casting to a const type). This attempts to fix the actual bug, and seems to work OK: --- a/gcc/cp/typeck.c +++ b/gcc/cp/typeck.c @@ -6708,6 +6708,10 @@ build_static_cast_1 (tree type, tree expr, bool c_cast_p, /* Save casted types in the function's used types hash table. */ used_types_insert (type); + /* Result of cast to pointer is prvalue of cv-unqualified type. */ + if (TREE_CODE (type) == POINTER_TYPE) + type = cv_unqualified (type); + /* [expr.static.cast] An lvalue of type "cv1 B", where B is a class type, can be cast @@ -7070,6 +7074,10 @@ build_reinterpret_cast_1 (tree type, tree expr, bool c_cast_p, /* Save casted types in the function's used types hash table. */ used_types_insert (type); + /* Result of cast to pointer is prvalue of cv-unqualified type. */ + if (TREE_CODE (type) == POINTER_TYPE) + type = cv_unqualified (type); + /* [expr.reinterpret.cast] An lvalue expression of type T1 can be cast to the type "reference to T2" if an expression of type "pointer to T1" can be @@ -7300,6 +7308,10 @@ build_const_cast_1 (tree dst_type, tree expr, tsubst_flags_t complain, /* Save casted types in the function's used types hash table. */ used_types_insert (dst_type); + /* Result of cast to pointer is prvalue of cv-unqualified type. */ + if (TREE_CODE (dst_type) == POINTER_TYPE) + dst_type = cv_unqualified (dst_type); + src_type = TREE_TYPE (expr); /* Expressions do not really have reference types. */ if (TREE_CODE (src_type) == REFERENCE_TYPE)