Does such a compiler option exist?

Having a function that is meant to return a value but does not can cause some pretty nasty bugs, but since determining whether a function returns a value is an undecidable problem, the compiler doesn't attempt to catch that kind of error. It seems like it would be pretty easy to have the compiler insert a runtime guard at the end of all functions that are meant to return values. The runtime guard would just make the program blow up with some message telling you your function failed to return a value, and then you don't need to sit around doing tests and figure out why gdb is so confused about what happened. Is there any compiler setting like that available? I mainly work with g++.
When you compile your code, the compiler should tell you if not all paths return a value. This is usually a warning, but it's there still.

What do you mean by not returning a value though. Did it return a value you didn't expect? or a null?
I think that you mean if you create a function that i supposed to return a value and you forget the return statement to have an error.
Something like the following
1
2
3
4
5
6
7
8
9
#include <iostream>
using namespace std;
int no_ret(){
   //whatever without a return statement
}
int main(){
   //your program
   return 0;
}

Then the compiler to show you a warning or an error message.

I think that g++ doesn't have this ability.
Last edited on
g++/gcc will tell you immediately that not all paths return a value. I actually think it's a compiler error, as opposed to warning.
Say I wrote this function:

1
2
3
int foo()
{
}


If I use -Wall, that gives a warning, but in any large group project there are tons of warnings and you never notice an important one like that.

If somewhere else I call foo:
 
cout << foo() << endl;


It happily returns -1079779132 or some other such garbage. It seems like it would be pretty easy to add a runtime check to prevent that behavior, basically something that would automatically modify foo to
1
2
3
4
5
int foo()
{

   assert(false); //This should be unreached
}

but in any large group project there are tons of warnings


Errr.. no lol. I dunno who you are coding with. But you shouldn't have any warnings. They are warnings for a reason. Very few warnings should be acceptable. Current project of mine is 20k lines, so it's small-medium and has 0 warnings. Every warning is corrected.

I would address the cause, not the symptom. You have bad code that is causing you problems. Address that before trying to find a work around.
Well, if you don't have control over other people's garbage, then you can always use a utility like grep or awk to find a specific subset of messages.
Topic archived. No new replies allowed.