[parted-devel] eliminating switch vs. enum warnings

Jim Meyering jim at meyering.net
Thu Feb 15 23:22:11 CET 2007


I've made the code compile with gcc and pretty strict warning-enabling
flags.  But there are a few judgment calls.

There are a number of switches like this

	switch (fs_info->fat_type) {
		case FAT_TYPE_FAT16:
			return cluster * 2;

		case FAT_TYPE_FAT32:
			return cluster * 4;
	}

But that doesn't handle FAT_TYPE_FAT12.
So if fs_info->fat_type ever has that value,
execution just falls through.

A switch stmt like the above evokes a warning from gcc -Wall
about FAT_TYPE_FAT12 not being handled.  One way to fix it
is to add an "default:" case, making it equivalent, but warning-free:

	switch (fs_info->fat_type) {
		case FAT_TYPE_FAT16:
			return cluster * 2;

		case FAT_TYPE_FAT32:
			return cluster * 4;

		default:
			break;
	}

But what if the FAT_TYPE_FAT12 case is a "can't-happen" case?
Add a "PED_ASSERT(0, (void)0)"?
or don't bother and just add the "default: break;"?

So the above is one class of warned-about switch stmts.
The other main one looks like this:

        switch (ex_status) {
                case PED_EXCEPTION_CANCEL:
                        goto error_close_dev;

                case PED_EXCEPTION_UNHANDLED:
                        ped_exception_catch ();
                case PED_EXCEPTION_IGNORE:
                        break;
        }

The problem is that there are numerous other PED_EXCEPTION_* symbols,
yet they're essentially "can't happen" cases.  So use the
"PED_ASSERT(0, (void)0)" again?  Or just "default: break;"?

        switch (ex_status) {
                case PED_EXCEPTION_CANCEL:
                        goto error_close_dev;

                case PED_EXCEPTION_UNHANDLED:
                        ped_exception_catch ();
                case PED_EXCEPTION_IGNORE:
                        break;
                default:
                        break;
        }

My current patch takes the latter (perhaps more conservative)
approach, but I can go the other way, too.

I'll send the patch in the morning, after I go through it at least
one more time.

For now, here's a summary:

 debug/clearfat/clearfat.c        |    5 +
 libparted/arch/linux.c           |   26 ++++++----
 libparted/cs/constraint.c        |    3 -
 libparted/cs/geom.c              |    3 -
 libparted/debug.c                |    6 --
 libparted/disk.c                 |    5 -
 libparted/filesys.c              |    3 -
 libparted/fs/amiga/affs.c        |    3 -
 libparted/fs/amiga/amiga.c       |   34 -------------
 libparted/fs/amiga/apfs.c        |    3 -
 libparted/fs/amiga/asfs.c        |    4 -
 libparted/fs/ext2/ext2.c         |    3 -
 libparted/fs/ext2/ext2_mkfs.c    |    3 -
 libparted/fs/fat/bootsector.c    |    6 +-
 libparted/fs/fat/calc.c          |    4 -
 libparted/fs/fat/clstdup.c       |    3 -
 libparted/fs/fat/count.c         |    7 +-
 libparted/fs/fat/fat.c           |    3 -
 libparted/fs/fat/resize.c        |   10 +++
 libparted/fs/fat/table.c         |   30 +++++++++--
 libparted/fs/fat/traverse.c      |    3 +
 libparted/fs/hfs/file_plus.c     |   10 ++-
 libparted/fs/hfs/hfs.c           |    9 +--
 libparted/fs/hfs/hfs.h           |    3 -
 libparted/fs/reiserfs/reiserfs.c |    9 +--
 libparted/labels/aix.c           |    8 ---
 libparted/labels/dos.c           |    6 --
 libparted/labels/dvh.c           |   10 +--
 libparted/labels/fdasd.c         |  101 ---------------------------------------
 libparted/labels/gpt.c           |    6 --
 libparted/labels/loop.c          |    4 -
 libparted/labels/mac.c           |    5 -
 libparted/labels/pc98.c          |    6 --
 libparted/labels/sun.c           |    3 -
 libparted/unit.c                 |    3 -
 parted/parted.c                  |   52 +++++++-------------
 parted/ui.c                      |    5 -
 37 files changed, 124 insertions(+), 283 deletions(-)



More information about the parted-devel mailing list