diff --git a/compiler/sc1.cpp b/compiler/sc1.cpp index 4d13f63f4..7c6c4894a 100644 --- a/compiler/sc1.cpp +++ b/compiler/sc1.cpp @@ -145,7 +145,7 @@ static int doif(void); static int dowhile(void); static int dodo(void); static int dofor(void); -static void doswitch(void); +static int doswitch(void); static void doreturn(void); static void dotypedef(); static void dotypeset(); @@ -5432,8 +5432,7 @@ static void statement(int *lastindent,int allow_decl) lastst=dofor(); break; case tSWITCH: - doswitch(); - lastst=tSWITCH; + lastst=doswitch(); break; case tCASE: case tDEFAULT: @@ -5928,7 +5927,7 @@ static int dofor(void) * param = table offset (code segment) * */ -static void doswitch(void) +static int doswitch(void) { int lbl_table,lbl_exit,lbl_case; int swdefault,casecount; @@ -5938,6 +5937,7 @@ static void doswitch(void) constvalue caselist = { NULL, "", 0, 0}; /* case list starts empty */ constvalue *cse,*csp; char labelname[sNAMEMAX+1]; + int return_statement = tSWITCH; endtok= matchtoken('(') ? ')' : tDO; doexpr(TRUE,FALSE,FALSE,FALSE,NULL,NULL,TRUE);/* evaluate switch expression */ @@ -6015,6 +6015,8 @@ static void doswitch(void) needtoken(':'); swdefault=TRUE; statement(NULL,FALSE); + if (lastst == tRETURN) + return_statement = tRETURN; /* Jump to lbl_exit, even thouh this is the last clause in the * switch, because the jump table is generated between the last * clause of the switch and the exit label. @@ -6054,6 +6056,7 @@ static void doswitch(void) setlabel(lbl_exit); delete_consttable(&caselist); /* clear list of case labels */ + return return_statement; } static void doassert(void) diff --git a/tests/compile-only/ok-return-in-default-case.sp b/tests/compile-only/ok-return-in-default-case.sp new file mode 100644 index 000000000..04819fd3a --- /dev/null +++ b/tests/compile-only/ok-return-in-default-case.sp @@ -0,0 +1,14 @@ +// warnings_are_errors: true +int main() { + funct(2); +} +int funct(int input) { + switch (input) { + case 0: + return 1; + case 1: + return 2; + default: + return 3; + } +}