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

Reduce amount of warnings with pedantic compiler #159

Closed
wants to merge 2 commits into from

Conversation

vnepogodin
Copy link

-Wshadow
-Wconversion
-Wsign-conversion

@@ -457,7 +457,7 @@ class Argument {
return start;
} else if (mNumArgs <= std::distance(start, end)) {
if (auto expected = maybe_nargs()) {
end = std::next(start, *expected);
end = std::next(start, static_cast<long>(*expected));
Copy link
Contributor

Choose a reason for hiding this comment

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

*expected is a std::size_t and is clearly defined as such. I'm surprised the compiler is warning about this.

Copy link
Author

@vnepogodin vnepogodin Feb 20, 2022

Choose a reason for hiding this comment

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

https://en.cppreference.com/w/cpp/iterator/next
because second argument for std::next is typename std::iterator_traits<InputIt>::difference_type. where InputIt difference_type is long int. In that case InputIt is it variable on this line:
https://github.com/p-ranav/argparse/blob/master/include/argparse/argparse.hpp#L1074

Copy link
Contributor

@skrobinson skrobinson Feb 23, 2022

Choose a reason for hiding this comment

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

I see the need, now. But, MSVC++ has 32-bit long int. The safer cast appears to be to decltype(start)::difference_type.

@@ -620,7 +620,7 @@ class Argument {
* sign: one of
* '+' '-'
*/
static bool is_decimal_literal(std::string_view s) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Do all these variable names need to change? Would renaming just the consume_digits parameter clear shadowing issues?

Copy link
Author

Choose a reason for hiding this comment

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

No, but it makes code more readable. Of cause if you don't like my change on variables name, then I will revert my changes on them

Copy link
Contributor

Choose a reason for hiding this comment

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

I wanted to be clear whether these naming changes affect the reported issue with pedantic warnings. I don't see a problem with the new names.

@@ -1018,7 +1018,7 @@ class ArgumentParser {
stream << "Positional arguments:\n";

for (const auto &mPositionalArgument : parser.mPositionalArguments) {
stream.width(tLongestArgumentLength);
stream.width(static_cast<std::streamsize>(tLongestArgumentLength));
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe we can change the output type for ::get_length_of_longest_argument (which is private) to std::streamsize. Then these casts are no longer needed?

Copy link
Author

Choose a reason for hiding this comment

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

Yes, it will be no longer needed. I think would be better to just change variable tLongestArgumentLength on line 1007 to std::streamsize

Copy link
Author

@vnepogodin vnepogodin Feb 20, 2022

Choose a reason for hiding this comment

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

e.g:

const auto& tLongestArgumentLength = static_cast<std::streamsize>(parser.get_length_of_longest_argument());

Copy link
Contributor

Choose a reason for hiding this comment

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

Why do you think it is better to static_cast instead of using the expected final type from the beginning? I'm not understanding the advantage you see.

return s.substr(it - std::begin(s));
auto consume_digits = [=](std::string_view needle) {
auto it = std::find_if_not(std::begin(needle), std::end(needle), is_digit);
return needle.substr(static_cast<std::size_t>(it - std::begin(needle)));
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't see a way around this cast, but is this the right type? For containers, I thought we should use size_type, e.g. static_cast<decltype(needle)::size_type>(it - std::begin(needle)).

Yes, these all should resolve to the same type (probably std::size_t), but this may save us if the type for needle changes in the future.

Copy link
Author

Choose a reason for hiding this comment

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

Yes for sure, it will save in future

p-ranav added a commit that referenced this pull request Sep 21, 2022
Added -Wshadow and -Wconversion to CXX_FLAGS and fixed warnings (related to #159)
@p-ranav
Copy link
Owner

p-ranav commented Sep 21, 2022

Fixed by #202 and #204. Closing this PR.

@p-ranav p-ranav closed this Sep 21, 2022
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.

3 participants