gdscript_warning.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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_OP_ASSIGN: {
  37. CHECK_SYMBOLS(1);
  38. return "Using assignment with operation but the variable '" + symbols[0] + "' was not previously assigned a value.";
  39. } break;
  40. case UNASSIGNED_VARIABLE: {
  41. CHECK_SYMBOLS(1);
  42. return "The variable '" + symbols[0] + "' was used but never assigned a value.";
  43. } break;
  44. case UNUSED_VARIABLE: {
  45. CHECK_SYMBOLS(1);
  46. return "The local variable '" + symbols[0] + "' is declared but never used in the block. If this is intended, prefix it with an underscore: '_" + symbols[0] + "'";
  47. } break;
  48. case UNUSED_LOCAL_CONSTANT: {
  49. CHECK_SYMBOLS(1);
  50. return "The local constant '" + symbols[0] + "' is declared but never used in the block. If this is intended, prefix it with an underscore: '_" + symbols[0] + "'";
  51. } break;
  52. case SHADOWED_VARIABLE: {
  53. CHECK_SYMBOLS(4);
  54. return vformat(R"(The local %s "%s" is shadowing an already-declared %s at line %s.)", symbols[0], symbols[1], symbols[2], symbols[3]);
  55. } break;
  56. case SHADOWED_VARIABLE_BASE_CLASS: {
  57. CHECK_SYMBOLS(4);
  58. return vformat(R"(The local %s "%s" is shadowing an already-declared %s at the base class "%s".)", symbols[0], symbols[1], symbols[2], symbols[3]);
  59. } break;
  60. case UNUSED_PRIVATE_CLASS_VARIABLE: {
  61. CHECK_SYMBOLS(1);
  62. return "The class variable '" + symbols[0] + "' is declared but never used in the script.";
  63. } break;
  64. case UNUSED_PARAMETER: {
  65. CHECK_SYMBOLS(2);
  66. return "The parameter '" + symbols[1] + "' is never used in the function '" + symbols[0] + "'. If this is intended, prefix it with an underscore: '_" + symbols[1] + "'";
  67. } break;
  68. case UNREACHABLE_CODE: {
  69. CHECK_SYMBOLS(1);
  70. return "Unreachable code (statement after return) in function '" + symbols[0] + "()'.";
  71. } break;
  72. case UNREACHABLE_PATTERN: {
  73. return "Unreachable pattern (pattern after wildcard or bind).";
  74. } break;
  75. case STANDALONE_EXPRESSION: {
  76. return "Standalone expression (the line has no effect).";
  77. } break;
  78. case NARROWING_CONVERSION: {
  79. return "Narrowing conversion (float is converted to int and loses precision).";
  80. } break;
  81. case INCOMPATIBLE_TERNARY: {
  82. return "Values of the ternary conditional are not mutually compatible.";
  83. } break;
  84. case UNUSED_SIGNAL: {
  85. CHECK_SYMBOLS(1);
  86. return "The signal '" + symbols[0] + "' is declared but never emitted.";
  87. } break;
  88. case RETURN_VALUE_DISCARDED: {
  89. CHECK_SYMBOLS(1);
  90. return "The function '" + symbols[0] + "()' returns a value that will be discarded if not used.";
  91. } break;
  92. case PROPERTY_USED_AS_FUNCTION: {
  93. CHECK_SYMBOLS(2);
  94. return "The method '" + symbols[0] + "()' was not found in base '" + symbols[1] + "' but there's a property with the same name. Did you mean to access it?";
  95. } break;
  96. case CONSTANT_USED_AS_FUNCTION: {
  97. CHECK_SYMBOLS(2);
  98. return "The method '" + symbols[0] + "()' was not found in base '" + symbols[1] + "' but there's a constant with the same name. Did you mean to access it?";
  99. } break;
  100. case FUNCTION_USED_AS_PROPERTY: {
  101. CHECK_SYMBOLS(2);
  102. return "The property '" + symbols[0] + "' was not found in base '" + symbols[1] + "' but there's a method with the same name. Did you mean to call it?";
  103. } break;
  104. case INTEGER_DIVISION: {
  105. return "Integer division, decimal part will be discarded.";
  106. } break;
  107. case UNSAFE_PROPERTY_ACCESS: {
  108. CHECK_SYMBOLS(2);
  109. return "The property '" + symbols[0] + "' is not present on the inferred type '" + symbols[1] + "' (but may be present on a subtype).";
  110. } break;
  111. case UNSAFE_METHOD_ACCESS: {
  112. CHECK_SYMBOLS(2);
  113. return "The method '" + symbols[0] + "' is not present on the inferred type '" + symbols[1] + "' (but may be present on a subtype).";
  114. } break;
  115. case UNSAFE_CAST: {
  116. CHECK_SYMBOLS(1);
  117. return "The value is cast to '" + symbols[0] + "' but has an unknown type.";
  118. } break;
  119. case UNSAFE_CALL_ARGUMENT: {
  120. CHECK_SYMBOLS(4);
  121. return "The argument '" + symbols[0] + "' of the function '" + symbols[1] + "' requires a the subtype '" + symbols[2] + "' but the supertype '" + symbols[3] + "' was provided";
  122. } break;
  123. case DEPRECATED_KEYWORD: {
  124. CHECK_SYMBOLS(2);
  125. return "The '" + symbols[0] + "' keyword is deprecated and will be removed in a future release, please replace its uses by '" + symbols[1] + "'.";
  126. } break;
  127. case STANDALONE_TERNARY: {
  128. return "Standalone ternary conditional operator: the return value is being discarded.";
  129. }
  130. case ASSERT_ALWAYS_TRUE: {
  131. return "Assert statement is redundant because the expression is always true.";
  132. }
  133. case ASSERT_ALWAYS_FALSE: {
  134. return "Assert statement will raise an error because the expression is always false.";
  135. }
  136. case REDUNDANT_AWAIT: {
  137. return R"("await" keyword not needed in this case, because the expression isn't a coroutine nor a signal.)";
  138. }
  139. case EMPTY_FILE: {
  140. return "Empty script file.";
  141. }
  142. case SHADOWED_GLOBAL_IDENTIFIER: {
  143. CHECK_SYMBOLS(3);
  144. return vformat(R"(The %s '%s' has the same name as a %s.)", symbols[0], symbols[1], symbols[2]);
  145. }
  146. case INT_AS_ENUM_WITHOUT_CAST: {
  147. return "Integer used when an enum value is expected. If this is intended cast the integer to the enum type.";
  148. }
  149. case INT_AS_ENUM_WITHOUT_MATCH: {
  150. CHECK_SYMBOLS(3);
  151. return vformat(R"(Cannot %s %s as Enum "%s": no enum member has matching value.)", symbols[0], symbols[1], symbols[2]);
  152. } break;
  153. case STATIC_CALLED_ON_INSTANCE: {
  154. CHECK_SYMBOLS(2);
  155. 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]);
  156. }
  157. case CONFUSABLE_IDENTIFIER: {
  158. CHECK_SYMBOLS(1);
  159. return vformat(R"(The identifier "%s" has misleading characters and might be confused with something else.)", symbols[0]);
  160. }
  161. case RENAMED_IN_GD4_HINT: {
  162. break; // Renamed identifier hint is taken care of by the GDScriptAnalyzer. No message needed here.
  163. }
  164. case WARNING_MAX:
  165. break; // Can't happen, but silences warning
  166. }
  167. ERR_FAIL_V_MSG(String(), "Invalid GDScript warning code: " + get_name_from_code(code) + ".");
  168. #undef CHECK_SYMBOLS
  169. }
  170. int GDScriptWarning::get_default_value(Code p_code) {
  171. if (get_name_from_code(p_code).to_lower().begins_with("unsafe_")) {
  172. return WarnLevel::IGNORE;
  173. }
  174. // Too spammy by default on common cases (connect, Tween, etc.).
  175. if (p_code == RETURN_VALUE_DISCARDED) {
  176. return WarnLevel::IGNORE;
  177. }
  178. return WarnLevel::WARN;
  179. }
  180. PropertyInfo GDScriptWarning::get_property_info(Code p_code) {
  181. // Making this a separate function in case a warning needs different PropertyInfo in the future.
  182. if (p_code == Code::RENAMED_IN_GD4_HINT) {
  183. return PropertyInfo(Variant::BOOL, get_settings_path_from_code(p_code));
  184. }
  185. return PropertyInfo(Variant::INT, get_settings_path_from_code(p_code), PROPERTY_HINT_ENUM, "Ignore,Warn,Error");
  186. }
  187. String GDScriptWarning::get_name() const {
  188. return get_name_from_code(code);
  189. }
  190. String GDScriptWarning::get_name_from_code(Code p_code) {
  191. ERR_FAIL_COND_V(p_code < 0 || p_code >= WARNING_MAX, String());
  192. static const char *names[] = {
  193. "UNASSIGNED_VARIABLE",
  194. "UNASSIGNED_VARIABLE_OP_ASSIGN",
  195. "UNUSED_VARIABLE",
  196. "UNUSED_LOCAL_CONSTANT",
  197. "SHADOWED_VARIABLE",
  198. "SHADOWED_VARIABLE_BASE_CLASS",
  199. "UNUSED_PRIVATE_CLASS_VARIABLE",
  200. "UNUSED_PARAMETER",
  201. "UNREACHABLE_CODE",
  202. "UNREACHABLE_PATTERN",
  203. "STANDALONE_EXPRESSION",
  204. "NARROWING_CONVERSION",
  205. "INCOMPATIBLE_TERNARY",
  206. "UNUSED_SIGNAL",
  207. "RETURN_VALUE_DISCARDED",
  208. "PROPERTY_USED_AS_FUNCTION",
  209. "CONSTANT_USED_AS_FUNCTION",
  210. "FUNCTION_USED_AS_PROPERTY",
  211. "INTEGER_DIVISION",
  212. "UNSAFE_PROPERTY_ACCESS",
  213. "UNSAFE_METHOD_ACCESS",
  214. "UNSAFE_CAST",
  215. "UNSAFE_CALL_ARGUMENT",
  216. "DEPRECATED_KEYWORD",
  217. "STANDALONE_TERNARY",
  218. "ASSERT_ALWAYS_TRUE",
  219. "ASSERT_ALWAYS_FALSE",
  220. "REDUNDANT_AWAIT",
  221. "EMPTY_FILE",
  222. "SHADOWED_GLOBAL_IDENTIFIER",
  223. "INT_AS_ENUM_WITHOUT_CAST",
  224. "INT_AS_ENUM_WITHOUT_MATCH",
  225. "STATIC_CALLED_ON_INSTANCE",
  226. "CONFUSABLE_IDENTIFIER",
  227. "RENAMED_IN_GODOT_4_HINT"
  228. };
  229. static_assert((sizeof(names) / sizeof(*names)) == WARNING_MAX, "Amount of warning types don't match the amount of warning names.");
  230. return names[(int)p_code];
  231. }
  232. String GDScriptWarning::get_settings_path_from_code(Code p_code) {
  233. return "debug/gdscript/warnings/" + get_name_from_code(p_code).to_lower();
  234. }
  235. GDScriptWarning::Code GDScriptWarning::get_code_from_name(const String &p_name) {
  236. for (int i = 0; i < WARNING_MAX; i++) {
  237. if (get_name_from_code((Code)i) == p_name) {
  238. return (Code)i;
  239. }
  240. }
  241. return WARNING_MAX;
  242. }
  243. #endif // DEBUG_ENABLED