gdscript_warning.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 file.
  49. UNUSED_PARAMETER, // Function parameter is never used.
  50. UNUSED_SIGNAL, // Signal is defined but never emitted.
  51. SHADOWED_VARIABLE, // Variable name shadowed by other variable in same class.
  52. SHADOWED_VARIABLE_BASE_CLASS, // Variable name shadowed by other variable in some base class.
  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. PROPERTY_USED_AS_FUNCTION, // Function not found, but there's a property with the same name.
  60. CONSTANT_USED_AS_FUNCTION, // Function not found, but there's a constant with the same name.
  61. FUNCTION_USED_AS_PROPERTY, // Property not found, but there's a function with the same name.
  62. UNTYPED_DECLARATION, // Variable/parameter/function has no static type, explicitly specified or implicitly inferred.
  63. INFERRED_DECLARATION, // Variable/constant/parameter has an implicitly inferred static type.
  64. UNSAFE_PROPERTY_ACCESS, // Property not found in the detected type (but can be in subtypes).
  65. UNSAFE_METHOD_ACCESS, // Function not found in the detected type (but can be in subtypes).
  66. UNSAFE_CAST, // Cast used in an unknown type.
  67. UNSAFE_CALL_ARGUMENT, // Function call argument is of a supertype of the required type.
  68. UNSAFE_VOID_RETURN, // Function returns void but returned a call to a function that can't be type checked.
  69. RETURN_VALUE_DISCARDED, // Function call returns something but the value isn't used.
  70. STATIC_CALLED_ON_INSTANCE, // A static method was called on an instance of a class instead of on the class itself.
  71. REDUNDANT_STATIC_UNLOAD, // The `@static_unload` annotation is used but the class does not have static data.
  72. REDUNDANT_AWAIT, // await is used but expression is synchronous (not a signal nor a coroutine).
  73. ASSERT_ALWAYS_TRUE, // Expression for assert argument is always true.
  74. ASSERT_ALWAYS_FALSE, // Expression for assert argument is always false.
  75. INTEGER_DIVISION, // Integer divide by integer, decimal part is discarded.
  76. NARROWING_CONVERSION, // Float value into an integer slot, precision is lost.
  77. INT_AS_ENUM_WITHOUT_CAST, // An integer value was used as an enum value without casting.
  78. INT_AS_ENUM_WITHOUT_MATCH, // An integer value was used as an enum value without matching enum member.
  79. EMPTY_FILE, // A script file is empty.
  80. DEPRECATED_KEYWORD, // The keyword is deprecated and should be replaced.
  81. RENAMED_IN_GODOT_4_HINT, // A variable or function that could not be found has been renamed in Godot 4.
  82. CONFUSABLE_IDENTIFIER, // The identifier contains misleading characters that can be confused. E.g. "usеr" (has Cyrillic "е" instead of Latin "e").
  83. CONFUSABLE_LOCAL_DECLARATION, // The parent block declares an identifier with the same name below.
  84. CONFUSABLE_LOCAL_USAGE, // The identifier will be shadowed below in the block.
  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. WARNING_MAX,
  90. };
  91. constexpr static WarnLevel default_warning_levels[] = {
  92. WARN, // UNASSIGNED_VARIABLE
  93. WARN, // UNASSIGNED_VARIABLE_OP_ASSIGN
  94. WARN, // UNUSED_VARIABLE
  95. WARN, // UNUSED_LOCAL_CONSTANT
  96. WARN, // UNUSED_PRIVATE_CLASS_VARIABLE
  97. WARN, // UNUSED_PARAMETER
  98. WARN, // UNUSED_SIGNAL
  99. WARN, // SHADOWED_VARIABLE
  100. WARN, // SHADOWED_VARIABLE_BASE_CLASS
  101. WARN, // SHADOWED_GLOBAL_IDENTIFIER
  102. WARN, // UNREACHABLE_CODE
  103. WARN, // UNREACHABLE_PATTERN
  104. WARN, // STANDALONE_EXPRESSION
  105. WARN, // STANDALONE_TERNARY
  106. WARN, // INCOMPATIBLE_TERNARY
  107. WARN, // PROPERTY_USED_AS_FUNCTION
  108. WARN, // CONSTANT_USED_AS_FUNCTION
  109. WARN, // FUNCTION_USED_AS_PROPERTY
  110. IGNORE, // UNTYPED_DECLARATION // Static typing is optional, we don't want to spam warnings.
  111. IGNORE, // INFERRED_DECLARATION // Static typing is optional, we don't want to spam warnings.
  112. IGNORE, // UNSAFE_PROPERTY_ACCESS // Too common in untyped scenarios.
  113. IGNORE, // UNSAFE_METHOD_ACCESS // Too common in untyped scenarios.
  114. IGNORE, // UNSAFE_CAST // Too common in untyped scenarios.
  115. IGNORE, // UNSAFE_CALL_ARGUMENT // Too common in untyped scenarios.
  116. WARN, // UNSAFE_VOID_RETURN
  117. IGNORE, // RETURN_VALUE_DISCARDED // Too spammy by default on common cases (connect, Tween, etc.).
  118. WARN, // STATIC_CALLED_ON_INSTANCE
  119. WARN, // REDUNDANT_STATIC_UNLOAD
  120. WARN, // REDUNDANT_AWAIT
  121. WARN, // ASSERT_ALWAYS_TRUE
  122. WARN, // ASSERT_ALWAYS_FALSE
  123. WARN, // INTEGER_DIVISION
  124. WARN, // NARROWING_CONVERSION
  125. WARN, // INT_AS_ENUM_WITHOUT_CAST
  126. WARN, // INT_AS_ENUM_WITHOUT_MATCH
  127. WARN, // EMPTY_FILE
  128. WARN, // DEPRECATED_KEYWORD
  129. WARN, // RENAMED_IN_GODOT_4_HINT
  130. WARN, // CONFUSABLE_IDENTIFIER
  131. WARN, // CONFUSABLE_LOCAL_DECLARATION
  132. WARN, // CONFUSABLE_LOCAL_USAGE
  133. ERROR, // INFERENCE_ON_VARIANT // Most likely done by accident, usually inference is trying for a particular type.
  134. ERROR, // NATIVE_METHOD_OVERRIDE // May not work as expected.
  135. ERROR, // GET_NODE_DEFAULT_WITHOUT_ONREADY // May not work as expected.
  136. ERROR, // ONREADY_WITH_EXPORT // May not work as expected.
  137. };
  138. static_assert((sizeof(default_warning_levels) / sizeof(default_warning_levels[0])) == WARNING_MAX, "Amount of default levels does not match the amount of warnings.");
  139. Code code = WARNING_MAX;
  140. int start_line = -1, end_line = -1;
  141. int leftmost_column = -1, rightmost_column = -1;
  142. Vector<String> symbols;
  143. String get_name() const;
  144. String get_message() const;
  145. static int get_default_value(Code p_code);
  146. static PropertyInfo get_property_info(Code p_code);
  147. static String get_name_from_code(Code p_code);
  148. static String get_settings_path_from_code(Code p_code);
  149. static Code get_code_from_name(const String &p_name);
  150. };
  151. #endif // DEBUG_ENABLED
  152. #endif // GDSCRIPT_WARNING_H