From 194bdde94787227e8f53a4e3273c192ab70b03ac Mon Sep 17 00:00:00 2001 From: A Thousand Ships <96648715+AThousandShips@users.noreply.github.com> Date: Fri, 26 Jul 2024 11:52:26 +0200 Subject: [PATCH] Cleanup of raw `nullptr` checks with `Ref` Using `is_valid/null` over checks with `nullptr` or `ERR_FAIL_NULL` etc. --- core/extension/gdextension.cpp | 2 +- core/io/file_access_encrypted.cpp | 4 +- core/io/image.cpp | 2 +- core/io/plist.cpp | 4 +- editor/animation_bezier_editor.cpp | 2 +- editor/export/editor_export_plugin.cpp | 2 +- editor/import/3d/resource_importer_scene.cpp | 2 +- .../import/dynamic_font_import_settings.cpp | 16 +++--- .../resource_importer_layered_texture.cpp | 2 +- editor/import_dock.cpp | 2 +- editor/plugins/node_3d_editor_gizmos.cpp | 2 +- editor/plugins/node_3d_editor_plugin.cpp | 8 +-- editor/plugins/path_2d_editor_plugin.cpp | 2 +- editor/plugins/script_editor_plugin.cpp | 34 ++++++------- editor/plugins/text_editor.cpp | 14 +++--- editor/plugins/texture_editor_plugin.cpp | 2 +- editor/scene_tree_dock.cpp | 2 +- editor/themes/editor_theme_manager.cpp | 2 +- modules/fbx/fbx_document.cpp | 9 ++-- modules/gdscript/gdscript_lambda_callable.cpp | 4 +- modules/gdscript/gdscript_parser.cpp | 4 +- .../gdscript_language_protocol.cpp | 6 +-- .../extensions/gltf_document_extension.cpp | 50 +++++++++---------- ...cument_extension_convert_importer_mesh.cpp | 2 +- .../extensions/physics/gltf_physics_shape.cpp | 2 +- modules/gltf/gltf_document.cpp | 24 ++++----- modules/mono/csharp_script.cpp | 2 +- modules/mono/godotsharp_dirs.cpp | 2 +- modules/noise/tests/test_noise_texture_2d.h | 4 +- modules/noise/tests/test_noise_texture_3d.h | 4 +- modules/regex/tests/test_regex.h | 50 +++++++++---------- modules/upnp/upnp.cpp | 12 ++--- modules/zip/zip_packer.cpp | 2 +- modules/zip/zip_reader.cpp | 2 +- scene/2d/animated_sprite_2d.cpp | 4 +- scene/3d/mesh_instance_3d.cpp | 4 +- scene/3d/sprite_3d.cpp | 6 +-- ...avigation_mesh_source_geometry_data_2d.cpp | 2 +- ...avigation_mesh_source_geometry_data_3d.cpp | 2 +- scene/resources/material.cpp | 2 +- scene/resources/shader.cpp | 2 +- scene/resources/surface_tool.cpp | 8 +-- servers/audio/effects/audio_effect_record.cpp | 10 ++-- servers/xr_server.cpp | 4 +- tests/core/io/test_http_client.h | 2 +- tests/scene/test_path_2d.h | 2 +- tests/scene/test_path_3d.h | 2 +- tests/scene/test_primitives.h | 6 +-- 48 files changed, 169 insertions(+), 170 deletions(-) diff --git a/core/extension/gdextension.cpp b/core/extension/gdextension.cpp index e764b9c1125..d4b50facb22 100644 --- a/core/extension/gdextension.cpp +++ b/core/extension/gdextension.cpp @@ -675,7 +675,7 @@ GDExtensionInterfaceFunctionPtr GDExtension::get_interface_function(const String } Error GDExtension::open_library(const String &p_path, const Ref &p_loader) { - ERR_FAIL_NULL_V_MSG(p_loader, FAILED, "Can't open GDExtension without a loader."); + ERR_FAIL_COND_V_MSG(p_loader.is_null(), FAILED, "Can't open GDExtension without a loader."); loader = p_loader; Error err = loader->open_library(p_path); diff --git a/core/io/file_access_encrypted.cpp b/core/io/file_access_encrypted.cpp index b689f5b6284..4b4122f2e3f 100644 --- a/core/io/file_access_encrypted.cpp +++ b/core/io/file_access_encrypted.cpp @@ -37,7 +37,7 @@ #include Error FileAccessEncrypted::open_and_parse(Ref p_base, const Vector &p_key, Mode p_mode, bool p_with_magic) { - ERR_FAIL_COND_V_MSG(file != nullptr, ERR_ALREADY_IN_USE, "Can't open file while another file from path '" + file->get_path_absolute() + "' is open."); + ERR_FAIL_COND_V_MSG(file.is_valid(), ERR_ALREADY_IN_USE, "Can't open file while another file from path '" + file->get_path_absolute() + "' is open."); ERR_FAIL_COND_V(p_key.size() != 32, ERR_INVALID_PARAMETER); pos = 0; @@ -162,7 +162,7 @@ void FileAccessEncrypted::_close() { } bool FileAccessEncrypted::is_open() const { - return file != nullptr; + return file.is_valid(); } String FileAccessEncrypted::get_path() const { diff --git a/core/io/image.cpp b/core/io/image.cpp index f6065d984b9..fcbe483e381 100644 --- a/core/io/image.cpp +++ b/core/io/image.cpp @@ -4225,7 +4225,7 @@ Dictionary Image::compute_image_metrics(const Ref p_compared_image, bool result["root_mean_squared"] = INFINITY; result["peak_snr"] = 0.0f; - ERR_FAIL_NULL_V(p_compared_image, result); + ERR_FAIL_COND_V(p_compared_image.is_null(), result); Error err = OK; Ref compared_image = duplicate(true); if (compared_image->is_compressed()) { diff --git a/core/io/plist.cpp b/core/io/plist.cpp index 86737609bf5..8d91e6dec29 100644 --- a/core/io/plist.cpp +++ b/core/io/plist.cpp @@ -814,7 +814,7 @@ bool PList::load_string(const String &p_string, String &r_err_out) { } PackedByteArray PList::save_asn1() const { - if (root == nullptr) { + if (root.is_null()) { ERR_FAIL_V_MSG(PackedByteArray(), "PList: Invalid PList, no root node."); } size_t size = root->get_asn1_size(1); @@ -848,7 +848,7 @@ PackedByteArray PList::save_asn1() const { } String PList::save_text() const { - if (root == nullptr) { + if (root.is_null()) { ERR_FAIL_V_MSG(String(), "PList: Invalid PList, no root node."); } diff --git a/editor/animation_bezier_editor.cpp b/editor/animation_bezier_editor.cpp index 5196857240c..e4ff73205cc 100644 --- a/editor/animation_bezier_editor.cpp +++ b/editor/animation_bezier_editor.cpp @@ -719,7 +719,7 @@ void AnimationBezierTrackEdit::set_root(Node *p_root) { void AnimationBezierTrackEdit::set_filtered(bool p_filtered) { is_filtered = p_filtered; - if (animation == nullptr) { + if (animation.is_null()) { return; } String base_path = animation->track_get_path(selected_track); diff --git a/editor/export/editor_export_plugin.cpp b/editor/export/editor_export_plugin.cpp index f56dd61e3bd..6bb21d7fd4b 100644 --- a/editor/export/editor_export_plugin.cpp +++ b/editor/export/editor_export_plugin.cpp @@ -140,7 +140,7 @@ Vector EditorExportPlugin::get_ios_project_static_libs() const { } Variant EditorExportPlugin::get_option(const StringName &p_name) const { - ERR_FAIL_NULL_V(export_preset, Variant()); + ERR_FAIL_COND_V(export_preset.is_null(), Variant()); return export_preset->get(p_name); } diff --git a/editor/import/3d/resource_importer_scene.cpp b/editor/import/3d/resource_importer_scene.cpp index fa07511dd08..5c28213ca7a 100644 --- a/editor/import/3d/resource_importer_scene.cpp +++ b/editor/import/3d/resource_importer_scene.cpp @@ -446,7 +446,7 @@ static String _fixstr(const String &p_what, const String &p_str) { } static void _pre_gen_shape_list(Ref &mesh, Vector> &r_shape_list, bool p_convex) { - ERR_FAIL_NULL_MSG(mesh, "Cannot generate shape list with null mesh value."); + ERR_FAIL_COND_MSG(mesh.is_null(), "Cannot generate shape list with null mesh value."); if (!p_convex) { Ref shape = mesh->create_trimesh_shape(); r_shape_list.push_back(shape); diff --git a/editor/import/dynamic_font_import_settings.cpp b/editor/import/dynamic_font_import_settings.cpp index 590e3a9ede0..c526ca0b0ca 100644 --- a/editor/import/dynamic_font_import_settings.cpp +++ b/editor/import/dynamic_font_import_settings.cpp @@ -509,7 +509,7 @@ void DynamicFontImportSettingsDialog::_variation_add() { Ref import_variation_data; import_variation_data.instantiate(); import_variation_data->owner = this; - ERR_FAIL_NULL(import_variation_data); + ERR_FAIL_COND(import_variation_data.is_null()); for (List::Element *E = options_variations.front(); E; E = E->next()) { import_variation_data->defaults[E->get().option.name] = E->get().default_value; @@ -529,7 +529,7 @@ void DynamicFontImportSettingsDialog::_variation_selected() { TreeItem *vars_item = vars_list->get_selected(); if (vars_item) { Ref import_variation_data = vars_item->get_metadata(0); - ERR_FAIL_NULL(import_variation_data); + ERR_FAIL_COND(import_variation_data.is_null()); inspector_vars->edit(import_variation_data.ptr()); import_variation_data->notify_property_list_changed(); @@ -588,14 +588,14 @@ void DynamicFontImportSettingsDialog::_variations_validate() { } for (TreeItem *vars_item_a = vars_list_root->get_first_child(); vars_item_a; vars_item_a = vars_item_a->get_next()) { Ref import_variation_data_a = vars_item_a->get_metadata(0); - ERR_FAIL_NULL(import_variation_data_a); + ERR_FAIL_COND(import_variation_data_a.is_null()); for (TreeItem *vars_item_b = vars_list_root->get_first_child(); vars_item_b; vars_item_b = vars_item_b->get_next()) { if (vars_item_b != vars_item_a) { bool match = true; for (const KeyValue &E : import_variation_data_a->settings) { Ref import_variation_data_b = vars_item_b->get_metadata(0); - ERR_FAIL_NULL(import_variation_data_b); + ERR_FAIL_COND(import_variation_data_b.is_null()); match = match && (import_variation_data_b->settings[E.key] == E.value); } if (match) { @@ -956,7 +956,7 @@ void DynamicFontImportSettingsDialog::_re_import() { Array configurations; for (TreeItem *vars_item = vars_list_root->get_first_child(); vars_item; vars_item = vars_item->get_next()) { Ref import_variation_data = vars_item->get_metadata(0); - ERR_FAIL_NULL(import_variation_data); + ERR_FAIL_COND(import_variation_data.is_null()); Dictionary preload_config; preload_config["name"] = vars_item->get_text(0); @@ -1107,7 +1107,7 @@ void DynamicFontImportSettingsDialog::open_settings(const String &p_path) { inspector_general->edit(nullptr); text_settings_data.instantiate(); - ERR_FAIL_NULL(text_settings_data); + ERR_FAIL_COND(text_settings_data.is_null()); text_settings_data->owner = this; @@ -1137,7 +1137,7 @@ void DynamicFontImportSettingsDialog::open_settings(const String &p_path) { Ref config; config.instantiate(); - ERR_FAIL_NULL(config); + ERR_FAIL_COND(config.is_null()); Error err = config->load(p_path + ".import"); print_verbose("Loading import settings:"); @@ -1169,7 +1169,7 @@ void DynamicFontImportSettingsDialog::open_settings(const String &p_path) { Ref import_variation_data_custom; import_variation_data_custom.instantiate(); - ERR_FAIL_NULL(import_variation_data_custom); + ERR_FAIL_COND(import_variation_data_custom.is_null()); import_variation_data_custom->owner = this; for (List::Element *F = options_variations.front(); F; F = F->next()) { diff --git a/editor/import/resource_importer_layered_texture.cpp b/editor/import/resource_importer_layered_texture.cpp index 212a4160bfc..552815a6afc 100644 --- a/editor/import/resource_importer_layered_texture.cpp +++ b/editor/import/resource_importer_layered_texture.cpp @@ -510,7 +510,7 @@ void ResourceImporterLayeredTexture::_check_compress_ctex(const String &p_source } bool can_compress_hdr = r_texture_import->hdr_compression > 0; - ERR_FAIL_NULL(r_texture_import->image); + ERR_FAIL_COND(r_texture_import->image.is_null()); bool is_hdr = (r_texture_import->image->get_format() >= Image::FORMAT_RF && r_texture_import->image->get_format() <= Image::FORMAT_RGBE9995); ERR_FAIL_NULL(r_texture_import->slices); // Can compress hdr, but hdr with alpha is not compressible. diff --git a/editor/import_dock.cpp b/editor/import_dock.cpp index a8f8e9ef110..16f4aeda957 100644 --- a/editor/import_dock.cpp +++ b/editor/import_dock.cpp @@ -171,7 +171,7 @@ void ImportDock::_add_keep_import_option(const String &p_importer_name) { void ImportDock::_update_options(const String &p_path, const Ref &p_config) { // Set the importer class to fetch the correct class in the XML class reference. // This allows tooltips to display when hovering properties. - if (params->importer != nullptr) { + if (params->importer.is_valid()) { // Null check to avoid crashing if the "Keep File (exported as is)" mode is selected. import_opts->set_object_class(params->importer->get_class_name()); } diff --git a/editor/plugins/node_3d_editor_gizmos.cpp b/editor/plugins/node_3d_editor_gizmos.cpp index 67d5e44ce57..f7e1b1fe510 100644 --- a/editor/plugins/node_3d_editor_gizmos.cpp +++ b/editor/plugins/node_3d_editor_gizmos.cpp @@ -982,7 +982,7 @@ void EditorNode3DGizmoPlugin::create_handle_material(const String &p_name, bool handle_material->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED); handle_material->set_flag(StandardMaterial3D::FLAG_USE_POINT_SIZE, true); - Ref handle_t = p_icon != nullptr ? p_icon : EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("Editor3DHandle"), EditorStringName(EditorIcons)); + Ref handle_t = p_icon.is_valid() ? p_icon : EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("Editor3DHandle"), EditorStringName(EditorIcons)); handle_material->set_point_size(handle_t->get_width()); handle_material->set_texture(StandardMaterial3D::TEXTURE_ALBEDO, handle_t); handle_material->set_albedo(Color(1, 1, 1)); diff --git a/editor/plugins/node_3d_editor_plugin.cpp b/editor/plugins/node_3d_editor_plugin.cpp index 8b0f4a64a74..b6b9ca2da8b 100644 --- a/editor/plugins/node_3d_editor_plugin.cpp +++ b/editor/plugins/node_3d_editor_plugin.cpp @@ -4506,8 +4506,8 @@ bool Node3DEditorViewport::_create_instance(Node *p_parent, const String &p_path Node *instantiated_scene = nullptr; - if (mesh != nullptr || scene != nullptr) { - if (mesh != nullptr) { + if (mesh.is_valid() || scene.is_valid()) { + if (mesh.is_valid()) { MeshInstance3D *mesh_instance = memnew(MeshInstance3D); mesh_instance->set_mesh(mesh); @@ -4538,7 +4538,7 @@ bool Node3DEditorViewport::_create_instance(Node *p_parent, const String &p_path } } - if (scene != nullptr) { + if (scene.is_valid()) { instantiated_scene->set_scene_file_path(ProjectSettings::get_singleton()->localize_path(p_path)); } @@ -9291,7 +9291,7 @@ struct _GizmoPluginNameComparator { }; void Node3DEditor::add_gizmo_plugin(Ref p_plugin) { - ERR_FAIL_NULL(p_plugin.ptr()); + ERR_FAIL_COND(p_plugin.is_null()); gizmo_plugins_by_priority.push_back(p_plugin); gizmo_plugins_by_priority.sort_custom<_GizmoPluginPriorityComparator>(); diff --git a/editor/plugins/path_2d_editor_plugin.cpp b/editor/plugins/path_2d_editor_plugin.cpp index 5b23cf44d03..87b57d40d91 100644 --- a/editor/plugins/path_2d_editor_plugin.cpp +++ b/editor/plugins/path_2d_editor_plugin.cpp @@ -295,7 +295,7 @@ bool Path2DEditor::forward_gui_input(const Ref &p_event) { Vector2 gpoint = mm->get_position(); Ref curve = node->get_curve(); - if (curve == nullptr) { + if (curve.is_null()) { return true; } if (curve->get_point_count() < 2) { diff --git a/editor/plugins/script_editor_plugin.cpp b/editor/plugins/script_editor_plugin.cpp index d7de5a72230..dd3af119b3a 100644 --- a/editor/plugins/script_editor_plugin.cpp +++ b/editor/plugins/script_editor_plugin.cpp @@ -510,7 +510,7 @@ void ScriptEditor::_set_execution(Ref p_script, int p_line) { continue; } - if ((scr != nullptr && se->get_edited_resource() == p_script) || se->get_edited_resource()->get_path() == scr->get_path()) { + if ((scr.is_valid() && se->get_edited_resource() == p_script) || se->get_edited_resource()->get_path() == scr->get_path()) { se->set_executing_line(p_line); } } @@ -526,7 +526,7 @@ void ScriptEditor::_clear_execution(Ref p_script) { continue; } - if ((scr != nullptr && se->get_edited_resource() == p_script) || se->get_edited_resource()->get_path() == scr->get_path()) { + if ((scr.is_valid() && se->get_edited_resource() == p_script) || se->get_edited_resource()->get_path() == scr->get_path()) { se->clear_executing_line(); } } @@ -711,7 +711,7 @@ void ScriptEditor::_go_to_tab(int p_idx) { } Ref