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

Use const references where possible for List range iterators #50809

Merged
merged 1 commit into from
Jul 25, 2021

Conversation

akien-mga
Copy link
Member

Follow-up to #50511.

@akien-mga akien-mga added this to the 4.0 milestone Jul 24, 2021
@akien-mga akien-mga requested review from aaronfranke and reduz July 24, 2021 16:26
@akien-mga akien-mga requested review from a team as code owners July 24, 2021 16:26
@akien-mga akien-mga force-pushed the iterators-const-references branch 3 times, most recently from 9783cdb to 5051c57 Compare July 24, 2021 20:05
for (Property &F : rd.properties) {
Property &p = F;
for (const Property &p : rd.properties) {
Copy link
Member Author

Choose a reason for hiding this comment

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

Changed this as it seemed redundant.

@@ -8542,7 +8542,7 @@ void RenderingDeviceVulkan::_free_rids(T &p_owner, const char *p_type) {
} else {
WARN_PRINT(vformat("%d RIDs of type \"%s\" were leaked.", owned.size(), p_type));
}
for (RID E : owned) {
for (const RID &E : owned) {
Copy link
Member Author

Choose a reason for hiding this comment

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

Turned all for (RID foo : bar) into for (const RID &foo : bar).

I didn't double check the implications but @reduz also seemed to think it's fine.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, const reference is better here.

for (Object::Connection &F : cl) {
Object::Connection &c = F;
for (const Object::Connection &c : cl) {
Copy link
Member Author

Choose a reason for hiding this comment

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

Simplified.

@@ -1431,7 +1431,7 @@ class GDScriptParser {
void push_line(const String &p_line = String());
void push_text(const String &p_text);

void print_annotation(AnnotationNode *p_annotation);
void print_annotation(const AnnotationNode *p_annotation);
Copy link
Member Author

Choose a reason for hiding this comment

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

I made this change to allow compiling the other changes iterating with const AnnotationNode *E, and since the method doesn't seem to modify the node.

But I see now it makes it inconsistent with other print_* methods, I could change it back if needed @vnen.

Comment on lines 324 to 325
for (const lsp::DocumentSymbol *&E : list) {
if (const lsp::DocumentSymbol *s = E) {
contents.push_back(s->render().value);
}
for (const lsp::DocumentSymbol *s : list) {
contents.push_back(s->render().value);
Copy link
Member Author

Choose a reason for hiding this comment

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

I think the code should be equivalent?

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 think those are equivalent. The = operator returns the value of the newly assigned variable. So the original code is assigning and checking for nullptr at the same time. So with the new code we may be accessing an null pointer.

scene/3d/node_3d.cpp Outdated Show resolved Hide resolved
scene/3d/sprite_3d.cpp Outdated Show resolved Hide resolved
scene/main/node.cpp Outdated Show resolved Hide resolved
@akien-mga
Copy link
Member Author

Some stuff I noticed while working on this:

  • We have many occurrences of for (Ref<T> foo : bar) which should probably be converted to for (const Ref<T> &foo : bar). That will be another PR.
  • const T *foo is often trickier as we tend to use the pointer in a non-const way in most cases. That's why there's only a handful of const specifiers added to pointer iterators here.

@akien-mga akien-mga force-pushed the iterators-const-references branch from 5051c57 to 467ec69 Compare July 24, 2021 21:21
@akien-mga
Copy link
Member Author

akien-mga commented Jul 24, 2021

Updated to leave the for (T *&E) cases alone after discussion with @JFonS. There aren't many of them but if we confirm that their use is intentional, we can indeed add parentheses to clarify (they're easy to find by grepping for *& outside thirdparty code).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants