martinmoene / expected-lite

expected lite - Expected objects in C++11 and later in a single-file header-only library
Boost Software License 1.0
375 stars 34 forks source link

C4702: unreachable code #65

Closed hanusek closed 3 months ago

hanusek commented 7 months ago

Hello! I have a problem:

C:\builds\eprotect-platfrom\auth\client\include\nonstd\expected.hpp(2142) : error C2220: the following warning is treated as an error
C:\builds\eprotect-platfrom\auth\client\include\nonstd\expected.hpp(2142) : warning C4702: unreachable code

OS: Windows 2022 Visual Studio 2022 v17.5.0

https://conan.io/center/recipes/expected-lite?version=0.6.3

subbota-a commented 6 months ago

Me too

szaszm commented 5 months ago

How does that work? That line is a comment, there is no code there: https://github.com/martinmoene/expected-lite/blob/v0.6.3/include/nonstd/expected.hpp#L2142 Is it any different in your build directory?

np-c0mp1ete commented 4 months ago

Hello!

Repro:

#include "expected.hpp"

int main()
{
    nonstd::expected<int, int> ei = 0;
    return ei.value();
}

Compile using cl /O2 /W4 /WX main.cpp. expected-lite version is 0.6.3.

Output:

Microsoft (R) C/C++ Optimizing Compiler Version 19.39.33523 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

main.cpp
C:\test\expected.hpp(1803) : error C2220: the following warning is treated as an error
C:\test\expected.hpp(1803) : warning C4702: unreachable code
martinmoene commented 4 months ago

On Compiler Explorer:

I'm not sure what to think of MSVC's warning.

szaszm commented 4 months ago

Ah, at first I thought line 2142 is with the 0.6.3 release branch, but apparently it's on master, so there is actual code there. So the correct reference is

Maybe MSVC doesn't like the value() call next to the comma operator after rethrow, which is actually unreachable. Maybe a random throw expression instead of value() call would silence it, but I can't test this now.

A few other wrong ideas I had:

martinmoene commented 4 months ago

Thanks for your helpful thoughts to advance this issue.