gdscript_warning.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*************************************************************************/
  2. /* gdscript_warning.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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, but this value is never 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 WARNING_MAX:
  144. break; // Can't happen, but silences warning
  145. }
  146. ERR_FAIL_V_MSG(String(), "Invalid GDScript warning code: " + get_name_from_code(code) + ".");
  147. #undef CHECK_SYMBOLS
  148. }
  149. String GDScriptWarning::get_name() const {
  150. return get_name_from_code(code);
  151. }
  152. String GDScriptWarning::get_name_from_code(Code p_code) {
  153. ERR_FAIL_COND_V(p_code < 0 || p_code >= WARNING_MAX, String());
  154. static const char *names[] = {
  155. "UNASSIGNED_VARIABLE",
  156. "UNASSIGNED_VARIABLE_OP_ASSIGN",
  157. "UNUSED_VARIABLE",
  158. "UNUSED_LOCAL_CONSTANT",
  159. "SHADOWED_VARIABLE",
  160. "SHADOWED_VARIABLE_BASE_CLASS",
  161. "UNUSED_PRIVATE_CLASS_VARIABLE",
  162. "UNUSED_PARAMETER",
  163. "UNREACHABLE_CODE",
  164. "UNREACHABLE_PATTERN",
  165. "STANDALONE_EXPRESSION",
  166. "VOID_ASSIGNMENT",
  167. "NARROWING_CONVERSION",
  168. "INCOMPATIBLE_TERNARY",
  169. "UNUSED_SIGNAL",
  170. "RETURN_VALUE_DISCARDED",
  171. "PROPERTY_USED_AS_FUNCTION",
  172. "CONSTANT_USED_AS_FUNCTION",
  173. "FUNCTION_USED_AS_PROPERTY",
  174. "INTEGER_DIVISION",
  175. "UNSAFE_PROPERTY_ACCESS",
  176. "UNSAFE_METHOD_ACCESS",
  177. "UNSAFE_CAST",
  178. "UNSAFE_CALL_ARGUMENT",
  179. "DEPRECATED_KEYWORD",
  180. "STANDALONE_TERNARY",
  181. "ASSERT_ALWAYS_TRUE",
  182. "ASSERT_ALWAYS_FALSE",
  183. "REDUNDANT_AWAIT",
  184. };
  185. static_assert((sizeof(names) / sizeof(*names)) == WARNING_MAX, "Amount of warning types don't match the amount of warning names.");
  186. return names[(int)p_code];
  187. }
  188. GDScriptWarning::Code GDScriptWarning::get_code_from_name(const String &p_name) {
  189. for (int i = 0; i < WARNING_MAX; i++) {
  190. if (get_name_from_code((Code)i) == p_name) {
  191. return (Code)i;
  192. }
  193. }
  194. ERR_FAIL_V_MSG(WARNING_MAX, "Invalid GDScript warning name: " + p_name);
  195. }
  196. #endif // DEBUG_ENABLED