https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78736

prathamesh3492 at gcc dot gnu.org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |prathamesh3492 at gcc dot 
gnu.org

--- Comment #5 from prathamesh3492 at gcc dot gnu.org ---
clang emits a similar diagnostic with -Wenum-conversion.
I suppose we could also add a similar option that warns when
an enum is implicitly converted from one enum type to other ?

With the following untested patch:

diff --git a/gcc/c/c-typeck.c b/gcc/c/c-typeck.c
index f0917ed..2a6e656 100644
--- a/gcc/c/c-typeck.c
+++ b/gcc/c/c-typeck.c
@@ -6255,6 +6255,19 @@ convert_for_assignment (location_t location, location_t
expr_loc, tree type,
        }
     }

+  {
+    tree checktype = origtype != NULL_TREE ? origtype : rhstype;
+    if (checktype != error_mark_node
+       && TREE_CODE (checktype) == ENUMERAL_TYPE
+       && TREE_CODE (type) == ENUMERAL_TYPE
+       && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
+      {
+        gcc_rich_location loc (location);
+       warning_at_rich_loc (&loc, 0, "implicit conversion from"
+                   " enum type %qT to %qT", checktype, type);
+      }
+  }
+
   if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
     return rhs;

gcc emits the diagnostic:
78736.c:5:16: warning: implicit conversion from enum
type ‘enum <anonymous>’ to ‘name2_t {aka enum <anonymous>}’
 name2_t name = brandon;
                ^~~~~~~

Is this patch in the right direction ?
Working on a fair patch.

Thanks,
Prathamesh

Reply via email to