Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/gdscript/gdscript_warning.cpp
10277 views
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 ASSERT_ALWAYS_TRUE:
122
return "Assert statement is redundant because the expression is always true.";
123
case ASSERT_ALWAYS_FALSE:
124
return "Assert statement will raise an error because the expression is always false.";
125
case INTEGER_DIVISION:
126
return "Integer division. Decimal part will be discarded.";
127
case NARROWING_CONVERSION:
128
return "Narrowing conversion (float is converted to int and loses precision).";
129
case INT_AS_ENUM_WITHOUT_CAST:
130
return "Integer used when an enum value is expected. If this is intended, cast the integer to the enum type using the \"as\" keyword.";
131
case INT_AS_ENUM_WITHOUT_MATCH:
132
CHECK_SYMBOLS(3);
133
return vformat(R"(Cannot %s %s as Enum "%s": no enum member has matching value.)", symbols[0], symbols[1], symbols[2]);
134
case ENUM_VARIABLE_WITHOUT_DEFAULT:
135
CHECK_SYMBOLS(1);
136
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]);
137
case EMPTY_FILE:
138
return "Empty script file.";
139
case DEPRECATED_KEYWORD:
140
CHECK_SYMBOLS(2);
141
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]);
142
case CONFUSABLE_IDENTIFIER:
143
CHECK_SYMBOLS(1);
144
return vformat(R"(The identifier "%s" has misleading characters and might be confused with something else.)", symbols[0]);
145
case CONFUSABLE_LOCAL_DECLARATION:
146
CHECK_SYMBOLS(2);
147
return vformat(R"(The %s "%s" is declared below in the parent block.)", symbols[0], symbols[1]);
148
case CONFUSABLE_LOCAL_USAGE:
149
CHECK_SYMBOLS(1);
150
return vformat(R"(The identifier "%s" will be shadowed below in the block.)", symbols[0]);
151
case CONFUSABLE_CAPTURE_REASSIGNMENT:
152
CHECK_SYMBOLS(1);
153
return vformat(R"(Reassigning lambda capture does not modify the outer local variable "%s".)", symbols[0]);
154
case INFERENCE_ON_VARIANT:
155
CHECK_SYMBOLS(1);
156
return vformat("The %s type is being inferred from a Variant value, so it will be typed as Variant.", symbols[0]);
157
case NATIVE_METHOD_OVERRIDE:
158
CHECK_SYMBOLS(2);
159
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]);
160
case GET_NODE_DEFAULT_WITHOUT_ONREADY:
161
CHECK_SYMBOLS(1);
162
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]);
163
case ONREADY_WITH_EXPORT:
164
return R"("@onready" will set the default value after "@export" takes effect and will override it.)";
165
#ifndef DISABLE_DEPRECATED
166
// Never produced. These warnings migrated from 3.x by mistake.
167
case PROPERTY_USED_AS_FUNCTION: // There is already an error.
168
case CONSTANT_USED_AS_FUNCTION: // There is already an error.
169
case FUNCTION_USED_AS_PROPERTY: // This is valid, returns `Callable`.
170
break;
171
#endif
172
case WARNING_MAX:
173
break; // Can't happen, but silences warning.
174
}
175
ERR_FAIL_V_MSG(String(), vformat(R"(Invalid GDScript warning "%s".)", get_name_from_code(code)));
176
177
#undef CHECK_SYMBOLS
178
}
179
180
int GDScriptWarning::get_default_value(Code p_code) {
181
ERR_FAIL_INDEX_V_MSG(p_code, WARNING_MAX, WarnLevel::IGNORE, "Getting default value of invalid warning code.");
182
return default_warning_levels[p_code];
183
}
184
185
PropertyInfo GDScriptWarning::get_property_info(Code p_code) {
186
return PropertyInfo(Variant::INT, get_settings_path_from_code(p_code), PROPERTY_HINT_ENUM, "Ignore,Warn,Error");
187
}
188
189
String GDScriptWarning::get_name() const {
190
return get_name_from_code(code);
191
}
192
193
String GDScriptWarning::get_name_from_code(Code p_code) {
194
ERR_FAIL_COND_V(p_code < 0 || p_code >= WARNING_MAX, String());
195
196
static const char *names[] = {
197
PNAME("UNASSIGNED_VARIABLE"),
198
PNAME("UNASSIGNED_VARIABLE_OP_ASSIGN"),
199
PNAME("UNUSED_VARIABLE"),
200
PNAME("UNUSED_LOCAL_CONSTANT"),
201
PNAME("UNUSED_PRIVATE_CLASS_VARIABLE"),
202
PNAME("UNUSED_PARAMETER"),
203
PNAME("UNUSED_SIGNAL"),
204
PNAME("SHADOWED_VARIABLE"),
205
PNAME("SHADOWED_VARIABLE_BASE_CLASS"),
206
PNAME("SHADOWED_GLOBAL_IDENTIFIER"),
207
PNAME("UNREACHABLE_CODE"),
208
PNAME("UNREACHABLE_PATTERN"),
209
PNAME("STANDALONE_EXPRESSION"),
210
PNAME("STANDALONE_TERNARY"),
211
PNAME("INCOMPATIBLE_TERNARY"),
212
PNAME("UNTYPED_DECLARATION"),
213
PNAME("INFERRED_DECLARATION"),
214
PNAME("UNSAFE_PROPERTY_ACCESS"),
215
PNAME("UNSAFE_METHOD_ACCESS"),
216
PNAME("UNSAFE_CAST"),
217
PNAME("UNSAFE_CALL_ARGUMENT"),
218
PNAME("UNSAFE_VOID_RETURN"),
219
PNAME("RETURN_VALUE_DISCARDED"),
220
PNAME("STATIC_CALLED_ON_INSTANCE"),
221
PNAME("MISSING_TOOL"),
222
PNAME("REDUNDANT_STATIC_UNLOAD"),
223
PNAME("REDUNDANT_AWAIT"),
224
PNAME("ASSERT_ALWAYS_TRUE"),
225
PNAME("ASSERT_ALWAYS_FALSE"),
226
PNAME("INTEGER_DIVISION"),
227
PNAME("NARROWING_CONVERSION"),
228
PNAME("INT_AS_ENUM_WITHOUT_CAST"),
229
PNAME("INT_AS_ENUM_WITHOUT_MATCH"),
230
PNAME("ENUM_VARIABLE_WITHOUT_DEFAULT"),
231
PNAME("EMPTY_FILE"),
232
PNAME("DEPRECATED_KEYWORD"),
233
PNAME("CONFUSABLE_IDENTIFIER"),
234
PNAME("CONFUSABLE_LOCAL_DECLARATION"),
235
PNAME("CONFUSABLE_LOCAL_USAGE"),
236
PNAME("CONFUSABLE_CAPTURE_REASSIGNMENT"),
237
PNAME("INFERENCE_ON_VARIANT"),
238
PNAME("NATIVE_METHOD_OVERRIDE"),
239
PNAME("GET_NODE_DEFAULT_WITHOUT_ONREADY"),
240
PNAME("ONREADY_WITH_EXPORT"),
241
#ifndef DISABLE_DEPRECATED
242
"PROPERTY_USED_AS_FUNCTION",
243
"CONSTANT_USED_AS_FUNCTION",
244
"FUNCTION_USED_AS_PROPERTY",
245
#endif
246
};
247
248
static_assert(std::size(names) == WARNING_MAX, "Amount of warning types don't match the amount of warning names.");
249
250
return names[(int)p_code];
251
}
252
253
String GDScriptWarning::get_settings_path_from_code(Code p_code) {
254
return "debug/gdscript/warnings/" + get_name_from_code(p_code).to_lower();
255
}
256
257
GDScriptWarning::Code GDScriptWarning::get_code_from_name(const String &p_name) {
258
for (int i = 0; i < WARNING_MAX; i++) {
259
if (get_name_from_code((Code)i) == p_name) {
260
return (Code)i;
261
}
262
}
263
264
return WARNING_MAX;
265
}
266
267
#endif // DEBUG_ENABLED
268
269