Skip to content

Fix a clang warning [-Wshadow-field-in-constructor-modified] #2780

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

Merged
merged 2 commits into from
Jan 14, 2021

Conversation

asmaloney
Copy link
Contributor

@asmaloney asmaloney commented Jan 9, 2021

Description

Fix warning from Apple clang version 11.0.0 (clang-1100.0.33.17)

Suggested changelog entry:

* fix warning modifying constructor parameter 'flag' that shadows a field of 'set_flag' [-Wshadow-field-in-constructor-modified]

warning: modifying constructor parameter 'flag' that shadows a field of 'set_flag' [-Wshadow-field-in-constructor-modified]
@@ -1910,7 +1910,7 @@ template <return_value_policy Policy = return_value_policy::reference_internal,
template <typename InputType, typename OutputType> void implicitly_convertible() {
struct set_flag {
bool &flag;
set_flag(bool &flag) : flag(flag) { flag = true; }
set_flag(bool &my_flag) : flag(my_flag) { my_flag = true; }
Copy link
Collaborator

@YannickJadoul YannickJadoul Jan 9, 2021

Choose a reason for hiding this comment

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

my_flag sounds very tutorial-for-programming-like, though. Could you change this to flag_ or f or so (or suggest something else)?

Also (mostly to other reviewers): should we turn on more warning flags in CI? What's our goal when it comes to such warnings?

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 figured you would have a preferred naming for something like this, I just couldn't find it. I use m for member style (mFlag) so I never have to deal with this in my own code.

I'll use flag_.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks! I can't really find it either. We do use m_flag for private class members, but this is a struct, so flag is part of the interface (well, it's a local struct in a function, so it's not that important, but you don't want to write some_struct.m_flag, I think :-) )

Copy link
Collaborator

Choose a reason for hiding this comment

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

A quick look seems that f and flag_ would be the two options. I found instances of both conventions. I think this is fine, though, so thanks!

@YannickJadoul
Copy link
Collaborator

Can't hurt to silence this, I suppose.

@YannickJadoul
Copy link
Collaborator

I'm surprised this is the only one you find, though? I can spot a bunch of other struct members and constructor arguments in attr.h. But those don't fire that warning for you?

@YannickJadoul YannickJadoul added this to the v2.6.2 milestone Jan 9, 2021
@asmaloney
Copy link
Contributor Author

I'm surprised this is the only one you find, though?

Hmmm... you're right - that is surprising.

I'm using a tiny subset of the API, but I see that struct doc and struct name have the same issue.

@henryiii
Copy link
Collaborator

henryiii commented Jan 9, 2021

This is a pretty specific warning, due to the fact you modify a value passed in. This is really weird out of context, it's setting the value and then changing the input to True?

bool val = False;
set_flag new_flag(val);
// val is now True, new_flag contains False.

Big fan of the change making this clearer!

@YannickJadoul
Copy link
Collaborator

Oh, I think I see what's going on. Isn't this guarding against re-entrance/infinite recursion of the implicit conversion, during the PyObject *result = PyObject_Call((PyObject *) type, args.ptr(), nullptr); call?

@YannickJadoul
Copy link
Collaborator

This is a pretty specific warning, due to the fact you modify a value passed in. This is really weird out of context, it's setting the value and then changing the input to True?

Right, I see, indeed. This could be an issue because it's not clear which flag = true; (should be the parameter you're setting?). In this case, it doesn't really matter though, since both are references, so it doesn't matter which one is changed. At any rate, it's not wrong either, to fix it, so let's just merge it and remove a warning :-)

@henryiii henryiii merged commit df8494d into pybind:master Jan 14, 2021
@github-actions github-actions bot added the needs changelog Possibly needs a changelog entry label Jan 14, 2021
@asmaloney asmaloney deleted the fix-clang-warning branch January 14, 2021 04:17
@henryiii henryiii removed the needs changelog Possibly needs a changelog entry label Jan 25, 2021
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