Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return statement will never be executed warning

I've recently come across with double return statement (one of them was added by mistake) in our project and wondering why compiler doesn't show a warning for that?!

Ok, I added -Wunreachable-code to other warning flags, but still no luck.

Got warning - with a code to execute after return statement:

enter image description here

Didn't get warning but still second return statement will never be executed. enter image description here

Even if I add something like this, still no warning

enter image description here

Is there extra warning flag for that, or compiler isn't smart enough?

like image 378
Injectios Avatar asked Oct 08 '14 10:10

Injectios


2 Answers

Good catch!

-Wunreachable-code does not report a warning and there is no other warning flag which would do.
Not even the Static Analyzer catches this mistake!

(Tested with XCode 6.1 GM 2)

like image 119
fluidsonic Avatar answered Oct 06 '22 01:10

fluidsonic


Wrap your code in pragma flags that will suppress that warning between the push and pop

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunreachable-code"
//Your code goes here
#pragma GCC diagnostic pop
like image 38
Albert Renshaw Avatar answered Oct 06 '22 00:10

Albert Renshaw