You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
token_type_name(token_type t): not all control paths return a value #156
The function "token_type_name(token_type t)" contains a switch where every case contains a return, but Visual Studio 2015 complains about the function still having a control path that wouldn't return a value. Adding a default to the switch would fix it.
I saw this issue, but the error message makes no sense. Adding a default branch just to shut up the compiler is something I would call bad practice as it would just introduce unreachable code.
In the version that we're using, token_type_name has a default case, so if it doesn't have one now, it was recently removed
default:
return "<parse error>";
I see, it used to be default:, it was just changed to case token_type::parse_error:
The compiler warning about there being a control path that doesn't return is a good thing. It's only unreachable if you've already guaranteed that t doesn't contain a value outside of the specified enum values. It looks like you've done that through making sure that t can never contain something other than a valid enum value. Unfortunately, the compiler doesn't know that, and so can't tell that control doesn't reach the end of the function. There's been discussion on the C++ standards proposal group recently about how to tell the compiler that you know for sure that all paths through the switch return from the function, but so far there's nothing portable.
@gregmarr, sorry for the confusion - I overworked the switch statements lately as I thought I could get rid of all warnings. However, there seems to always be one compiler flag that yields a warning - even though the code is fine.
In the concrete setting, I did not want to replace the last case by default, because it would, from my point of view, make the code less readable.
I'm fine with that, we may just need to add another warning to the list that we already suppress around the header: 4706 (assignment within conditional) and 4996 (function was declared deprecated). We have our own header that includes json.hpp, which is where we have our fix for the android strtold issue, so that's not a big issue.
Activity
nlohmann commentedon Dec 15, 2015
I saw this issue, but the error message makes no sense. Adding a default branch just to shut up the compiler is something I would call bad practice as it would just introduce unreachable code.
Drealmer commentedon Dec 15, 2015
Alright, thanks for your answer and sorry for bothering you.
gregmarr commentedon Dec 15, 2015
In the version that we're using, token_type_name has a default case, so if it doesn't have one now, it was recently removed
default:
return "<parse error>";
I see, it used to be default:, it was just changed to case token_type::parse_error:
The compiler warning about there being a control path that doesn't return is a good thing. It's only unreachable if you've already guaranteed that t doesn't contain a value outside of the specified enum values. It looks like you've done that through making sure that t can never contain something other than a valid enum value. Unfortunately, the compiler doesn't know that, and so can't tell that control doesn't reach the end of the function. There's been discussion on the C++ standards proposal group recently about how to tell the compiler that you know for sure that all paths through the switch return from the function, but so far there's nothing portable.
nlohmann commentedon Dec 15, 2015
@gregmarr, sorry for the confusion - I overworked the switch statements lately as I thought I could get rid of all warnings. However, there seems to always be one compiler flag that yields a warning - even though the code is fine.
In the concrete setting, I did not want to replace the last case by default, because it would, from my point of view, make the code less readable.
gregmarr commentedon Dec 15, 2015
I'm fine with that, we may just need to add another warning to the list that we already suppress around the header: 4706 (assignment within conditional) and 4996 (function was declared deprecated). We have our own header that includes json.hpp, which is where we have our fix for the android strtold issue, so that's not a big issue.
nlohmann commentedon Dec 17, 2015
Meanwhile, I saw that GCC 4.9 also warns about this.