This arose during the review of another patch.

We often omit the default case of a switch statement to allow the compiler to complain if an enum case has been missed. I found a few where that wasn't done yet, but it would make sense and would have found an omission in another patch.
From 37a03b420ce52f02119a0085f3c5f6666e3b44fb Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <pe...@eisentraut.org>
Date: Wed, 9 Nov 2022 14:52:33 +0100
Subject: [PATCH] Update some more ObjectType switch statements to not have
 default

This allows the compiler to complain if a case has been missed.  In
these instances, the tradeoff of having to list a few unneeded cases
to silence the compiler seems better than the risk of actually missing
one.
---
 src/backend/catalog/objectaddress.c | 26 +++++++++++--------
 src/backend/commands/dropcmds.c     | 39 +++++++++++++++++++++++++++--
 2 files changed, 53 insertions(+), 12 deletions(-)

diff --git a/src/backend/catalog/objectaddress.c 
b/src/backend/catalog/objectaddress.c
index c7de7232b890..f36581d106b2 100644
--- a/src/backend/catalog/objectaddress.c
+++ b/src/backend/catalog/objectaddress.c
@@ -960,7 +960,7 @@ ObjectAddress
 get_object_address(ObjectType objtype, Node *object,
                                   Relation *relp, LOCKMODE lockmode, bool 
missing_ok)
 {
-       ObjectAddress address;
+       ObjectAddress address = {InvalidOid, InvalidOid, 0};
        ObjectAddress old_address = {InvalidOid, InvalidOid, 0};
        Relation        relation = NULL;
        uint64          inval_count;
@@ -991,6 +991,7 @@ get_object_address(ObjectType objtype, Node *object,
                                                                                
                   &relation, lockmode,
                                                                                
                   missing_ok);
                                break;
+                       case OBJECT_ATTRIBUTE:
                        case OBJECT_COLUMN:
                                address =
                                        get_object_address_attribute(objtype, 
castNode(List, object),
@@ -1163,14 +1164,12 @@ get_object_address(ObjectType objtype, Node *object,
                                                                                
                                         missing_ok);
                                address.objectSubId = 0;
                                break;
-                       default:
-                               elog(ERROR, "unrecognized object type: %d", 
(int) objtype);
-                               /* placate compiler, in case it thinks elog 
might return */
-                               address.classId = InvalidOid;
-                               address.objectId = InvalidOid;
-                               address.objectSubId = 0;
+                               /* no default, to let compiler warn about 
missing case */
                }
 
+               if (!address.classId)
+                       elog(ERROR, "unrecognized object type: %d", (int) 
objtype);
+
                /*
                 * If we could not find the supplied object, return without 
locking.
                 */
@@ -2635,9 +2634,16 @@ check_object_ownership(Oid roleid, ObjectType objtype, 
ObjectAddress address,
                                aclcheck_error(ACLCHECK_NOT_OWNER, objtype,
                                                           
NameListToString(castNode(List, object)));
                        break;
-               default:
-                       elog(ERROR, "unrecognized object type: %d",
-                                (int) objtype);
+               case OBJECT_AMOP:
+               case OBJECT_AMPROC:
+               case OBJECT_DEFAULT:
+               case OBJECT_DEFACL:
+               case OBJECT_PUBLICATION_NAMESPACE:
+               case OBJECT_PUBLICATION_REL:
+               case OBJECT_USER_MAPPING:
+                       /* These are currently not supported or don't make 
sense here. */
+                       elog(ERROR, "unsupported object type: %d", (int) 
objtype);
+                       break;
        }
 }
 
diff --git a/src/backend/commands/dropcmds.c b/src/backend/commands/dropcmds.c
index 26157eb4e3f5..8bac58a9941a 100644
--- a/src/backend/commands/dropcmds.c
+++ b/src/backend/commands/dropcmds.c
@@ -480,10 +480,45 @@ does_not_exist_skipping(ObjectType objtype, Node *object)
                        msg = gettext_noop("publication \"%s\" does not exist, 
skipping");
                        name = strVal(object);
                        break;
-               default:
-                       elog(ERROR, "unrecognized object type: %d", (int) 
objtype);
+
+               case OBJECT_COLUMN:
+               case OBJECT_DATABASE:
+               case OBJECT_FOREIGN_TABLE:
+               case OBJECT_INDEX:
+               case OBJECT_MATVIEW:
+               case OBJECT_ROLE:
+               case OBJECT_SEQUENCE:
+               case OBJECT_SUBSCRIPTION:
+               case OBJECT_TABLE:
+               case OBJECT_TABLESPACE:
+               case OBJECT_VIEW:
+                       /*
+                        * These are handled elsewhere, so if someone gets here 
the code
+                        * is probably wrong or should be revisited.
+                        */
+                       elog(ERROR, "unsupported object type: %d", (int) 
objtype);
+                       break;
+
+               case OBJECT_AMOP:
+               case OBJECT_AMPROC:
+               case OBJECT_ATTRIBUTE:
+               case OBJECT_DEFAULT:
+               case OBJECT_DEFACL:
+               case OBJECT_DOMCONSTRAINT:
+               case OBJECT_LARGEOBJECT:
+               case OBJECT_PARAMETER_ACL:
+               case OBJECT_PUBLICATION_NAMESPACE:
+               case OBJECT_PUBLICATION_REL:
+               case OBJECT_TABCONSTRAINT:
+               case OBJECT_USER_MAPPING:
+                       /* These are currently not used or needed. */
+                       elog(ERROR, "unsupported object type: %d", (int) 
objtype);
                        break;
+
+                       /* no default, to let compiler warn about missing case 
*/
        }
+       if (!msg)
+               elog(ERROR, "unrecognized object type: %d", (int) objtype);
 
        if (!args)
                ereport(NOTICE, (errmsg(msg, name)));
-- 
2.38.1

Reply via email to