Debian Codegraph @

modules/gdscript/gdscript_warning.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* gdscript_warning.cpp */
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 #include "gdscript_warning.h"
32
33 #include "core/variant/variant.h"
34
35 #ifdef DEBUG_ENABLED
36
37 String GDScriptWarning::get_message() const {
38 #define CHECK_SYMBOLS(m_amount) ERR_FAIL_COND_V(symbols.size() < m_amount, String());
39
40 switch (code) {
41 case UNASSIGNED_VARIABLE:
42 CHECK_SYMBOLS(1);
43 return vformat(R"(The variable "%s" is used before being assigned a value.)", symbols[0]);
44 case UNASSIGNED_VARIABLE_OP_ASSIGN:
45 CHECK_SYMBOLS(2);
46 return vformat(R"(The variable "%s" is modified with the compound-assignment operator "%s=" but was not previously initialized.)", symbols[0], symbols[1]);
47 case UNUSED_VARIABLE:
48 CHECK_SYMBOLS(1);
49 return vformat(R"(The local variable "%s" is declared but never used in the block. If this is intended, prefix it with an underscore: "_%s".)", symbols[0], symbols[0]);
50 case UNUSED_LOCAL_CONSTANT:
51 CHECK_SYMBOLS(1);
52 return vformat(R"(The local constant "%s" is declared but never used in the block. If this is intended, prefix it with an underscore: "_%s".)", symbols[0], symbols[0]);
53 case UNUSED_PRIVATE_CLASS_VARIABLE:
54 CHECK_SYMBOLS(1);
55 return vformat(R"(The class variable "%s" is declared but never used in the class.)", symbols[0]);
56 case UNUSED_PARAMETER:
57 CHECK_SYMBOLS(2);
58 return vformat(R"*(The parameter "%s" is never used in the function "%s()". If this is intended, prefix it with an underscore: "_%s".)*", symbols[1], symbols[0], symbols[1]);
59 case UNUSED_SIGNAL:
60 CHECK_SYMBOLS(1);
61 return vformat(R"(The signal "%s" is declared but never explicitly used in the class.)", symbols[0]);
62 case SHADOWED_VARIABLE:
63 CHECK_SYMBOLS(4);
64 return vformat(R"(The local %s "%s" is shadowing an already-declared %s at line %s in the current class.)", symbols[0], symbols[1], symbols[2], symbols[3]);
65 case SHADOWED_VARIABLE_BASE_CLASS:
66 CHECK_SYMBOLS(4);
67 if (symbols.size() > 4) {
68 return vformat(R"(The local %s "%s" is shadowing an already-declared %s at line %s in the base class "%s".)", symbols[0], symbols[1], symbols[2], symbols[3], symbols[4]);
69 }
70 return vformat(R"(The local %s "%s" is shadowing an already-declared %s in the base class "%s".)", symbols[0], symbols[1], symbols[2], symbols[3]);
71 case SHADOWED_GLOBAL_IDENTIFIER:
72 CHECK_SYMBOLS(3);
73 return vformat(R"(The %s "%s" has the same name as a %s.)", symbols[0], symbols[1], symbols[2]);
74 case UNREACHABLE_CODE:
75 CHECK_SYMBOLS(1);
76 return vformat(R"*(Unreachable code (statement after return) in function "%s()".)*", symbols[0]);
77 case UNREACHABLE_PATTERN:
78 return "Unreachable pattern (pattern after wildcard or bind).";
79 case STANDALONE_EXPRESSION:
80 return "Standalone expression (the line may have no effect).";
81 case STANDALONE_TERNARY:
82 return "Standalone ternary operator (the return value is being discarded).";
83 case INCOMPATIBLE_TERNARY:
84 return "Values of the ternary operator are not mutually compatible.";
85 case UNTYPED_DECLARATION:
86 CHECK_SYMBOLS(2);
87 if (symbols[0] == "Function") {
88 return vformat(R"*(%s "%s()" has no static return type.)*", symbols[0], symbols[1]);
89 }
90 return vformat(R"(%s "%s" has no static type.)", symbols[0], symbols[1]);
91 case INFERRED_DECLARATION:
92 CHECK_SYMBOLS(2);
93 return vformat(R"(%s "%s" has an implicitly inferred static type.)", symbols[0], symbols[1]);
94 case UNSAFE_PROPERTY_ACCESS:
95 CHECK_SYMBOLS(2);
96 return vformat(R"(The property "%s" is not present on the inferred type "%s" (but may be present on a subtype).)", symbols[0], symbols[1]);
97 case UNSAFE_METHOD_ACCESS:
98 CHECK_SYMBOLS(2);
99 return vformat(R"*(The method "%s()" is not present on the inferred type "%s" (but may be present on a subtype).)*", symbols[0], symbols[1]);
100 case UNSAFE_CAST:
101 CHECK_SYMBOLS(1);
102 return vformat(R"(Casting "Variant" to "%s" is unsafe.)", symbols[0]);
103 case UNSAFE_CALL_ARGUMENT:
104 CHECK_SYMBOLS(5);
105 return vformat(R"*(The argument %s of the %s "%s()" requires the subtype "%s" but the supertype "%s" was provided.)*", symbols[0], symbols[1], symbols[2], symbols[3], symbols[4]);
106 case UNSAFE_VOID_RETURN:
107 CHECK_SYMBOLS(2);
108 return vformat(R"*(The method "%s()" returns "void" but it's trying to return a call to "%s()" that can't be ensured to also be "void".)*", symbols[0], symbols[1]);
109 case RETURN_VALUE_DISCARDED:
110 CHECK_SYMBOLS(1);
111 return vformat(R"*(The function "%s()" returns a value that will be discarded if not used.)*", symbols[0]);
112 case STATIC_CALLED_ON_INSTANCE:
113 CHECK_SYMBOLS(2);
114 return vformat(R"*(The function "%s()" is a static function but was called from an instance. Instead, it should be directly called from the type: "%s.%s()".)*", symbols[0], symbols[1], symbols[0]);
115 case MISSING_TOOL:
116 return R"(The base class script has the "@tool" annotation, but this script does not have it.)";
117 case REDUNDANT_STATIC_UNLOAD:
118 return R"(The "@static_unload" annotation is redundant because the file does not have a class with static variables.)";
119 case REDUNDANT_AWAIT:
120 return R"("await" keyword is unnecessary because the expression isn't a coroutine nor a signal.)";
121 case MISSING_AWAIT:
122 return R"("await" keyword might be desired because the expression is a coroutine.)";
123 case ASSERT_ALWAYS_TRUE:
124 return "Assert statement is redundant because the expression is always true.";
125 case ASSERT_ALWAYS_FALSE:
126 return "Assert statement will raise an error because the expression is always false.";
127 case INTEGER_DIVISION:
128 return "Integer division. Decimal part will be discarded.";
129 case NARROWING_CONVERSION:
130 return "Narrowing conversion (float is converted to int and loses precision).";
131 case INT_AS_ENUM_WITHOUT_CAST:
132 return "Integer used when an enum value is expected. If this is intended, cast the integer to the enum type using the \"as\" keyword.";
133 case INT_AS_ENUM_WITHOUT_MATCH:
134 CHECK_SYMBOLS(3);
135 return vformat(R"(Cannot %s %s as Enum "%s": no enum member has matching value.)", symbols[0], symbols[1], symbols[2]);
136 case ENUM_VARIABLE_WITHOUT_DEFAULT:
137 CHECK_SYMBOLS(1);
138 return vformat(R"(The variable "%s" has an enum type and does not set an explicit default value. The default will be set to "0".)", symbols[0]);
139 case EMPTY_FILE:
140 return "Empty script file.";
141 case DEPRECATED_KEYWORD:
142 CHECK_SYMBOLS(2);
143 return vformat(R"(The "%s" keyword is deprecated and will be removed in a future release. Please replace it with "%s".)", symbols[0], symbols[1]);
144 case CONFUSABLE_IDENTIFIER:
145 CHECK_SYMBOLS(1);
146 return vformat(R"(The identifier "%s" has misleading characters and might be confused with something else.)", symbols[0]);
147 case CONFUSABLE_LOCAL_DECLARATION:
148 CHECK_SYMBOLS(2);
149 return vformat(R"(The %s "%s" is declared below in the parent block.)", symbols[0], symbols[1]);
150 case CONFUSABLE_LOCAL_USAGE:
151 CHECK_SYMBOLS(1);
152 return vformat(R"(The identifier "%s" will be shadowed below in the block.)", symbols[0]);
153 case CONFUSABLE_CAPTURE_REASSIGNMENT:
154 CHECK_SYMBOLS(1);
155 return vformat(R"(Reassigning lambda capture does not modify the outer local variable "%s".)", symbols[0]);
156 case INFERENCE_ON_VARIANT:
157 CHECK_SYMBOLS(1);
158 return vformat("The %s type is being inferred from a Variant value, so it will be typed as Variant.", symbols[0]);
159 case NATIVE_METHOD_OVERRIDE:
160 CHECK_SYMBOLS(2);
161 return vformat(R"*(The method "%s()" overrides a method from native class "%s". This won't be called by the engine and may not work as expected.)*", symbols[0], symbols[1]);
162 case GET_NODE_DEFAULT_WITHOUT_ONREADY:
163 CHECK_SYMBOLS(1);
164 return vformat(R"*(The default value uses "%s" which won't return nodes in the scene tree before "_ready()" is called. Use the "@onready" annotation to solve this.)*", symbols[0]);
165 case ONREADY_WITH_EXPORT:
166 return R"("@onready" will set the default value after "@export" takes effect and will override it.)";
167 #ifndef DISABLE_DEPRECATED
168 // Never produced. These warnings migrated from 3.x by mistake.
169 case PROPERTY_USED_AS_FUNCTION: // There is already an error.
170 case CONSTANT_USED_AS_FUNCTION: // There is already an error.
171 case FUNCTION_USED_AS_PROPERTY: // This is valid, returns `Callable`.
172 break;
173 #endif // DISABLE_DEPRECATED
174 case WARNING_MAX:
175 break; // Can't happen, but silences warning.
176 }
177 ERR_FAIL_V_MSG(String(), vformat(R"(Invalid GDScript warning "%s".)", get_name_from_code(code)));
178
179 #undef CHECK_SYMBOLS
180 }
181
182 int GDScriptWarning::get_default_value(Code p_code) {
183 ERR_FAIL_INDEX_V_MSG(p_code, WARNING_MAX, WarnLevel::IGNORE, "Getting default value of invalid warning code.");
184 return default_warning_levels[p_code];
185 }
186
187 PropertyInfo GDScriptWarning::get_property_info(Code p_code) {
188 return PropertyInfo(Variant::INT, get_setting_path_from_code(p_code), PROPERTY_HINT_ENUM, "Ignore,Warn,Error");
189 }
190
191 String GDScriptWarning::get_name() const {
192 return get_name_from_code(code);
193 }
194
195 String GDScriptWarning::get_name_from_code(Code p_code) {
196 ERR_FAIL_COND_V(p_code < 0 || p_code >= WARNING_MAX, String());
197
198 static const char *names[] = {
199 PNAME("UNASSIGNED_VARIABLE"),
200 PNAME("UNASSIGNED_VARIABLE_OP_ASSIGN"),
201 PNAME("UNUSED_VARIABLE"),
202 PNAME("UNUSED_LOCAL_CONSTANT"),
203 PNAME("UNUSED_PRIVATE_CLASS_VARIABLE"),
204 PNAME("UNUSED_PARAMETER"),
205 PNAME("UNUSED_SIGNAL"),
206 PNAME("SHADOWED_VARIABLE"),
207 PNAME("SHADOWED_VARIABLE_BASE_CLASS"),
208 PNAME("SHADOWED_GLOBAL_IDENTIFIER"),
209 PNAME("UNREACHABLE_CODE"),
210 PNAME("UNREACHABLE_PATTERN"),
211 PNAME("STANDALONE_EXPRESSION"),
212 PNAME("STANDALONE_TERNARY"),
213 PNAME("INCOMPATIBLE_TERNARY"),
214 PNAME("UNTYPED_DECLARATION"),
215 PNAME("INFERRED_DECLARATION"),
216 PNAME("UNSAFE_PROPERTY_ACCESS"),
217 PNAME("UNSAFE_METHOD_ACCESS"),
218 PNAME("UNSAFE_CAST"),
219 PNAME("UNSAFE_CALL_ARGUMENT"),
220 PNAME("UNSAFE_VOID_RETURN"),
221 PNAME("RETURN_VALUE_DISCARDED"),
222 PNAME("STATIC_CALLED_ON_INSTANCE"),
223 PNAME("MISSING_TOOL"),
224 PNAME("REDUNDANT_STATIC_UNLOAD"),
225 PNAME("REDUNDANT_AWAIT"),
226 PNAME("MISSING_AWAIT"),
227 PNAME("ASSERT_ALWAYS_TRUE"),
228 PNAME("ASSERT_ALWAYS_FALSE"),
229 PNAME("INTEGER_DIVISION"),
230 PNAME("NARROWING_CONVERSION"),
231 PNAME("INT_AS_ENUM_WITHOUT_CAST"),
232 PNAME("INT_AS_ENUM_WITHOUT_MATCH"),
233 PNAME("ENUM_VARIABLE_WITHOUT_DEFAULT"),
234 PNAME("EMPTY_FILE"),
235 PNAME("DEPRECATED_KEYWORD"),
236 PNAME("CONFUSABLE_IDENTIFIER"),
237 PNAME("CONFUSABLE_LOCAL_DECLARATION"),
238 PNAME("CONFUSABLE_LOCAL_USAGE"),
239 PNAME("CONFUSABLE_CAPTURE_REASSIGNMENT"),
240 PNAME("INFERENCE_ON_VARIANT"),
241 PNAME("NATIVE_METHOD_OVERRIDE"),
242 PNAME("GET_NODE_DEFAULT_WITHOUT_ONREADY"),
243 PNAME("ONREADY_WITH_EXPORT"),
244 #ifndef DISABLE_DEPRECATED
245 "PROPERTY_USED_AS_FUNCTION",
246 "CONSTANT_USED_AS_FUNCTION",
247 "FUNCTION_USED_AS_PROPERTY",
248 #endif // DISABLE_DEPRECATED
249 };
250
251 static_assert(std_size(names) == WARNING_MAX, "Amount of warning types don't match the amount of warning names.");
252
253 return names[(int)p_code];
254 }
255
256 String GDScriptWarning::get_setting_path_from_code(Code p_code) {
257 return "debug/gdscript/warnings/" + get_name_from_code(p_code).to_lower();
258 }
259
260 GDScriptWarning::Code GDScriptWarning::get_code_from_name(const String &p_name) {
261 for (int i = 0; i < WARNING_MAX; i++) {
262 if (get_name_from_code((Code)i) == p_name) {
263 return (Code)i;
264 }
265 }
266
267 return WARNING_MAX;
268 }
269
270 #endif // DEBUG_ENABLED