Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
How can I get the same "not all control paths return a value" behaviour across Clang and MSVC?
I've recently discovered that it's not actually an error to have control reach the end of a non-void function without returning anything, it's merely undefined behaviour. I want to promote the relevant warning(s) to error.
Our product that's built on multiple platforms, using Clang on one and Microsoft Visual Studio on the other. So far, I've tried using -Werror=return-type
for Clang and /we4183 /we4715
for MSVC. However, there's a problem.
The following code produces no warnings at all in Clang with -Wall -W
, but for MSVC triggers 4715, "not all control paths return a value":
enum class Letter
{
A,
B,
C,
END
};
int toNum(Letter letter)
{
switch (letter)
{
case Letter::A:
return 0;
case Letter::B:
return 1;
case Letter::C:
return 2;
case Letter::END:
return -1;
}
}
It seems Clang determines that all the enum cases are handled and that control will never reach the end of the function, while MSVC does not.
Is there any way around this? It would be really, really handy to have an error triggered when someone does forget a return value, but we also can't have builds fail when our mostly Clang developers put in code that MSVC will object to.
The next-best solution would be to promote the warnings to error for Clang only, but ideally I'd like to catch any problems like this when working on Windows too...
2 answers
You can't - but there are some workarounds.
Based on two answers from the VS forums (1 (2020), 2 (2023)), this behaviour is by design, and it doesn't appear to be configurable.
Aside MSVC, GCC works the same way:
❯ g++ test6.cpp -Werror=return-type
test6.cpp: In function ‘int toNum(Letter)’:
test6.cpp:23:1: error: control reaches end of non-void function [-Werror=return-type]
23 | }
| ^
cc1plus: some warnings being treated as errors
This is because both of them treat the code as if it isn't prepared for an invalid enum value, because reasons.
If you add an enum value but don't add it to the switch, Clang will naturally show the same error:
❯ clang++ test6.cpp -Werror=return-type
test6.cpp:12:13: warning: enumeration value 'ABCD' not handled in switch [-Wswitch]
12 | switch (letter)
| ^~~~~~
test6.cpp:24:1: error: non-void function does not return a value in all control paths [-Werror,-Wreturn-type]
24 | }
| ^
1 warning and 1 error generated.
However, take a close look at the warning. Unlike MSVC and GCC, Clang enables -Wswitch
by default, which requires a switch to be exhaustive (or have a default
case to cover anything not included). GCC enables -Wswitch
with -Wall
, or you can pass the specific flag manually if you prefer.
While not a solution per se, you have an alternative option. If you set -Wswitch
(GCC + Clang) and enable C4062 (MSVC's poorly named -Wswitch
), you get half way there.
Without those flags, if you change your code to:
int toNum(Letter letter)
{
switch (letter)
{
case Letter::A:
return 0;
case Letter::B:
return 1;
case Letter::C:
return 2;
case Letter::END:
return -1;
}
// Pre-C++23; there may be better options using attributes or other misc. dark magic, but this is left as an exercise to the reader
throw std::runtime_error("Unreachable");
// C++23
std::unreachable();
}
All the compilers are happy. This just complies with the compilers' needs to have something terminating at the end of the function, presumably just in case there's an invalid enum value.
But if you now add an enum value, only Clang is going to complain. In fact, it shows the same output as the previous clang run, minus the return-type error; clang only picks it up because of -Wswitch
If you now compile with -Werror=switch
//we4062
, every single compiler is going to complain about the missing enum case.
TL;DR: If you compile with an error for non-exhaustive switch statements rather than just -Werror=return-type
, and add either std::unreachable()
[C++23], a throw, or some magic C++ attribute function after your exhaustive switch, you'll get the behaviour you want.
Side-notes
CI
The next-best solution would be to promote the warnings to error for Clang only, but ideally I'd like to catch any problems like this when working on Windows too...
Depending on how you're developing, you could also consider setting up CI. If your project is open-source, GitHub Actions is free. GitLab CI is more restricted (400 compute minutes per month in the free tier), so not as easy there.
It won't help you locally, but it at least means it's caught somewhere in your workflow without needing you to access a Linux machine.
clang-tidy/linting
If you use clang-tidy
, you get a lot of Clang's warnings without depending on specifically Clang being used. It includes the same diagnostics as clang proper, at least if you enable the clang-*
diagnostics[1]. For -Wswitch
, you can enable clang-diagnostic-switch
, and set it under WarningsAsErrors
as well. Clang-tidy's config is verbose is verbose, but this is functionally equivalent to -Werror=switch
, and works regardless of the compiler.
This means you get Clang's diagnostics (plus a bunch of extra diagnostics, depending on what you enable[2]) regardless of which compiler you use. Setting up clang-tidy up is fairly well-documented for most build systems IIRC[3], so I won't cover that here. Anyway, this means you wouldn't need to set (these specific) error flags in MSVC, but rather defer those diagnostics to clang-tidy.
-
it might be enabled by default, but don't quote me on that. ↩︎
-
I do strongly recommend enabling more diagnostics if you're already setting up clang-tidy. It does add some overhead to the compilation process, so you might as well squeeze more value out of it. ↩︎
-
If you use CMake, it's really easy to integrate, and it has first-class support. ↩︎
1 comment thread
The reason why it is undefined behaviour instead of a hard error is that the problem of determining it is, quite literally, equivalent to the halting problem. So any implementation will either warn about cases where a return cannot actually happen,or miss cases where it can happen, or both. Different implementations will probably make different tradeoffs when giving that warning, and will differ in the effort they put into deciding whether to warn or not. I wouldn't even rely on different versions of the same compiler giving consistent results.
1 comment thread