-
-
Notifications
You must be signed in to change notification settings - Fork 21.9k
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
Add const references detected by clang-tidy #84445
Add const references detected by clang-tidy #84445
Conversation
@@ -586,7 +586,7 @@ void EditorExportPlatformMacOS::_make_icon(const Ref<EditorExportPreset> &p_pres | |||
}; | |||
|
|||
for (uint64_t i = 0; i < (sizeof(icon_infos) / sizeof(icon_infos[0])); ++i) { | |||
Ref<Image> copy = p_icon; // does this make sense? doesn't this just increase the reference count instead of making a copy? Do we even need a copy? | |||
const Ref<Image> © = p_icon; // does this make sense? doesn't this just increase the reference count instead of making a copy? Do we even need a copy? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment is referring to a copy, with some confusion on what happens with reference copying.
I'd at least remove the comment but I'd investigate whether that's relevant anymore to the code it's in.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just checked and the comment is still relevant. There's a single EditorExportPlatformMacOS::_make_icon
call from which p_icon
comes from and there's a specific case where referencing p_icon
is not needed. Properly patching this merits its own pull request. I'll undo this change from this PR.
7fa5cb9
to
4718e78
Compare
Be careful, because Godot uses its own structures. This is not the STL. For example this code: String path = fields[0]; Does not actually copy. The String class is implemented using CoW (Copy on Write) which means a copy is almost free unless "path" is modified. If path is modified only then an actual copy occurs. Changing it to: const String &path = fields[0]; Just adds an extra indirection for no reason. Something more controversial happens with: Ref<Mesh> mesh = p_meshes[i];
// vs
const Ref<Mesh> &mesh = p_meshes[i]; The void null_some_data()
{
some_data = nullptr; // Decrease reference count. It may reach 0.
}
Ref<Mesh> mesh = some_data[i];
null_some_data();
mesh->do_something(); // valid
// vs
const Ref<Mesh> &mesh = some_data[i];
null_some_data();
mesh->do_something(); // ERROR! This is a nullptr. |
The data here are largely (with some exceptions) COW data types ( However for performance critical paths there is a small, but potentially important, difference: Additionally: The primary benefit of replacing with const references is that they can be optimized by the compiler, assuming other optimization assumptions hold |
In the past we've opted not to mainly due to false positives and these usually happen when considering header guards, however there's other reasons as well. One false positive I caught in this particular PR was that the tool suggested the following change: index 2e0ca7b536..900f9b2714 100644
--- a/main/main.cpp
+++ b/main/main.cpp
@@ -1758,7 +1758,7 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph
#endif
String default_driver = driver_hints.get_slice(",", 0);
- String default_driver_macos = default_driver;
+ const String &default_driver_macos = default_driver;
#if defined(GLES3_ENABLED) && defined(EGL_STATIC) && defined(MACOS_ENABLED)
default_driver_macos = "opengl3_angle"; // Default to ANGLE if it's built-in.
#endif You can get the above suggestion by scanning with:
At first glance it might seem fine, but upon closer inspection Another reason for not always checking the source with static analyzers is because we stumble upon Path Explosion. The case above is a small example of how I think that covers most of our reasons. If we were to zoom out even more on this problem, we may end up talking about abstract topics such as the Halting Problem, Godel's Incompleteness, and other fundamental problems considered under specific contexts, at which point the answer to whether we should implement the CI check gets blurred by unnecessary details. |
Very valid point, thanks! |
Second this, and adding that these kinds of static checks can easily confuse and mess with new contributors etc. |
c386d29
to
9154139
Compare
Will take a look, but as suggested above many of these can be converted into simple uses, will recommend some tomorrow |
I should add there are many that I think add readability and perhaps should be kept, but I don't have a strong opinion about it. I don't mind simplifying further. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got around to it now, leaving it to your discression but most of these are direct improvements and several improve readability by not requiring you to check where the data is from (and are readable in what they represent)
832613f
to
3933c29
Compare
3933c29
to
d05f741
Compare
@AThousandShips the patches for most of your suggestions were included in #85071. And thanks for the thorough review -- thanks to that I realized we could use a couple more |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These look good to me
d05f741
to
a3cb1b0
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks fine to me.
Thanks! |
I stumbled upon a couple of lines where data is unnecessarily copied data during variable initialization (for which I opened #84379) and decided to scan the codebase with
clang-tidy
to see what it could find.If this pull request conflicts with another one, priority should be given to the other pull request since this particular patch can be automatically generated with:
And selectively choosing what to commit