From 579feb387c9af6c75f1c599be9d51c380e8efd52 Mon Sep 17 00:00:00 2001 From: Thaddeus Crews Date: Sun, 23 Jun 2024 12:28:04 -0500 Subject: [PATCH] Core: Add `[[nodiscard]]` to string-like classes --- core/io/image.cpp | 2 +- core/io/ip_address.h | 2 +- core/io/resource_format_binary.cpp | 2 +- core/string/node_path.h | 2 +- core/string/string_name.h | 2 +- core/string/ustring.h | 6 +++--- editor/script_create_dialog.cpp | 3 ++- modules/gdscript/gdscript.cpp | 3 ++- modules/gdscript/language_server/gdscript_workspace.cpp | 7 +++---- scene/resources/animation_library.cpp | 5 +---- servers/rendering/shader_compiler.cpp | 3 ++- servers/rendering/shader_preprocessor.cpp | 6 ++++-- 12 files changed, 22 insertions(+), 21 deletions(-) diff --git a/core/io/image.cpp b/core/io/image.cpp index e10a2a88ae4..4b7b55e5a35 100644 --- a/core/io/image.cpp +++ b/core/io/image.cpp @@ -2266,7 +2266,7 @@ void Image::initialize_data(const char **p_xpm) { switch (status) { case READING_HEADER: { String line_str = line_ptr; - line_str.replace_char('\t', ' '); + line_str = line_str.replace_char('\t', ' '); size_width = line_str.get_slicec(' ', 0).to_int(); size_height = line_str.get_slicec(' ', 1).to_int(); diff --git a/core/io/ip_address.h b/core/io/ip_address.h index 114038d902b..f7e86962676 100644 --- a/core/io/ip_address.h +++ b/core/io/ip_address.h @@ -32,7 +32,7 @@ #include "core/string/ustring.h" -struct IPAddress { +struct [[nodiscard]] IPAddress { private: union { uint8_t field8[16]; diff --git a/core/io/resource_format_binary.cpp b/core/io/resource_format_binary.cpp index a0a156ad445..f089fd345d8 100644 --- a/core/io/resource_format_binary.cpp +++ b/core/io/resource_format_binary.cpp @@ -1195,7 +1195,7 @@ String ResourceLoaderBinary::recognize_script_class(Ref p_f) { return ""; } - get_unicode_string(); // type + _ALLOW_DISCARD_ get_unicode_string(); // type f->get_64(); // Metadata offset uint32_t flags = f->get_32(); diff --git a/core/string/node_path.h b/core/string/node_path.h index 2dce2d1c403..75a8c15c842 100644 --- a/core/string/node_path.h +++ b/core/string/node_path.h @@ -33,7 +33,7 @@ #include "core/string/string_name.h" #include "core/string/ustring.h" -class NodePath { +class [[nodiscard]] NodePath { struct Data { SafeRefCount refcount; Vector path; diff --git a/core/string/string_name.h b/core/string/string_name.h index 8f68242978f..e1c319f4e38 100644 --- a/core/string/string_name.h +++ b/core/string/string_name.h @@ -37,7 +37,7 @@ class Main; -class StringName { +class [[nodiscard]] StringName { struct Table; struct _Data { diff --git a/core/string/ustring.h b/core/string/ustring.h index fc153a52e75..8229ff3a4af 100644 --- a/core/string/ustring.h +++ b/core/string/ustring.h @@ -127,7 +127,7 @@ constexpr int64_t str_compare(const L *l_ptr, const R *r_ptr) { /*************************************************************************/ template -class CharProxy { +class [[nodiscard]] CharProxy { friend String; friend CharStringT; @@ -170,7 +170,7 @@ public: /*************************************************************************/ template -class CharStringT { +class [[nodiscard]] CharStringT { CowData _cowdata; static constexpr T _null = 0; @@ -265,7 +265,7 @@ using Char16String = CharStringT; /* String */ /*************************************************************************/ -class String { +class [[nodiscard]] String { CowData _cowdata; static constexpr char32_t _null = 0; static constexpr char32_t _replacement_char = 0xfffd; diff --git a/editor/script_create_dialog.cpp b/editor/script_create_dialog.cpp index 2bb12e73906..90378c06900 100644 --- a/editor/script_create_dialog.cpp +++ b/editor/script_create_dialog.cpp @@ -59,7 +59,8 @@ static String _get_parent_class_of_script(const String &p_path) { // Inherits from a built-in class. if (base.is_null()) { - script->get_language()->get_global_class_name(script->get_path(), &class_name); + // We only care about the referenced class_name. + _ALLOW_DISCARD_ script->get_language()->get_global_class_name(script->get_path(), &class_name); return class_name; } diff --git a/modules/gdscript/gdscript.cpp b/modules/gdscript/gdscript.cpp index 0d229c04d61..645980b0aa4 100644 --- a/modules/gdscript/gdscript.cpp +++ b/modules/gdscript/gdscript.cpp @@ -2854,7 +2854,8 @@ String GDScriptLanguage::get_global_class_name(const String &p_path, String *r_b if (subclass->extends_used) { if (!subclass->extends_path.is_empty()) { if (subclass->extends.is_empty()) { - get_global_class_name(subclass->extends_path, r_base_type); + // We only care about the referenced class_name. + _ALLOW_DISCARD_ get_global_class_name(subclass->extends_path, r_base_type); subclass = nullptr; break; } else { diff --git a/modules/gdscript/language_server/gdscript_workspace.cpp b/modules/gdscript/language_server/gdscript_workspace.cpp index 2014dccae12..e7847700c3e 100644 --- a/modules/gdscript/language_server/gdscript_workspace.cpp +++ b/modules/gdscript/language_server/gdscript_workspace.cpp @@ -489,7 +489,8 @@ bool GDScriptWorkspace::can_rename(const LSP::TextDocumentPositionParams &p_doc_ String path = get_file_path(p_doc_pos.textDocument.uri); if (const ExtendGDScriptParser *parser = get_parse_result(path)) { - parser->get_identifier_under_position(p_doc_pos.position, r_range); + // We only care about the range. + _ALLOW_DISCARD_ parser->get_identifier_under_position(p_doc_pos.position, r_range); r_symbol = *reference_symbol; return true; } @@ -847,9 +848,7 @@ Error GDScriptWorkspace::resolve_signature(const LSP::TextDocumentPositionParams return ERR_METHOD_NOT_FOUND; } -GDScriptWorkspace::GDScriptWorkspace() { - ProjectSettings::get_singleton()->get_resource_path(); -} +GDScriptWorkspace::GDScriptWorkspace() {} GDScriptWorkspace::~GDScriptWorkspace() { HashSet cached_parsers; diff --git a/scene/resources/animation_library.cpp b/scene/resources/animation_library.cpp index a4e81f50f63..899f8b45569 100644 --- a/scene/resources/animation_library.cpp +++ b/scene/resources/animation_library.cpp @@ -41,10 +41,7 @@ bool AnimationLibrary::is_valid_library_name(const String &p_name) { } String AnimationLibrary::validate_library_name(const String &p_name) { - String name = p_name; - static const char *characters = "/:,["; - name.replace_chars(characters, '_'); - return name; + return p_name.replace_chars("/:,[", '_'); } Error AnimationLibrary::add_animation(const StringName &p_name, const Ref &p_animation) { diff --git a/servers/rendering/shader_compiler.cpp b/servers/rendering/shader_compiler.cpp index 8c7fae49bea..397da80b9f0 100644 --- a/servers/rendering/shader_compiler.cpp +++ b/servers/rendering/shader_compiler.cpp @@ -1563,7 +1563,8 @@ Error ShaderCompiler::compile(RS::ShaderMode p_mode, const String &p_code, Ident shader = parser.get_shader(); function = nullptr; - _dump_node_code(shader, 1, r_gen_code, *p_actions, actions, false); + // Return value only relevant within nested calls. + _ALLOW_DISCARD_ _dump_node_code(shader, 1, r_gen_code, *p_actions, actions, false); return OK; } diff --git a/servers/rendering/shader_preprocessor.cpp b/servers/rendering/shader_preprocessor.cpp index 24866bdb4ac..ec167ac2ae2 100644 --- a/servers/rendering/shader_preprocessor.cpp +++ b/servers/rendering/shader_preprocessor.cpp @@ -552,7 +552,8 @@ void ShaderPreprocessor::process_else(Tokenizer *p_tokenizer) { if (skip) { Vector ends; ends.push_back("endif"); - next_directive(p_tokenizer, ends); + // Legacy return value. + _ALLOW_DISCARD_ next_directive(p_tokenizer, ends); } } @@ -847,7 +848,8 @@ void ShaderPreprocessor::start_branch_condition(Tokenizer *p_tokenizer, bool p_s ends.push_back("elif"); ends.push_back("else"); ends.push_back("endif"); - next_directive(p_tokenizer, ends); + // Legacy return value. + _ALLOW_DISCARD_ next_directive(p_tokenizer, ends); } }