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.

Comments on How can I get the same "not all control paths return a value" behaviour across Clang and MSVC?

Parent

How can I get the same "not all control paths return a value" behaviour across Clang and MSVC?

+3
−0

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

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

Static code analysis (1 comment)
Post
+3
−0

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.


  1. it might be enabled by default, but don't quote me on that. ↩︎

  2. 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. If you use CMake, it's really easy to integrate, and it has first-class support. ↩︎

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

Interesting. - We're already using `-Wswitch`, the problem is that nothing triggers for Clang whe... (4 comments)
Interesting. - We're already using `-Wswitch`, the problem is that nothing triggers for Clang whe...
Hyperlynx‭ wrote 2 months ago

Interesting.

  • We're already using -Wswitch, the problem is that nothing triggers for Clang when all of the enum's cases are accounted for in the switch.
  • std::abort can be used in place of std::unreachable as well. I've used it as part of my work-in-progress patch in some areas.
  • CI might be the way to go here. We've got a build system and I was hoping to pick up the issue before it hits the build system, but maybe CI would help...
  • Unsure if clang-tidy would help, since we'd have to get everyone to use it, which isn't as nice as a compiler flag.

I think it's likely that in our case we wouldn't be able to promote this particular warning to error. Or, perhaps, we'd just have to put up with doing it on Clang but not MSVC...

Hyperlynx‭ wrote 2 months ago

That said,

This is because both of them treat the code as if it isn't prepared for an invalid enum value, because reasons.

This isn't necessarily incorrect, imho. It's not unlikely that someone might cast an int to an enum, which does introduce the possibility of the enum having a value that's outside of the defined values. I guess, to be safest, the ideal would probably be getting Clang to behave the same as MSVC...

Zoe‭ wrote 2 months ago

We're already using -Wswitch, the problem is that nothing triggers for Clang when all of the enum's cases are accounted for in the switch

Yeah, you'd have to combine -Wswitch and -Wreturn-type for full coverage. Again, not optimal, but it is what it is right now.

Would definitely be nice if the compilers acted consistently, but that doesn't seem likely.

Also: you can run clang-tidy in the CI too, so if adoption becomes a problem, the CI would still run, and maybe give you more consistent compiler behaviour? /shrug though, there's lots of ways to go about moving the problem catching somewhere everyone can use it

Hyperlynx‭ wrote 2 months ago

The -Wswitch business is a red herring. The real issue is that - assuming you have a switch statement that does satisfy all the cases in the enum - MSVC does not consider that enough to satisfy 4715 while Clang does consider it enough to satisfy -Werror=return-type