2
0

gdscript_warning.cpp 13 KB

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