Path: blob/master/modules/gdscript/gdscript_warning.h
11351 views
/**************************************************************************/1/* gdscript_warning.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132#ifdef DEBUG_ENABLED3334#include "core/object/object.h"35#include "core/string/ustring.h"36#include "core/templates/vector.h"3738class GDScriptWarning {39public:40enum WarnLevel {41IGNORE,42WARN,43ERROR44};4546enum Code {47UNASSIGNED_VARIABLE, // Variable used but never assigned.48UNASSIGNED_VARIABLE_OP_ASSIGN, // Variable never assigned but used in an assignment operation (+=, *=, etc).49UNUSED_VARIABLE, // Local variable is declared but never used.50UNUSED_LOCAL_CONSTANT, // Local constant is declared but never used.51UNUSED_PRIVATE_CLASS_VARIABLE, // Class variable is declared private ("_" prefix) but never used in the class.52UNUSED_PARAMETER, // Function parameter is never used.53UNUSED_SIGNAL, // Signal is defined but never explicitly used in the class.54SHADOWED_VARIABLE, // A local variable/constant shadows a current class member.55SHADOWED_VARIABLE_BASE_CLASS, // A local variable/constant shadows a base class member.56SHADOWED_GLOBAL_IDENTIFIER, // A global class or function has the same name as variable.57UNREACHABLE_CODE, // Code after a return statement.58UNREACHABLE_PATTERN, // Pattern in a match statement after a catch all pattern (wildcard or bind).59STANDALONE_EXPRESSION, // Expression not assigned to a variable.60STANDALONE_TERNARY, // Return value of ternary expression is discarded.61INCOMPATIBLE_TERNARY, // Possible values of a ternary if are not mutually compatible.62UNTYPED_DECLARATION, // Variable/parameter/function has no static type, explicitly specified or implicitly inferred.63INFERRED_DECLARATION, // Variable/constant/parameter has an implicitly inferred static type.64UNSAFE_PROPERTY_ACCESS, // Property not found in the detected type (but can be in subtypes).65UNSAFE_METHOD_ACCESS, // Function not found in the detected type (but can be in subtypes).66UNSAFE_CAST, // Casting a `Variant` value to non-`Variant`.67UNSAFE_CALL_ARGUMENT, // Function call argument is of a supertype of the required type.68UNSAFE_VOID_RETURN, // Function returns void but returned a call to a function that can't be type checked.69RETURN_VALUE_DISCARDED, // Function call returns something but the value isn't used.70STATIC_CALLED_ON_INSTANCE, // A static method was called on an instance of a class instead of on the class itself.71MISSING_TOOL, // The base class script has the "@tool" annotation, but this script does not have it.72REDUNDANT_STATIC_UNLOAD, // The `@static_unload` annotation is used but the class does not have static data.73REDUNDANT_AWAIT, // await is used but expression is synchronous (not a signal nor a coroutine).74MISSING_AWAIT, // await is not used but expression is a coroutine.75ASSERT_ALWAYS_TRUE, // Expression for assert argument is always true.76ASSERT_ALWAYS_FALSE, // Expression for assert argument is always false.77INTEGER_DIVISION, // Integer divide by integer, decimal part is discarded.78NARROWING_CONVERSION, // Float value into an integer slot, precision is lost.79INT_AS_ENUM_WITHOUT_CAST, // An integer value was used as an enum value without casting.80INT_AS_ENUM_WITHOUT_MATCH, // An integer value was used as an enum value without matching enum member.81ENUM_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.82EMPTY_FILE, // A script file is empty.83DEPRECATED_KEYWORD, // The keyword is deprecated and should be replaced.84CONFUSABLE_IDENTIFIER, // The identifier contains misleading characters that can be confused. E.g. "usеr" (has Cyrillic "е" instead of Latin "e").85CONFUSABLE_LOCAL_DECLARATION, // The parent block declares an identifier with the same name below.86CONFUSABLE_LOCAL_USAGE, // The identifier will be shadowed below in the block.87CONFUSABLE_CAPTURE_REASSIGNMENT, // Reassigning lambda capture does not modify the outer local variable.88INFERENCE_ON_VARIANT, // The declaration uses type inference but the value is typed as Variant.89NATIVE_METHOD_OVERRIDE, // The script method overrides a native one, this may not work as intended.90GET_NODE_DEFAULT_WITHOUT_ONREADY, // A class variable uses `get_node()` (or the `$` notation) as its default value, but does not use the @onready annotation.91ONREADY_WITH_EXPORT, // The `@onready` annotation will set the value after `@export` which is likely not intended.92#ifndef DISABLE_DEPRECATED93PROPERTY_USED_AS_FUNCTION, // Function not found, but there's a property with the same name.94CONSTANT_USED_AS_FUNCTION, // Function not found, but there's a constant with the same name.95FUNCTION_USED_AS_PROPERTY, // Property not found, but there's a function with the same name.96#endif97WARNING_MAX,98};99100#ifndef DISABLE_DEPRECATED101static constexpr int FIRST_DEPRECATED_WARNING = PROPERTY_USED_AS_FUNCTION;102#endif103104constexpr static WarnLevel default_warning_levels[] = {105WARN, // UNASSIGNED_VARIABLE106WARN, // UNASSIGNED_VARIABLE_OP_ASSIGN107WARN, // UNUSED_VARIABLE108WARN, // UNUSED_LOCAL_CONSTANT109WARN, // UNUSED_PRIVATE_CLASS_VARIABLE110WARN, // UNUSED_PARAMETER111WARN, // UNUSED_SIGNAL112WARN, // SHADOWED_VARIABLE113WARN, // SHADOWED_VARIABLE_BASE_CLASS114WARN, // SHADOWED_GLOBAL_IDENTIFIER115WARN, // UNREACHABLE_CODE116WARN, // UNREACHABLE_PATTERN117WARN, // STANDALONE_EXPRESSION118WARN, // STANDALONE_TERNARY119WARN, // INCOMPATIBLE_TERNARY120IGNORE, // UNTYPED_DECLARATION // Static typing is optional, we don't want to spam warnings.121IGNORE, // INFERRED_DECLARATION // Static typing is optional, we don't want to spam warnings.122IGNORE, // UNSAFE_PROPERTY_ACCESS // Too common in untyped scenarios.123IGNORE, // UNSAFE_METHOD_ACCESS // Too common in untyped scenarios.124IGNORE, // UNSAFE_CAST // Too common in untyped scenarios.125IGNORE, // UNSAFE_CALL_ARGUMENT // Too common in untyped scenarios.126WARN, // UNSAFE_VOID_RETURN127IGNORE, // RETURN_VALUE_DISCARDED // Too spammy by default on common cases (connect, Tween, etc.).128WARN, // STATIC_CALLED_ON_INSTANCE129WARN, // MISSING_TOOL130WARN, // REDUNDANT_STATIC_UNLOAD131WARN, // REDUNDANT_AWAIT132IGNORE, // MISSING_AWAIT133WARN, // ASSERT_ALWAYS_TRUE134WARN, // ASSERT_ALWAYS_FALSE135WARN, // INTEGER_DIVISION136WARN, // NARROWING_CONVERSION137WARN, // INT_AS_ENUM_WITHOUT_CAST138WARN, // INT_AS_ENUM_WITHOUT_MATCH139WARN, // ENUM_VARIABLE_WITHOUT_DEFAULT140WARN, // EMPTY_FILE141WARN, // DEPRECATED_KEYWORD142WARN, // CONFUSABLE_IDENTIFIER143WARN, // CONFUSABLE_LOCAL_DECLARATION144WARN, // CONFUSABLE_LOCAL_USAGE145WARN, // CONFUSABLE_CAPTURE_REASSIGNMENT146ERROR, // INFERENCE_ON_VARIANT // Most likely done by accident, usually inference is trying for a particular type.147ERROR, // NATIVE_METHOD_OVERRIDE // May not work as expected.148ERROR, // GET_NODE_DEFAULT_WITHOUT_ONREADY // May not work as expected.149ERROR, // ONREADY_WITH_EXPORT // May not work as expected.150#ifndef DISABLE_DEPRECATED151WARN, // PROPERTY_USED_AS_FUNCTION152WARN, // CONSTANT_USED_AS_FUNCTION153WARN, // FUNCTION_USED_AS_PROPERTY154#endif155};156157static_assert(std_size(default_warning_levels) == WARNING_MAX, "Amount of default levels does not match the amount of warnings.");158159Code code = WARNING_MAX;160int start_line = -1, end_line = -1;161Vector<String> symbols;162163String get_name() const;164String get_message() const;165static int get_default_value(Code p_code);166static PropertyInfo get_property_info(Code p_code);167static String get_name_from_code(Code p_code);168static String get_settings_path_from_code(Code p_code);169static Code get_code_from_name(const String &p_name);170};171172#endif // DEBUG_ENABLED173174175