Skip to contentSkip to conversation

zfcp: drop default switch case which might paper over missing case

PATCH Steffen Maier 1 participant 1 message Nov 22, 2018 1 message not in the archive s390

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw
Quoted 5 lines · Hannes Reinecke
Quoted 4 lines · Steffen Maier

would now
suppress helpful -Wswitch compiler warnings when building with W=1

But then again, only with W=1 we would notice unhandled enum cases.

that's the only caveat

Quoted 2 lines
Quoted 2 lines

Without the default cases and a missed unhandled enum case, the code might perform unforeseen things we might not want...

this would be a bug that needs fixing

Quoted 5 lines
Quoted 5 lines

As of today, we never run through the removed default case, so removing it is no functional change.
In the future, we never should run through a default case but

introduce the necessary specific case(s) to handle new functionality.

that's the fix

Quoted 8 lines
Quoted 8 lines
drivers/s390/scsi/zfcp_erp.c +0 -0
index b2845c5b8106..9345fed3bb37 100644
@@ -151,9 +151,6 @@ static enum zfcp_erp_act_type zfcp_erp_handle_failed(
 ����������������� adapter, ZFCP_STATUS_COMMON_ERP_FAILED);
 ��������� }
 ��������� break;
Quoted 8 lines

drivers/s390/scsi/zfcp_erp.c static enum zfcp_erp_act_type zfcp_erp_handle_failed( · line 156

Quoted 6 lines
drivers/s390/scsi/zfcp_erp.c +0 -3
-��� default:
-������� need = 0;
-������� break;
 ����� }
 ����� return need;
 

If you 'should' not run through this code path, doesn't it warrant a WARN_ON() or something?

#include <stdio.h>
enum foo { A, B };

int main(int argc, char *argv[]) {
	enum foo f = argc - 1;
	switch (f) {
	case A: printf("A\n"); break;
	case B: printf("B\n"); break;
	default: printf("default\n"); break;
	}
	return 0;
}
$ gcc -Wswitch -Wall -g -o Wswitch Wswitch.c

Removing case B (while keeping default:) does not warn on build. Removing case B and default: nicely warns on build.

Hopefully I haven't missed anything. From above, I conclude:

A runtime check would require the introduction of a default case. Adding a default case would trade a static build warning for a runtime WARN_ON(_ONCE) which only appears if one manages to get the code run into the default case that should not happen.

I find the static build warning more helpful for future extensions adding more value(s) to the enum. Without a default case, we always getting a build warning for missing switch cases.

Signature · 10 lines
Mit freundlichen Gruessen / Kind regards
Steffen Maier

Linux on IBM Z Development

IBM Deutschland Research & Development GmbH
Vorsitzende des Aufsichtsrats: Martina Koederitz
Geschaeftsfuehrung: Dirk Wittkopp
Sitz der Gesellschaft: Boeblingen
Registergericht: Amtsgericht Stuttgart, HRB 243294