gdscript_warning.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /**************************************************************************/
  2. /* gdscript_warning.h */
  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. #ifndef GDSCRIPT_WARNING_H
  31. #define GDSCRIPT_WARNING_H
  32. #ifdef DEBUG_ENABLED
  33. #include "core/object/object.h"
  34. #include "core/string/ustring.h"
  35. #include "core/templates/vector.h"
  36. class GDScriptWarning {
  37. public:
  38. enum WarnLevel {
  39. IGNORE,
  40. WARN,
  41. ERROR
  42. };
  43. enum Code {
  44. UNASSIGNED_VARIABLE, // Variable used but never assigned.
  45. UNASSIGNED_VARIABLE_OP_ASSIGN, // Variable never assigned but used in an assignment operation (+=, *=, etc).
  46. UNUSED_VARIABLE, // Local variable is declared but never used.
  47. UNUSED_LOCAL_CONSTANT, // Local constant is declared but never used.
  48. UNUSED_PRIVATE_CLASS_VARIABLE, // Class variable is declared private ("_" prefix) but never used in the class.
  49. UNUSED_PARAMETER, // Function parameter is never used.
  50. UNUSED_SIGNAL, // Signal is defined but never explicitly used in the class.
  51. SHADOWED_VARIABLE, // A local variable/constant shadows a current class member.
  52. SHADOWED_VARIABLE_BASE_CLASS, // A local variable/constant shadows a base class member.
  53. SHADOWED_GLOBAL_IDENTIFIER, // A global class or function has the same name as variable.
  54. UNREACHABLE_CODE, // Code after a return statement.
  55. UNREACHABLE_PATTERN, // Pattern in a match statement after a catch all pattern (wildcard or bind).
  56. STANDALONE_EXPRESSION, // Expression not assigned to a variable.
  57. STANDALONE_TERNARY, // Return value of ternary expression is discarded.
  58. INCOMPATIBLE_TERNARY, // Possible values of a ternary if are not mutually compatible.
  59. UNTYPED_DECLARATION, // Variable/parameter/function has no static type, explicitly specified or implicitly inferred.
  60. INFERRED_DECLARATION, // Variable/constant/parameter has an implicitly inferred static type.
  61. UNSAFE_PROPERTY_ACCESS, // Property not found in the detected type (but can be in subtypes).
  62. UNSAFE_METHOD_ACCESS, // Function not found in the detected type (but can be in subtypes).
  63. UNSAFE_CAST, // Casting a `Variant` value to non-`Variant`.
  64. UNSAFE_CALL_ARGUMENT, // Function call argument is of a supertype of the required type.
  65. UNSAFE_VOID_RETURN, // Function returns void but returned a call to a function that can't be type checked.
  66. RETURN_VALUE_DISCARDED, // Function call returns something but the value isn't used.
  67. STATIC_CALLED_ON_INSTANCE, // A static method was called on an instance of a class instead of on the class itself.
  68. MISSING_TOOL, // The base class script has the "@tool" annotation, but this script does not have it.
  69. REDUNDANT_STATIC_UNLOAD, // The `@static_unload` annotation is used but the class does not have static data.
  70. REDUNDANT_AWAIT, // await is used but expression is synchronous (not a signal nor a coroutine).
  71. ASSERT_ALWAYS_TRUE, // Expression for assert argument is always true.
  72. ASSERT_ALWAYS_FALSE, // Expression for assert argument is always false.
  73. INTEGER_DIVISION, // Integer divide by integer, decimal part is discarded.
  74. NARROWING_CONVERSION, // Float value into an integer slot, precision is lost.
  75. INT_AS_ENUM_WITHOUT_CAST, // An integer value was used as an enum value without casting.
  76. INT_AS_ENUM_WITHOUT_MATCH, // An integer value was used as an enum value without matching enum member.
  77. ENUM_VARIABLE_WITHOUT_DEFAULT, // A variable with an enum type does not have a default value. The default will be set to `0` instead of the first enum value.
  78. EMPTY_FILE, // A script file is empty.
  79. DEPRECATED_KEYWORD, // The keyword is deprecated and should be replaced.
  80. RENAMED_IN_GODOT_4_HINT, // A variable or function that could not be found has been renamed in Godot 4.
  81. CONFUSABLE_IDENTIFIER, // The identifier contains misleading characters that can be confused. E.g. "usеr" (has Cyrillic "е" instead of Latin "e").
  82. CONFUSABLE_LOCAL_DECLARATION, // The parent block declares an identifier with the same name below.
  83. CONFUSABLE_LOCAL_USAGE, // The identifier will be shadowed below in the block.
  84. CONFUSABLE_CAPTURE_REASSIGNMENT, // Reassigning lambda capture does not modify the outer local variable.
  85. INFERENCE_ON_VARIANT, // The declaration uses type inference but the value is typed as Variant.
  86. NATIVE_METHOD_OVERRIDE, // The script method overrides a native one, this may not work as intended.
  87. GET_NODE_DEFAULT_WITHOUT_ONREADY, // A class variable uses `get_node()` (or the `$` notation) as its default value, but does not use the @onready annotation.
  88. ONREADY_WITH_EXPORT, // The `@onready` annotation will set the value after `@export` which is likely not intended.
  89. #ifndef DISABLE_DEPRECATED
  90. PROPERTY_USED_AS_FUNCTION, // Function not found, but there's a property with the same name.
  91. CONSTANT_USED_AS_FUNCTION, // Function not found, but there's a constant with the same name.
  92. FUNCTION_USED_AS_PROPERTY, // Property not found, but there's a function with the same name.
  93. #endif
  94. WARNING_MAX,
  95. };
  96. constexpr static WarnLevel default_warning_levels[] = {
  97. WARN, // UNASSIGNED_VARIABLE
  98. WARN, // UNASSIGNED_VARIABLE_OP_ASSIGN
  99. WARN, // UNUSED_VARIABLE
  100. WARN, // UNUSED_LOCAL_CONSTANT
  101. WARN, // UNUSED_PRIVATE_CLASS_VARIABLE
  102. WARN, // UNUSED_PARAMETER
  103. WARN, // UNUSED_SIGNAL
  104. WARN, // SHADOWED_VARIABLE
  105. WARN, // SHADOWED_VARIABLE_BASE_CLASS
  106. WARN, // SHADOWED_GLOBAL_IDENTIFIER
  107. WARN, // UNREACHABLE_CODE
  108. WARN, // UNREACHABLE_PATTERN
  109. WARN, // STANDALONE_EXPRESSION
  110. WARN, // STANDALONE_TERNARY
  111. WARN, // INCOMPATIBLE_TERNARY
  112. IGNORE, // UNTYPED_DECLARATION // Static typing is optional, we don't want to spam warnings.
  113. IGNORE, // INFERRED_DECLARATION // Static typing is optional, we don't want to spam warnings.
  114. IGNORE, // UNSAFE_PROPERTY_ACCESS // Too common in untyped scenarios.
  115. IGNORE, // UNSAFE_METHOD_ACCESS // Too common in untyped scenarios.
  116. IGNORE, // UNSAFE_CAST // Too common in untyped scenarios.
  117. IGNORE, // UNSAFE_CALL_ARGUMENT // Too common in untyped scenarios.
  118. WARN, // UNSAFE_VOID_RETURN
  119. IGNORE, // RETURN_VALUE_DISCARDED // Too spammy by default on common cases (connect, Tween, etc.).
  120. WARN, // STATIC_CALLED_ON_INSTANCE
  121. WARN, // MISSING_TOOL
  122. WARN, // REDUNDANT_STATIC_UNLOAD
  123. WARN, // REDUNDANT_AWAIT
  124. WARN, // ASSERT_ALWAYS_TRUE
  125. WARN, // ASSERT_ALWAYS_FALSE
  126. WARN, // INTEGER_DIVISION
  127. WARN, // NARROWING_CONVERSION
  128. WARN, // INT_AS_ENUM_WITHOUT_CAST
  129. WARN, // INT_AS_ENUM_WITHOUT_MATCH
  130. WARN, // ENUM_VARIABLE_WITHOUT_DEFAULT
  131. WARN, // EMPTY_FILE
  132. WARN, // DEPRECATED_KEYWORD
  133. WARN, // RENAMED_IN_GODOT_4_HINT
  134. WARN, // CONFUSABLE_IDENTIFIER
  135. WARN, // CONFUSABLE_LOCAL_DECLARATION
  136. WARN, // CONFUSABLE_LOCAL_USAGE
  137. WARN, // CONFUSABLE_CAPTURE_REASSIGNMENT
  138. ERROR, // INFERENCE_ON_VARIANT // Most likely done by accident, usually inference is trying for a particular type.
  139. ERROR, // NATIVE_METHOD_OVERRIDE // May not work as expected.
  140. ERROR, // GET_NODE_DEFAULT_WITHOUT_ONREADY // May not work as expected.
  141. ERROR, // ONREADY_WITH_EXPORT // May not work as expected.
  142. #ifndef DISABLE_DEPRECATED
  143. WARN, // PROPERTY_USED_AS_FUNCTION
  144. WARN, // CONSTANT_USED_AS_FUNCTION
  145. WARN, // FUNCTION_USED_AS_PROPERTY
  146. #endif
  147. };
  148. static_assert((sizeof(default_warning_levels) / sizeof(default_warning_levels[0])) == WARNING_MAX, "Amount of default levels does not match the amount of warnings.");
  149. Code code = WARNING_MAX;
  150. int start_line = -1, end_line = -1;
  151. int leftmost_column = -1, rightmost_column = -1;
  152. Vector<String> symbols;
  153. String get_name() const;
  154. String get_message() const;
  155. static int get_default_value(Code p_code);
  156. static PropertyInfo get_property_info(Code p_code);
  157. static String get_name_from_code(Code p_code);
  158. static String get_settings_path_from_code(Code p_code);
  159. static Code get_code_from_name(const String &p_name);
  160. };
  161. #endif // DEBUG_ENABLED
  162. #endif // GDSCRIPT_WARNING_H