Path: blob/master/modules/gdscript/gdscript_warning.h
10277 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).74ASSERT_ALWAYS_TRUE, // Expression for assert argument is always true.75ASSERT_ALWAYS_FALSE, // Expression for assert argument is always false.76INTEGER_DIVISION, // Integer divide by integer, decimal part is discarded.77NARROWING_CONVERSION, // Float value into an integer slot, precision is lost.78INT_AS_ENUM_WITHOUT_CAST, // An integer value was used as an enum value without casting.79INT_AS_ENUM_WITHOUT_MATCH, // An integer value was used as an enum value without matching enum member.80ENUM_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.81EMPTY_FILE, // A script file is empty.82DEPRECATED_KEYWORD, // The keyword is deprecated and should be replaced.83CONFUSABLE_IDENTIFIER, // The identifier contains misleading characters that can be confused. E.g. "usеr" (has Cyrillic "е" instead of Latin "e").84CONFUSABLE_LOCAL_DECLARATION, // The parent block declares an identifier with the same name below.85CONFUSABLE_LOCAL_USAGE, // The identifier will be shadowed below in the block.86CONFUSABLE_CAPTURE_REASSIGNMENT, // Reassigning lambda capture does not modify the outer local variable.87INFERENCE_ON_VARIANT, // The declaration uses type inference but the value is typed as Variant.88NATIVE_METHOD_OVERRIDE, // The script method overrides a native one, this may not work as intended.89GET_NODE_DEFAULT_WITHOUT_ONREADY, // A class variable uses `get_node()` (or the `$` notation) as its default value, but does not use the @onready annotation.90ONREADY_WITH_EXPORT, // The `@onready` annotation will set the value after `@export` which is likely not intended.91#ifndef DISABLE_DEPRECATED92PROPERTY_USED_AS_FUNCTION, // Function not found, but there's a property with the same name.93CONSTANT_USED_AS_FUNCTION, // Function not found, but there's a constant with the same name.94FUNCTION_USED_AS_PROPERTY, // Property not found, but there's a function with the same name.95#endif96WARNING_MAX,97};9899#ifndef DISABLE_DEPRECATED100static constexpr int FIRST_DEPRECATED_WARNING = PROPERTY_USED_AS_FUNCTION;101#endif102103constexpr static WarnLevel default_warning_levels[] = {104WARN, // UNASSIGNED_VARIABLE105WARN, // UNASSIGNED_VARIABLE_OP_ASSIGN106WARN, // UNUSED_VARIABLE107WARN, // UNUSED_LOCAL_CONSTANT108WARN, // UNUSED_PRIVATE_CLASS_VARIABLE109WARN, // UNUSED_PARAMETER110WARN, // UNUSED_SIGNAL111WARN, // SHADOWED_VARIABLE112WARN, // SHADOWED_VARIABLE_BASE_CLASS113WARN, // SHADOWED_GLOBAL_IDENTIFIER114WARN, // UNREACHABLE_CODE115WARN, // UNREACHABLE_PATTERN116WARN, // STANDALONE_EXPRESSION117WARN, // STANDALONE_TERNARY118WARN, // INCOMPATIBLE_TERNARY119IGNORE, // UNTYPED_DECLARATION // Static typing is optional, we don't want to spam warnings.120IGNORE, // INFERRED_DECLARATION // Static typing is optional, we don't want to spam warnings.121IGNORE, // UNSAFE_PROPERTY_ACCESS // Too common in untyped scenarios.122IGNORE, // UNSAFE_METHOD_ACCESS // Too common in untyped scenarios.123IGNORE, // UNSAFE_CAST // Too common in untyped scenarios.124IGNORE, // UNSAFE_CALL_ARGUMENT // Too common in untyped scenarios.125WARN, // UNSAFE_VOID_RETURN126IGNORE, // RETURN_VALUE_DISCARDED // Too spammy by default on common cases (connect, Tween, etc.).127WARN, // STATIC_CALLED_ON_INSTANCE128WARN, // MISSING_TOOL129WARN, // REDUNDANT_STATIC_UNLOAD130WARN, // REDUNDANT_AWAIT131WARN, // ASSERT_ALWAYS_TRUE132WARN, // ASSERT_ALWAYS_FALSE133WARN, // INTEGER_DIVISION134WARN, // NARROWING_CONVERSION135WARN, // INT_AS_ENUM_WITHOUT_CAST136WARN, // INT_AS_ENUM_WITHOUT_MATCH137WARN, // ENUM_VARIABLE_WITHOUT_DEFAULT138WARN, // EMPTY_FILE139WARN, // DEPRECATED_KEYWORD140WARN, // CONFUSABLE_IDENTIFIER141WARN, // CONFUSABLE_LOCAL_DECLARATION142WARN, // CONFUSABLE_LOCAL_USAGE143WARN, // CONFUSABLE_CAPTURE_REASSIGNMENT144ERROR, // INFERENCE_ON_VARIANT // Most likely done by accident, usually inference is trying for a particular type.145ERROR, // NATIVE_METHOD_OVERRIDE // May not work as expected.146ERROR, // GET_NODE_DEFAULT_WITHOUT_ONREADY // May not work as expected.147ERROR, // ONREADY_WITH_EXPORT // May not work as expected.148#ifndef DISABLE_DEPRECATED149WARN, // PROPERTY_USED_AS_FUNCTION150WARN, // CONSTANT_USED_AS_FUNCTION151WARN, // FUNCTION_USED_AS_PROPERTY152#endif153};154155static_assert(std::size(default_warning_levels) == WARNING_MAX, "Amount of default levels does not match the amount of warnings.");156157Code code = WARNING_MAX;158int start_line = -1, end_line = -1;159Vector<String> symbols;160161String get_name() const;162String get_message() const;163static int get_default_value(Code p_code);164static PropertyInfo get_property_info(Code p_code);165static String get_name_from_code(Code p_code);166static String get_settings_path_from_code(Code p_code);167static Code get_code_from_name(const String &p_name);168};169170#endif // DEBUG_ENABLED171172173