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