Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

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.

Post History

71%
+3 −0
Q&A How can I get the same "not all control paths return a value" behaviour across Clang and MSVC?

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 wo...

posted 2mo ago by Zoe‭

Answer
#1: Initial revision by user avatar Zoe‭ · 2024-09-10T13:43:09Z (2 months ago)
You can't - but there are some workarounds.

Based on two answers from the VS forums ([1](https://developercommunity.visualstudio.com/t/erroneous-c4715-warning-returning-from-switch-case/1167679#TPIN-N1180038) (2020), [2](https://developercommunity.visualstudio.com/t/Visual-Studio-warning-on-Strongly-typed-/96302) (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](https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-4-c4062?view=msvc-170) (MSVC's poorly named `-Wswitch`), you get half way there.

Without those flags, if you change your code to:
```cpp
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()`<sup>[C++23]</sup>, 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[^3]. 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[^1]) regardless of which compiler you use. Setting up clang-tidy up is fairly well-documented for most build systems IIRC[^2], 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.

[^2]: If you use CMake, it's [really easy to integrate](https://danielsieger.com/blog/2021/12/21/clang-tidy-cmake.html), and it has first-class support.
[^1]: 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.
[^3]: it might be enabled by default, but don't quote me on that.