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

g++ (11) -Wuseless-cast gives lots of warnings #2893

Closed
1 of 5 tasks
fhuberts opened this issue Jul 27, 2021 · 9 comments · Fixed by #2902
Closed
1 of 5 tasks

g++ (11) -Wuseless-cast gives lots of warnings #2893

fhuberts opened this issue Jul 27, 2021 · 9 comments · Fixed by #2902
Assignees
Labels
release item: 🔨 further change solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@fhuberts
Copy link
Contributor

What is the issue you have?

When I compile the 'single include' file on g++ v11 on my Fedora 34 machine with the '-Wuseless-cast' flags enabled then I get lots of warnings.

Please describe the steps to reproduce the issue.

  1. enable the -Wuseless-cast compiler flag and compile

Can you provide a small but working code example?

What is the expected behavior?

And what is the actual behavior instead?

Which compiler and operating system are you using?

  • Compiler: gnu g++ v11
  • Operating system: Fedora 34 x64
g++ (GCC) 11.1.1 20210531 (Red Hat 11.1.1-3)
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.

Which version of the library did you use?

  • latest release version 3.9.1
  • other release - please state the version: commit a563338
  • the develop branch

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

  • yes
  • no - please copy/paste the error message below
@nlohmann
Copy link
Owner

Can you provide the warnings?

@nlohmann nlohmann added the state: needs more info the author of the issue needs to provide more details label Jul 27, 2021
@fhuberts
Copy link
Contributor Author

fhuberts commented Jul 27, 2021

So it turns out 'lots' is 2. I just saw lots because we include it in lots of places. oops.

xxx/json.hpp: In member function ‘bool nlohmann::detail::binary_reader<BasicJsonType, InputAdapterType, SAX>::parse_cbor_internal(bool, nlohmann::detail::cbor_tag_handler_t)’:
xxx/json.hpp:8856:80: warning: useless cast to type ‘std::size_t’ {aka ‘long unsigned int’} [-Wuseless-cast]
 8856 |                 return get_number(input_format_t::cbor, len) && get_cbor_array(static_cast<std::size_t>(len), tag_handler);
      |                                                                                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
xxx/json.hpp:8910:81: warning: useless cast to type ‘std::size_t’ {aka ‘long unsigned int’} [-Wuseless-cast]
 8910 |                 return get_number(input_format_t::cbor, len) && get_cbor_object(static_cast<std::size_t>(len), tag_handler);
      |                                                                                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~

@nlohmann
Copy link
Owner

These are hard to fix, because not all architectures have the same definitions for size_t.

@nlohmann
Copy link
Owner

I double checked. In both cases, there is a cast from std::uint64_t to std::size_t:

std::uint64_t len{};
return get_number(input_format_t::cbor, len) && get_cbor_object(static_cast<std::size_t>(len), tag_handler);

I case these types are the same, your reported warning makes sense. However, the types do not necessarily need to be the same.

Does anyone has an idea how to silence this warning?

@nlohmann nlohmann added state: help needed the issue needs help to proceed and removed kind: bug state: needs more info the author of the issue needs to provide more details labels Jul 29, 2021
@t-b
Copy link
Contributor

t-b commented Jul 29, 2021

I guess one could use something like the following

#include <type_traits>

template<typename T, typename U, std::enable_if_t<!std::is_same<T, U>::value, int> = 0>
T conditional_static_cast(U value)
{
return static_cast<T>(value);
}

template<typename T, typename U, std::enable_if_t<std::is_same<T, U>::value, int> = 0>
T conditional_static_cast(U value)
{
return value;
}

void func()
{
    int a = conditional_static_cast<int>(0);
    int b = static_cast<int>(0);

}

The first line in func gives no warning the second does.

@fhuberts
Copy link
Contributor Author

fhuberts commented Jul 29, 2021

I think I've dealt with this before, need to look it up.
Solution was something with ifdef's and type sizes.

However, the template solution above does seem elegant.

@nlohmann nlohmann self-assigned this Jul 30, 2021
@nlohmann nlohmann removed the state: help needed the issue needs help to proceed label Jul 30, 2021
@nlohmann
Copy link
Owner

Thanks @t-b - I was thinking of preprocessor magic before, but this is much cleaner. I'll give it a try, see #2902.

@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Jul 30, 2021
@nlohmann nlohmann added this to the Release 3.9.2 milestone Jul 31, 2021
@fhuberts
Copy link
Contributor Author

tnx!

@Pirulax
Copy link

Pirulax commented Mar 14, 2023

Wouldn't it be easier to just disable the warning instead making a workaround for it?
It seems like a pretty useless warning anyways, and disabling it is pretty straight forward.
EDIT:
Though it's used in headers too, so it might not be possible.

EDIT2: Sorry, I just noticed this isn't spdlog github, please disregard.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.

4 participants