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

usless_cast warnings with gcc 9.3 and 10.1 (C++17) #2114

Closed
madeso opened this issue May 16, 2020 · 4 comments · Fixed by #2902
Closed

usless_cast warnings with gcc 9.3 and 10.1 (C++17) #2114

madeso opened this issue May 16, 2020 · 4 comments · Fixed by #2902
Assignees
Labels
kind: bug release item: 🔨 further change solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@madeso
Copy link

madeso commented May 16, 2020

What is the issue you have?

gcc (g++) gives 2 usless_cast warnings when including json.hpp

Please describe the steps to reproduce the issue. Can you provide a small but working code example?

#include "json.hpp"

int main()
{
    return 0;
}

g++ -Wuseless-cast -std=c++17 main.cc

What is the expected behavior?

No warnings.

And what is the actual behavior instead?

2 compiler warnings:

In file included from main.cc:1:
json.hpp: In member function ‘bool nlohmann::detail::binary_reader<BasicJsonType, SAX>::parse_cbor_internal(bool)’:
json.hpp:5691:81: warning: useless cast to type ‘std::size_t’ {aka ‘long unsigned int’} [-Wuseless-cast]
 5691 |                 return get_number(input_format_t::cbor, len) and get_cbor_array(static_cast<std::size_t>(len));
      |                                                                                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
json.hpp:5745:82: warning: useless cast to type ‘std::size_t’ {aka ‘long unsigned int’} [-Wuseless-cast]
 5745 |                 return get_number(input_format_t::cbor, len) and get_cbor_object(static_cast<std::size_t>(len));
      |                                                                                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Which compiler and operating system are you using? Is it a supported compiler?

supported: g++ 9.3.0 (Ubuntu 9.3.0-11ubuntu0~18.04.1)
not supported but might be working: g++ 10.1.0 (arch linux)

Did you use a released version of the library or the version from the develop branch?

For g++ 9.3.0, only latest release v3.7.3 was tested
For g++ 10.1.0 both 3.7.3 and develop branch can reproduce it

If you experience a compilation error: can you compile and run the unit tests?

Not a compilation error, but a warning

@nlohmann
Copy link
Owner

Hm, this is very pedantic. In both cases, we cast a std::uint64_t to std::size_t. These types may be the same, but they do not need to be. So I would assume this warning is a false positive.

@nlohmann nlohmann added the state: help needed the issue needs help to proceed label May 16, 2020
@madeso
Copy link
Author

madeso commented May 17, 2020

Hrm... you might be right about the false-positive.

I have a similar (open) issue in spdlog and it seems that that useless-cast can be removed by storing the offending code in a auto variable and then static_casting, making me considering removing the useless-cast warning from my compiler settings altogether.

@nlohmann
Copy link
Owner

In this specific case, the types are important as they are defined by CBOR. So I think there is nothing I can do.

@nlohmann nlohmann added solution: wontfix the issue will not be fixed (either it is impossible or deemed out of scope) and removed state: help needed the issue needs help to proceed labels May 17, 2020
@nlohmann nlohmann added release item: 🔨 further change solution: proposed fix a fix for the issue has been proposed and waits for confirmation and removed solution: wontfix the issue will not be fixed (either it is impossible or deemed out of scope) labels Jul 31, 2021
@nlohmann nlohmann self-assigned this Jul 31, 2021
@nlohmann nlohmann added this to the Release 3.9.2 milestone Jul 31, 2021
@nlohmann
Copy link
Owner

Fixed with #2902.

@nlohmann nlohmann linked a pull request Jul 31, 2021 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: bug release item: 🔨 further change solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants