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

Cleanup additional warnings #407

Merged

Conversation

rianquinn
Copy link
Contributor

@rianquinn rianquinn commented Oct 27, 2016

When turning on the following flags, several additional warnings
were generated, which have been cleaned up in this patch. The
flags included:

-Wextra
-Wpedantic
-Wconversion
-Wsign-conversion
-Wctor-dtor-privacy
-Wshadow
-Wnon-virtual-dtor
-Wold-style-cast
-Wcast-align
-Woverloaded-virtual

Signed-off-by: “Rian <“rianquinn@gmail.com”>

@@ -630,7 +630,7 @@ template <class ElementType, std::ptrdiff_t Extent>
constexpr ElementType& at(const span<ElementType, Extent>& s, size_t index)
{
// No bounds checking here because it is done in span::operator[] called below
return s[index];
return s[narrow_cast<typename span<ElementType, Extent>::index_type>(index)];
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the only one that I was not sure about what to do as we could use gsl::narrow or gsl::narrow_cast. I used gsl::narrow_cast as I know a lot of people have brought up issues with performance so I figured that would be the likely choice.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think you're fine to use gsl::narrow_cast() here because you know that s will validate the bounds anyway. So I like it as you have it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since the other PR changes the index type to std::ptrdiff_t, there is no need for this cast anymore. Once the other PR is merged, I will clean this one up so that only the fixes that are needed there.


for (int i = 0; i < 4; ++i)
for (size_t i = 0; i < 4; ++i)
CHECK(at(a, i) == i+1);
Copy link
Member

Choose a reason for hiding this comment

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

Why size_t?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

at takes a size_t so attempting to put an int in it will result in a signed conversion warning.

Copy link
Member

Choose a reason for hiding this comment

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

Hmm, I missed that. Why is at() taking a different type than []?

Copy link
Contributor Author

@rianquinn rianquinn Oct 27, 2016

Choose a reason for hiding this comment

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

You tell me 😄

That is the one that's been there for a while (this is testing gsl::at and not the new gsl::span::at).

Copy link
Member

Choose a reason for hiding this comment

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

Yes; it was question to "us". It is interesting the compiler isn't warning about i < 4, presumably because 4 is small enough?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll test with Clang, but I'm almost positive it will complain about the same thing as I have had to make a lot of changes like this to support Clang Tidy with these warnings enabled

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, clang has the same warning (and some others I might add, but we can address those in a different PR)

/home/user/gsl/tests/at_tests.cpp:62:25: warning: implicit conversion changes signedness: 'int' to 'size_t' (aka 'unsigned long')
      [-Wsign-conversion]
            CHECK(at(a, i) == i+1);

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm with @gdr-at-ms ... why does at() take size_t? It should not. It should take ptrdiff_t. If that's my fault, it's probably because we never updated it after the original prototype back in the mists of the past. But that should change, rather than be worked around.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@neilmacintosh ok I can update the PR to change this.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks @rianquinn and thanks for #408 which corrects my laziness!

@rianquinn
Copy link
Contributor Author

I'd like to work on the following PR first, before we continue with this one.
#408

Once we address the issue with gsl::at, I can take a stab at this again.

@neilmacintosh neilmacintosh self-assigned this Oct 28, 2016
When turning on the following flags, several additional warnings
were generated, which have been cleaned up in this patch. The
flags included:

-Wextra
-Wpedantic
-Wconversion
-Wsign-conversion
-Wctor-dtor-privacy
-Wshadow
-Wnon-virtual-dtor
-Wold-style-cast
-Wcast-align
-Woverloaded-virtual

Signed-off-by: “Rian <“rianquinn@gmail.com”>
@rianquinn
Copy link
Contributor Author

@neilmacintosh I updated this PR now that the size_t issue has been addressed with at(). I also enabled the rest of the C++ warnings that GCC provides, so I think that is all of them. With this PR, people should be able to enable -Werror and any of the GCC warnings.

@neilmacintosh
Copy link
Collaborator

@rianquinn thanks very much for doing this useful cleanup! It looks great to me.

@neilmacintosh neilmacintosh merged commit d641796 into microsoft:master Nov 4, 2016
@martinmoene martinmoene mentioned this pull request Nov 10, 2016
10 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants