Please see the code example below
- Does anyone know why visual studio 22 behaves this way
- Does anyone know if there is a setting or something i can change to my debug configuration so that it does not generate the ‘not all paths return a value’ warning
Note: I have ‘treat warnings as errors’ – this cannot change
// In debug this compiles
// In release this has a compiler warning about unreachable code
int aaa()
{
int value = 0;
try
{
value = myFunctionThatCouldThrow();
}
catch (const std::exception& exc)
{
ThisFunctionALWAYSThrows(exc);
}
return value;
}
// In debug this has a compiler warning that not all paths return a value
// In release this compiles
int aaa()
{
try
{
return myFunctionThatCouldThrow();
}
catch (const std::exception& exc)
{
ThisFunctionALWAYSThrows(exc);
}
}
// This compiles in both but requires the preprocessor debug flag
int aaa()
{
try
{
return myFunctionThatCouldThrow();
}
catch (const std::exception& exc)
{
ThisFunctionALWAYSThrows(exc);
}
#ifdef _DEBUG
// The return statement is a pointless line as it can never be hit - I agree with the release build warning
// yet without this return statement - code building in debug generates a warning...
// I know I could also pragma ignore the warning - Im hoping for a cleaner solution without macros
// There are multiple functions in the code base that behave this way (the codes been ported from VS17)
// A non macro solution is better as I dont need to amend all functions
// I also dont want a more global ignore of the warning as that could hide genuine issues
return -1;
#endif
}
2
Answers
First up apologies - my initial code was not going to reproduce the issue - it was copied from code i couldnt share as an example. Its amended now. The questions posted did assist me in spotting this oversight and working out the error. Thankyou.
Onto the answer - its just due to the optimiser. The function that always throws is optimised to be an inline throw meaning release wont complain about all paths not returning a value. Whereas debug it wont optimise and it will complain.
First thing I need to mention that I couldn’t reproduce your warnings in Compiler Explorer with any available MSVC compilers. Try providing reproducible example.
Assuming that the warning really exists, you can disable it with
#pragma warning(disable: 4702)
, where 4702 is the unreachable code warning.Notice, that when using warnings with numbers 4700-4999 a special rule exists. Quoting from Microsoft documentation: (https://learn.microsoft.com/en-us/cpp/preprocessor/warning?view=msvc-170)
In your case it would look like this: