I have the following function (reduced example):
QByteArray DecompressBytes(const QByteArray& content){
/* function body (with other return expressions) */
do { return content; } while(content.size() != 0);
}
Last line was added for testing, replacing a macro used. Visual Studio doesn't see a problem with this code, but g++ generates
warning: control reaches end of non-void function [-Wreturn-type]
Changing last line to return content;
removes the warning.
My question: why the compiler behaves this way and what should be the form of the code to avoid any warnings?
The last line used to be ASSERT_FAIL("must be unreachable", content)
with ASSERT_FAIL
expanding into do { ... } while(false)
pattern with another macro replacing while
to forbid non-bool expressions, so the resulting expression was like do { qt_assert_x("", "", 42); return content; } while(::helper::bool_verify(false));
.
g++ version 5.3.0, used with MinGW (standard Qt setup).
Upd: after trying to comment different parts of code (since pure example above doesn't allow to reproduce the problem), something really strange emerged:
QByteArray DecompressBytes(const QByteArray& content){
QByteArray decompressed; //no 'unused variable' warning
do { return content; } while(content.size() != 0);
} //produces warning above
QByteArray DecompressBytes2(const QByteArray& content){
//QByteArray decompressed;
do { return content; } while(content.size() != 0);
} //doesn't produce warning
std::vector<char> DecompressBytes3(const std::vector<char>& content){
std::vector<char> decompressed; //no 'unused variable' warning
do { return content; } while(content.size() != 0);
} //does produce warning
std::vector<char> DecompressBytes4(const std::vector<char>& content){
int decompressed; //unused variable warning is given
do { return content; } while(content.size() != 0);
} //doesn't produce warning
Not sure what all of this means.