| 1 | /**************************************************************************/ |
| 2 | /* gdscript_warning.h */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #pragma once |
| 32 | |
| 33 | #ifdef DEBUG_ENABLED |
| 34 | |
| 35 | #include "core/object/object.h" |
| 36 | #include "core/string/ustring.h" |
| 37 | #include "core/templates/vector.h" |
| 38 | |
| 39 | class GDScriptWarning { |
| 40 | public: |
| 41 | enum WarnLevel { |
| 42 | IGNORE, |
| 43 | WARN, |
| 44 | ERROR |
| 45 | }; |
| 46 | |
| 47 | enum Code { |
| 48 | UNASSIGNED_VARIABLE, // Variable used but never assigned. |
| 49 | UNASSIGNED_VARIABLE_OP_ASSIGN, // Variable never assigned but used in an assignment operation (+=, *=, etc). |
| 50 | UNUSED_VARIABLE, // Local variable is declared but never used. |
| 51 | UNUSED_LOCAL_CONSTANT, // Local constant is declared but never used. |
| 52 | UNUSED_PRIVATE_CLASS_VARIABLE, // Class variable is declared private ("_" prefix) but never used in the class. |
| 53 | UNUSED_PARAMETER, // Function parameter is never used. |
| 54 | UNUSED_SIGNAL, // Signal is defined but never explicitly used in the class. |
| 55 | SHADOWED_VARIABLE, // A local variable/constant shadows a current class member. |
| 56 | SHADOWED_VARIABLE_BASE_CLASS, // A local variable/constant shadows a base class member. |
| 57 | SHADOWED_GLOBAL_IDENTIFIER, // A global class or function has the same name as variable. |
| 58 | UNREACHABLE_CODE, // Code after a return statement. |
| 59 | UNREACHABLE_PATTERN, // Pattern in a match statement after a catch all pattern (wildcard or bind). |
| 60 | STANDALONE_EXPRESSION, // Expression not assigned to a variable. |
| 61 | STANDALONE_TERNARY, // Return value of ternary expression is discarded. |
| 62 | INCOMPATIBLE_TERNARY, // Possible values of a ternary if are not mutually compatible. |
| 63 | UNTYPED_DECLARATION, // Variable/parameter/function has no static type, explicitly specified or implicitly inferred. |
| 64 | INFERRED_DECLARATION, // Variable/constant/parameter has an implicitly inferred static type. |
| 65 | UNSAFE_PROPERTY_ACCESS, // Property not found in the detected type (but can be in subtypes). |
| 66 | UNSAFE_METHOD_ACCESS, // Function not found in the detected type (but can be in subtypes). |
| 67 | UNSAFE_CAST, // Casting a `Variant` value to non-`Variant`. |
| 68 | UNSAFE_CALL_ARGUMENT, // Function call argument is of a supertype of the required type. |
| 69 | UNSAFE_VOID_RETURN, // Function returns void but returned a call to a function that can't be type checked. |
| 70 | RETURN_VALUE_DISCARDED, // Function call returns something but the value isn't used. |
| 71 | STATIC_CALLED_ON_INSTANCE, // A static method was called on an instance of a class instead of on the class itself. |
| 72 | MISSING_TOOL, // The base class script has the "@tool" annotation, but this script does not have it. |
| 73 | REDUNDANT_STATIC_UNLOAD, // The `@static_unload` annotation is used but the class does not have static data. |
| 74 | REDUNDANT_AWAIT, // await is used but expression is synchronous (not a signal nor a coroutine). |
| 75 | MISSING_AWAIT, // await is not used but expression is a coroutine. |
| 76 | ASSERT_ALWAYS_TRUE, // Expression for assert argument is always true. |
| 77 | ASSERT_ALWAYS_FALSE, // Expression for assert argument is always false. |
| 78 | INTEGER_DIVISION, // Integer divide by integer, decimal part is discarded. |
| 79 | NARROWING_CONVERSION, // Float value into an integer slot, precision is lost. |
| 80 | INT_AS_ENUM_WITHOUT_CAST, // An integer value was used as an enum value without casting. |
| 81 | INT_AS_ENUM_WITHOUT_MATCH, // An integer value was used as an enum value without matching enum member. |
| 82 | ENUM_VARIABLE_WITHOUT_DEFAULT, // A variable with an enum type does not have a default value. The default will be set to `0` instead of the first enum value. |
| 83 | EMPTY_FILE, // A script file is empty. |
| 84 | DEPRECATED_KEYWORD, // The keyword is deprecated and should be replaced. |
| 85 | CONFUSABLE_IDENTIFIER, // The identifier contains misleading characters that can be confused. E.g. "usеr" (has Cyrillic "е" instead of Latin "e"). |
| 86 | CONFUSABLE_LOCAL_DECLARATION, // The parent block declares an identifier with the same name below. |
| 87 | CONFUSABLE_LOCAL_USAGE, // The identifier will be shadowed below in the block. |
| 88 | CONFUSABLE_CAPTURE_REASSIGNMENT, // Reassigning lambda capture does not modify the outer local variable. |
| 89 | INFERENCE_ON_VARIANT, // The declaration uses type inference but the value is typed as Variant. |
| 90 | NATIVE_METHOD_OVERRIDE, // The script method overrides a native one, this may not work as intended. |
| 91 | GET_NODE_DEFAULT_WITHOUT_ONREADY, // A class variable uses `get_node()` (or the `$` notation) as its default value, but does not use the @onready annotation. |
| 92 | ONREADY_WITH_EXPORT, // The `@onready` annotation will set the value after `@export` which is likely not intended. |
| 93 | #ifndef DISABLE_DEPRECATED |
| 94 | PROPERTY_USED_AS_FUNCTION, // Function not found, but there's a property with the same name. |
| 95 | CONSTANT_USED_AS_FUNCTION, // Function not found, but there's a constant with the same name. |
| 96 | FUNCTION_USED_AS_PROPERTY, // Property not found, but there's a function with the same name. |
| 97 | #endif // DISABLE_DEPRECATED |
| 98 | WARNING_MAX, |
| 99 | }; |
| 100 | |
| 101 | #ifndef DISABLE_DEPRECATED |
| 102 | static constexpr int FIRST_DEPRECATED_WARNING = PROPERTY_USED_AS_FUNCTION; |
| 103 | #endif // DISABLE_DEPRECATED |
| 104 | |
| 105 | constexpr static WarnLevel default_warning_levels[] = { |
| 106 | WARN, // UNASSIGNED_VARIABLE |
| 107 | WARN, // UNASSIGNED_VARIABLE_OP_ASSIGN |
| 108 | WARN, // UNUSED_VARIABLE |
| 109 | WARN, // UNUSED_LOCAL_CONSTANT |
| 110 | WARN, // UNUSED_PRIVATE_CLASS_VARIABLE |
| 111 | WARN, // UNUSED_PARAMETER |
| 112 | WARN, // UNUSED_SIGNAL |
| 113 | WARN, // SHADOWED_VARIABLE |
| 114 | WARN, // SHADOWED_VARIABLE_BASE_CLASS |
| 115 | WARN, // SHADOWED_GLOBAL_IDENTIFIER |
| 116 | WARN, // UNREACHABLE_CODE |
| 117 | WARN, // UNREACHABLE_PATTERN |
| 118 | WARN, // STANDALONE_EXPRESSION |
| 119 | WARN, // STANDALONE_TERNARY |
| 120 | WARN, // INCOMPATIBLE_TERNARY |
| 121 | IGNORE, // UNTYPED_DECLARATION // Static typing is optional, we don't want to spam warnings. |
| 122 | IGNORE, // INFERRED_DECLARATION // Static typing is optional, we don't want to spam warnings. |
| 123 | IGNORE, // UNSAFE_PROPERTY_ACCESS // Too common in untyped scenarios. |
| 124 | IGNORE, // UNSAFE_METHOD_ACCESS // Too common in untyped scenarios. |
| 125 | IGNORE, // UNSAFE_CAST // Too common in untyped scenarios. |
| 126 | IGNORE, // UNSAFE_CALL_ARGUMENT // Too common in untyped scenarios. |
| 127 | WARN, // UNSAFE_VOID_RETURN |
| 128 | IGNORE, // RETURN_VALUE_DISCARDED // Too spammy by default on common cases (connect, Tween, etc.). |
| 129 | WARN, // STATIC_CALLED_ON_INSTANCE |
| 130 | WARN, // MISSING_TOOL |
| 131 | WARN, // REDUNDANT_STATIC_UNLOAD |
| 132 | WARN, // REDUNDANT_AWAIT |
| 133 | IGNORE, // MISSING_AWAIT |
| 134 | WARN, // ASSERT_ALWAYS_TRUE |
| 135 | WARN, // ASSERT_ALWAYS_FALSE |
| 136 | WARN, // INTEGER_DIVISION |
| 137 | WARN, // NARROWING_CONVERSION |
| 138 | WARN, // INT_AS_ENUM_WITHOUT_CAST |
| 139 | WARN, // INT_AS_ENUM_WITHOUT_MATCH |
| 140 | WARN, // ENUM_VARIABLE_WITHOUT_DEFAULT |
| 141 | WARN, // EMPTY_FILE |
| 142 | WARN, // DEPRECATED_KEYWORD |
| 143 | WARN, // CONFUSABLE_IDENTIFIER |
| 144 | WARN, // CONFUSABLE_LOCAL_DECLARATION |
| 145 | WARN, // CONFUSABLE_LOCAL_USAGE |
| 146 | WARN, // CONFUSABLE_CAPTURE_REASSIGNMENT |
| 147 | ERROR, // INFERENCE_ON_VARIANT // Most likely done by accident, usually inference is trying for a particular type. |
| 148 | ERROR, // NATIVE_METHOD_OVERRIDE // May not work as expected. |
| 149 | ERROR, // GET_NODE_DEFAULT_WITHOUT_ONREADY // May not work as expected. |
| 150 | ERROR, // ONREADY_WITH_EXPORT // May not work as expected. |
| 151 | #ifndef DISABLE_DEPRECATED |
| 152 | WARN, // PROPERTY_USED_AS_FUNCTION |
| 153 | WARN, // CONSTANT_USED_AS_FUNCTION |
| 154 | WARN, // FUNCTION_USED_AS_PROPERTY |
| 155 | #endif // DISABLE_DEPRECATED |
| 156 | }; |
| 157 | |
| 158 | static_assert(std_size(default_warning_levels) == WARNING_MAX, "Amount of default levels does not match the amount of warnings."); |
| 159 | |
| 160 | Code code = WARNING_MAX; |
| 161 | int start_line = -1, end_line = -1; |
| 162 | Vector<String> symbols; |
| 163 | |
| 164 | String get_name() const; |
| 165 | String get_message() const; |
| 166 | static int get_default_value(Code p_code); |
| 167 | static PropertyInfo get_property_info(Code p_code); |
| 168 | static String get_name_from_code(Code p_code); |
| 169 | static String get_setting_path_from_code(Code p_code); |
| 170 | static Code get_code_from_name(const String &p_name); |
| 171 | }; |
| 172 | |
| 173 | #endif // DEBUG_ENABLED |