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

Fix implicit signedness conversion warning #1963

Merged
merged 1 commit into from
Oct 29, 2020

Conversation

jgopel
Copy link
Contributor

@jgopel jgopel commented Oct 28, 2020

Problem:

  • On Apple clang 11.0.3 (clang-1103.0.32.62), pedantic mode compilation
    generates the following error:

    test/std-format-test.cc:114:22: error: implicit conversion changes
    signedness: 'int' to 'size_t' (aka 'unsigned long')
    [-Werror,-Wsign-conversion]
    width_arg_id = c - '0';
    ~ ~~^~~~~

Solution:

  • Use a to_unsigned to make the conversion explicit. This is
    guaranteed to be safe due to the check before the ASCII-to-int
    conversion.

I agree that my contributions are licensed under the {fmt} license, and agree to future changes to the licensing.

@vitaut
Copy link
Contributor

vitaut commented Oct 28, 2020

Thanks for the PR. Please post the warning and repro details including compiler version. Also it looks like the description was copied from a wrong PR.

@jgopel jgopel force-pushed the fix-sign-conversion-warning branch from 364475e to 1ce1507 Compare October 28, 2020 14:18
@jgopel
Copy link
Contributor Author

jgopel commented Oct 28, 2020

Updated in the commit notes and here in the original comment.

@@ -111,7 +111,7 @@ template <> struct std::formatter<S> {
char c = get_char();
if (!isdigit(c) || (++iter, get_char()) != '}')
throw format_error("invalid format");
width_arg_id = c - '0';
width_arg_id = static_cast<decltype(width_arg_id)>(c - '0');
Copy link
Contributor

Choose a reason for hiding this comment

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

Please use fmt::detail::to_unsigned instead of a cast.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

Problem:
- On Apple clang 11.0.3 (clang-1103.0.32.62), pedantic mode compilation
  generates the following error:

    test/std-format-test.cc:114:22: error: implicit conversion changes
          signedness: 'int' to 'size_t' (aka 'unsigned long')
          [-Werror,-Wsign-conversion]
        width_arg_id = c - '0';
                     ~ ~~^~~~~

Solution:
- Use a `to_unsigned` to make the conversion explicit. This is
  guaranteed to be safe due to the check before the ASCII-to-int
  conversion.
@jgopel jgopel force-pushed the fix-sign-conversion-warning branch from 1ce1507 to d3d84cd Compare October 28, 2020 23:14
@vitaut vitaut changed the title 🐛 [std-format-test] Implicit signedness conversion Fix implicit signedness conversion warning Oct 29, 2020
@vitaut vitaut merged commit b3a4f28 into fmtlib:master Oct 29, 2020
@vitaut
Copy link
Contributor

vitaut commented Oct 29, 2020

Thanks

@jgopel jgopel deleted the fix-sign-conversion-warning branch November 7, 2020 19:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants