Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

All: fix warnings when compiling with -Wswitch-enum #2927

Merged
merged 6 commits into from
Aug 13, 2021

Conversation

fhuberts
Copy link
Contributor

Signed-off-by: Ferry Huberts [email protected]

[Describe your pull request here. Please read the text below the line, and make sure you follow the checklist.]


Pull request checklist

Read the Contribution Guidelines for detailed information.

  • Changes are described in the pull request, or an existing issue is referenced.
  • The test suite compiles and runs without error.
  • Code coverage is 100%. Test cases can be added by editing the test suite.
  • The source code is amalgamated; that is, after making changes to the sources in the include/nlohmann directory, run make amalgamate to create the single-header file single_include/nlohmann/json.hpp. The whole process is described here.

Please don't

  • The C++11 support varies between different compilers and versions. Please note the list of supported compilers. Some compilers like GCC 4.7 (and earlier), Clang 3.3 (and earlier), or Microsoft Visual Studio 13.0 and earlier are known not to work due to missing or incomplete C++11 support. Please refrain from proposing changes that work around these compiler's limitations with #ifdefs or other means.
  • Specifically, I am aware of compilation problems with Microsoft Visual Studio (there even is an issue label for these kind of bugs). I understand that even in 2016, complete C++11 support isn't there yet. But please also understand that I do not want to drop features or uglify the code just to make Microsoft's sub-standard compiler happy. The past has shown that there are ways to express the functionality such that the code compiles with the most recent MSVC - unfortunately, this is not the main objective of the project.
  • Please refrain from proposing changes that would break JSON conformance. If you propose a conformant extension of JSON to be supported by the library, please motivate this extension.
  • Please do not open pull requests that address multiple issues.

@coveralls
Copy link

coveralls commented Aug 12, 2021

Coverage Status

Coverage remained the same at 100.0% when pulling cdbc50b on fhuberts:fix-switch-enum into 910fabf on nlohmann:develop.

Copy link
Owner

@nlohmann nlohmann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am surprised that fixing this warning does not come at the expense of another warning coming up - at least in the past, we always have had to suppress one of the switch warnings.

Can you please remove the -Wno-switch-enum from https://github.com/nlohmann/json/blob/develop/cmake/ci.cmake#L102 and replace -Wno-switch-enum by -Wswitch-enum in https://github.com/nlohmann/json/blob/develop/cmake/ci.cmake#L329? Then the CI is actually checking whether the warning is really gone.

@fhuberts
Copy link
Contributor Author

I am surprised that fixing this warning does not come at the expense of another warning coming up - at least in the past, we always have had to suppress one of the switch warnings.

Can you please remove the -Wno-switch-enum from https://github.com/nlohmann/json/blob/develop/cmake/ci.cmake#L102 and replace -Wno-switch-enum by -Wswitch-enum in https://github.com/nlohmann/json/blob/develop/cmake/ci.cmake#L329? Then the CI is actually checking whether the warning is really gone.

Done.
Works fine for me on Fedora 34.

g++ (GCC) 11.2.1 20210728 (Red Hat 11.2.1-1)
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

@gregmarr
Copy link
Contributor

gregmarr commented Aug 12, 2021

@nlohmann The warning that you're thinking of is -Wcovered-switch-default which is part of -Weverything on Clang. This will warn because these switches have all the cases covered AND have a default: statement.

#555 (comment)

See also #889 and #890

@fhuberts
Copy link
Contributor Author

Works fine on Debian 10.10 with gcc as well.
So it indeed seems that conflicting options are to blame here.

@fhuberts
Copy link
Contributor Author

fhuberts commented Aug 13, 2021

I checked the other issues that are mentioned (did not see them before).

If the currently feeling is the same as before, then no problem because I'll just keep this as a private patch in our repo.

Personally I do prefer the gcc way because it makes the code explicit and therefore easier to read and reason about.
My 2 cents.

Copy link
Owner

@nlohmann nlohmann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the warnings raised by Clang and GCC all complain about some switches which have not yet been touched by the PR. I think they can be fixed similarly.

@fhuberts
Copy link
Contributor Author

I'm a bit surprised that there are now errors on files I didn't touch.
Like /home/runner/work/json/json/include/nlohmann/detail/input/binary_reader.hpp.
I compiled as prescribed.

@nlohmann
Copy link
Owner

I'm a bit surprised that there are now errors on files I didn't touch.
Like /home/runner/work/json/json/include/nlohmann/detail/input/binary_reader.hpp.
I compiled as prescribed.

Did you compile the unit test suite?

@fhuberts
Copy link
Contributor Author

I'm a bit surprised that there are now errors on files I didn't touch.
Like /home/runner/work/json/json/include/nlohmann/detail/input/binary_reader.hpp.
I compiled as prescribed.

Did you compile the unit test suite?

I followed the instructions of the PR, I did as described here: https://github.com/nlohmann/json/blob/develop/README.md#execute-unit-tests

@nlohmann
Copy link
Owner

I see - but I guess the CI uses a different set of warnings.

@fhuberts
Copy link
Contributor Author

Then if you let me know how I should compile it, I'll see if I can fix those warnings as well.

@nlohmann
Copy link
Owner

Try this:

mkdir build
cd build
cmake .. -DJSON_CI=ON

Then

make ci_test_gcc

for GCC with all flags and

make ci_test_clang

for Clang with all flags.

@fhuberts
Copy link
Contributor Author

Adjusted.
make ci_test_gcc didn't work for me, the clang build did work
Might be because I have ccache installed

Signed-off-by: Ferry Huberts <[email protected]>
Copy link
Owner

@nlohmann nlohmann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

@nlohmann nlohmann self-assigned this Aug 13, 2021
@nlohmann nlohmann added this to the Release 3.9.2 milestone Aug 13, 2021
@nlohmann nlohmann self-requested a review August 13, 2021 13:26
Copy link
Owner

@nlohmann nlohmann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also add // LCOV_EXCL_LINE to the added branches, as the coverage has decreased.

image

See https://coveralls.io/builds/42131077.

Copy link
Owner

@nlohmann nlohmann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

@nlohmann
Copy link
Owner

Thanks for the patience! I will merge this as soon as the CI is done.

@nlohmann nlohmann merged commit e5ef44c into nlohmann:develop Aug 13, 2021
@fhuberts
Copy link
Contributor Author

success!
tnx!

@fhuberts fhuberts deleted the fix-switch-enum branch August 13, 2021 18:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants