gdscript_warning.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*************************************************************************/
  2. /* gdscript_warning.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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 VOID_ASSIGNMENT: {
  79. CHECK_SYMBOLS(1);
  80. return "Assignment operation, but the function '" + symbols[0] + "()' returns void.";
  81. } break;
  82. case NARROWING_CONVERSION: {
  83. return "Narrowing conversion (float is converted to int and loses precision).";
  84. } break;
  85. case INCOMPATIBLE_TERNARY: {
  86. return "Values of the ternary conditional are not mutually compatible.";
  87. } break;
  88. case UNUSED_SIGNAL: {
  89. CHECK_SYMBOLS(1);
  90. return "The signal '" + symbols[0] + "' is declared but never emitted.";
  91. } break;
  92. case RETURN_VALUE_DISCARDED: {
  93. CHECK_SYMBOLS(1);
  94. return "The function '" + symbols[0] + "()' returns a value that will be discarded if not used.";
  95. } break;
  96. case PROPERTY_USED_AS_FUNCTION: {
  97. CHECK_SYMBOLS(2);
  98. 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?";
  99. } break;
  100. case CONSTANT_USED_AS_FUNCTION: {
  101. CHECK_SYMBOLS(2);
  102. 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?";
  103. } break;
  104. case FUNCTION_USED_AS_PROPERTY: {
  105. CHECK_SYMBOLS(2);
  106. 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?";
  107. } break;
  108. case INTEGER_DIVISION: {
  109. return "Integer division, decimal part will be discarded.";
  110. } break;
  111. case UNSAFE_PROPERTY_ACCESS: {
  112. CHECK_SYMBOLS(2);
  113. return "The property '" + symbols[0] + "' is not present on the inferred type '" + symbols[1] + "' (but may be present on a subtype).";
  114. } break;
  115. case UNSAFE_METHOD_ACCESS: {
  116. CHECK_SYMBOLS(2);
  117. return "The method '" + symbols[0] + "' is not present on the inferred type '" + symbols[1] + "' (but may be present on a subtype).";
  118. } break;
  119. case UNSAFE_CAST: {
  120. CHECK_SYMBOLS(1);
  121. return "The value is cast to '" + symbols[0] + "' but has an unknown type.";
  122. } break;
  123. case UNSAFE_CALL_ARGUMENT: {
  124. CHECK_SYMBOLS(4);
  125. return "The argument '" + symbols[0] + "' of the function '" + symbols[1] + "' requires a the subtype '" + symbols[2] + "' but the supertype '" + symbols[3] + "' was provided";
  126. } break;
  127. case DEPRECATED_KEYWORD: {
  128. CHECK_SYMBOLS(2);
  129. return "The '" + symbols[0] + "' keyword is deprecated and will be removed in a future release, please replace its uses by '" + symbols[1] + "'.";
  130. } break;
  131. case STANDALONE_TERNARY: {
  132. return "Standalone ternary conditional operator: the return value is being discarded.";
  133. }
  134. case ASSERT_ALWAYS_TRUE: {
  135. return "Assert statement is redundant because the expression is always true.";
  136. }
  137. case ASSERT_ALWAYS_FALSE: {
  138. return "Assert statement will raise an error because the expression is always false.";
  139. }
  140. case REDUNDANT_AWAIT: {
  141. return R"("await" keyword not needed in this case, because the expression isn't a coroutine nor a signal.)";
  142. }
  143. case EMPTY_FILE: {
  144. return "Empty script file.";
  145. }
  146. case SHADOWED_GLOBAL_IDENTIFIER: {
  147. CHECK_SYMBOLS(3);
  148. return vformat(R"(The %s '%s' has the same name as a %s.)", symbols[0], symbols[1], symbols[2]);
  149. }
  150. case INT_ASSIGNED_TO_ENUM: {
  151. return "Integer used when an enum value is expected. If this is intended cast the integer to the enum type.";
  152. }
  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 WARNING_MAX:
  158. break; // Can't happen, but silences warning
  159. }
  160. ERR_FAIL_V_MSG(String(), "Invalid GDScript warning code: " + get_name_from_code(code) + ".");
  161. #undef CHECK_SYMBOLS
  162. }
  163. int GDScriptWarning::get_default_value(Code p_code) {
  164. if (get_name_from_code(p_code).to_lower().begins_with("unsafe_")) {
  165. return WarnLevel::IGNORE;
  166. }
  167. return WarnLevel::WARN;
  168. }
  169. PropertyInfo GDScriptWarning::get_property_info(Code p_code) {
  170. // Making this a separate function in case a warning needs different PropertyInfo in the future.
  171. return PropertyInfo(Variant::INT, get_settings_path_from_code(p_code), PROPERTY_HINT_ENUM, "Ignore,Warn,Error");
  172. }
  173. String GDScriptWarning::get_name() const {
  174. return get_name_from_code(code);
  175. }
  176. String GDScriptWarning::get_name_from_code(Code p_code) {
  177. ERR_FAIL_COND_V(p_code < 0 || p_code >= WARNING_MAX, String());
  178. static const char *names[] = {
  179. "UNASSIGNED_VARIABLE",
  180. "UNASSIGNED_VARIABLE_OP_ASSIGN",
  181. "UNUSED_VARIABLE",
  182. "UNUSED_LOCAL_CONSTANT",
  183. "SHADOWED_VARIABLE",
  184. "SHADOWED_VARIABLE_BASE_CLASS",
  185. "UNUSED_PRIVATE_CLASS_VARIABLE",
  186. "UNUSED_PARAMETER",
  187. "UNREACHABLE_CODE",
  188. "UNREACHABLE_PATTERN",
  189. "STANDALONE_EXPRESSION",
  190. "VOID_ASSIGNMENT",
  191. "NARROWING_CONVERSION",
  192. "INCOMPATIBLE_TERNARY",
  193. "UNUSED_SIGNAL",
  194. "RETURN_VALUE_DISCARDED",
  195. "PROPERTY_USED_AS_FUNCTION",
  196. "CONSTANT_USED_AS_FUNCTION",
  197. "FUNCTION_USED_AS_PROPERTY",
  198. "INTEGER_DIVISION",
  199. "UNSAFE_PROPERTY_ACCESS",
  200. "UNSAFE_METHOD_ACCESS",
  201. "UNSAFE_CAST",
  202. "UNSAFE_CALL_ARGUMENT",
  203. "DEPRECATED_KEYWORD",
  204. "STANDALONE_TERNARY",
  205. "ASSERT_ALWAYS_TRUE",
  206. "ASSERT_ALWAYS_FALSE",
  207. "REDUNDANT_AWAIT",
  208. "EMPTY_FILE",
  209. "SHADOWED_GLOBAL_IDENTIFIER",
  210. "INT_ASSIGNED_TO_ENUM",
  211. "STATIC_CALLED_ON_INSTANCE",
  212. };
  213. static_assert((sizeof(names) / sizeof(*names)) == WARNING_MAX, "Amount of warning types don't match the amount of warning names.");
  214. return names[(int)p_code];
  215. }
  216. String GDScriptWarning::get_settings_path_from_code(Code p_code) {
  217. return "debug/gdscript/warnings/" + get_name_from_code(p_code).to_lower();
  218. }
  219. GDScriptWarning::Code GDScriptWarning::get_code_from_name(const String &p_name) {
  220. for (int i = 0; i < WARNING_MAX; i++) {
  221. if (get_name_from_code((Code)i) == p_name) {
  222. return (Code)i;
  223. }
  224. }
  225. return WARNING_MAX;
  226. }
  227. #endif // DEBUG_ENABLED