Path: blob/master/modules/gdscript/gdscript_warning.cpp
11351 views
/**************************************************************************/1/* gdscript_warning.cpp */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#include "gdscript_warning.h"3132#include "core/variant/variant.h"3334#ifdef DEBUG_ENABLED3536String GDScriptWarning::get_message() const {37#define CHECK_SYMBOLS(m_amount) ERR_FAIL_COND_V(symbols.size() < m_amount, String());3839switch (code) {40case UNASSIGNED_VARIABLE:41CHECK_SYMBOLS(1);42return vformat(R"(The variable "%s" is used before being assigned a value.)", symbols[0]);43case UNASSIGNED_VARIABLE_OP_ASSIGN:44CHECK_SYMBOLS(2);45return vformat(R"(The variable "%s" is modified with the compound-assignment operator "%s=" but was not previously initialized.)", symbols[0], symbols[1]);46case UNUSED_VARIABLE:47CHECK_SYMBOLS(1);48return vformat(R"(The local variable "%s" is declared but never used in the block. If this is intended, prefix it with an underscore: "_%s".)", symbols[0], symbols[0]);49case UNUSED_LOCAL_CONSTANT:50CHECK_SYMBOLS(1);51return vformat(R"(The local constant "%s" is declared but never used in the block. If this is intended, prefix it with an underscore: "_%s".)", symbols[0], symbols[0]);52case UNUSED_PRIVATE_CLASS_VARIABLE:53CHECK_SYMBOLS(1);54return vformat(R"(The class variable "%s" is declared but never used in the class.)", symbols[0]);55case UNUSED_PARAMETER:56CHECK_SYMBOLS(2);57return vformat(R"*(The parameter "%s" is never used in the function "%s()". If this is intended, prefix it with an underscore: "_%s".)*", symbols[1], symbols[0], symbols[1]);58case UNUSED_SIGNAL:59CHECK_SYMBOLS(1);60return vformat(R"(The signal "%s" is declared but never explicitly used in the class.)", symbols[0]);61case SHADOWED_VARIABLE:62CHECK_SYMBOLS(4);63return vformat(R"(The local %s "%s" is shadowing an already-declared %s at line %s in the current class.)", symbols[0], symbols[1], symbols[2], symbols[3]);64case SHADOWED_VARIABLE_BASE_CLASS:65CHECK_SYMBOLS(4);66if (symbols.size() > 4) {67return vformat(R"(The local %s "%s" is shadowing an already-declared %s at line %s in the base class "%s".)", symbols[0], symbols[1], symbols[2], symbols[3], symbols[4]);68}69return vformat(R"(The local %s "%s" is shadowing an already-declared %s in the base class "%s".)", symbols[0], symbols[1], symbols[2], symbols[3]);70case SHADOWED_GLOBAL_IDENTIFIER:71CHECK_SYMBOLS(3);72return vformat(R"(The %s "%s" has the same name as a %s.)", symbols[0], symbols[1], symbols[2]);73case UNREACHABLE_CODE:74CHECK_SYMBOLS(1);75return vformat(R"*(Unreachable code (statement after return) in function "%s()".)*", symbols[0]);76case UNREACHABLE_PATTERN:77return "Unreachable pattern (pattern after wildcard or bind).";78case STANDALONE_EXPRESSION:79return "Standalone expression (the line may have no effect).";80case STANDALONE_TERNARY:81return "Standalone ternary operator (the return value is being discarded).";82case INCOMPATIBLE_TERNARY:83return "Values of the ternary operator are not mutually compatible.";84case UNTYPED_DECLARATION:85CHECK_SYMBOLS(2);86if (symbols[0] == "Function") {87return vformat(R"*(%s "%s()" has no static return type.)*", symbols[0], symbols[1]);88}89return vformat(R"(%s "%s" has no static type.)", symbols[0], symbols[1]);90case INFERRED_DECLARATION:91CHECK_SYMBOLS(2);92return vformat(R"(%s "%s" has an implicitly inferred static type.)", symbols[0], symbols[1]);93case UNSAFE_PROPERTY_ACCESS:94CHECK_SYMBOLS(2);95return vformat(R"(The property "%s" is not present on the inferred type "%s" (but may be present on a subtype).)", symbols[0], symbols[1]);96case UNSAFE_METHOD_ACCESS:97CHECK_SYMBOLS(2);98return vformat(R"*(The method "%s()" is not present on the inferred type "%s" (but may be present on a subtype).)*", symbols[0], symbols[1]);99case UNSAFE_CAST:100CHECK_SYMBOLS(1);101return vformat(R"(Casting "Variant" to "%s" is unsafe.)", symbols[0]);102case UNSAFE_CALL_ARGUMENT:103CHECK_SYMBOLS(5);104return vformat(R"*(The argument %s of the %s "%s()" requires the subtype "%s" but the supertype "%s" was provided.)*", symbols[0], symbols[1], symbols[2], symbols[3], symbols[4]);105case UNSAFE_VOID_RETURN:106CHECK_SYMBOLS(2);107return vformat(R"*(The method "%s()" returns "void" but it's trying to return a call to "%s()" that can't be ensured to also be "void".)*", symbols[0], symbols[1]);108case RETURN_VALUE_DISCARDED:109CHECK_SYMBOLS(1);110return vformat(R"*(The function "%s()" returns a value that will be discarded if not used.)*", symbols[0]);111case STATIC_CALLED_ON_INSTANCE:112CHECK_SYMBOLS(2);113return vformat(R"*(The function "%s()" is a static function but was called from an instance. Instead, it should be directly called from the type: "%s.%s()".)*", symbols[0], symbols[1], symbols[0]);114case MISSING_TOOL:115return R"(The base class script has the "@tool" annotation, but this script does not have it.)";116case REDUNDANT_STATIC_UNLOAD:117return R"(The "@static_unload" annotation is redundant because the file does not have a class with static variables.)";118case REDUNDANT_AWAIT:119return R"("await" keyword is unnecessary because the expression isn't a coroutine nor a signal.)";120case MISSING_AWAIT:121return R"("await" keyword might be desired because the expression is a coroutine.)";122case ASSERT_ALWAYS_TRUE:123return "Assert statement is redundant because the expression is always true.";124case ASSERT_ALWAYS_FALSE:125return "Assert statement will raise an error because the expression is always false.";126case INTEGER_DIVISION:127return "Integer division. Decimal part will be discarded.";128case NARROWING_CONVERSION:129return "Narrowing conversion (float is converted to int and loses precision).";130case INT_AS_ENUM_WITHOUT_CAST:131return "Integer used when an enum value is expected. If this is intended, cast the integer to the enum type using the \"as\" keyword.";132case INT_AS_ENUM_WITHOUT_MATCH:133CHECK_SYMBOLS(3);134return vformat(R"(Cannot %s %s as Enum "%s": no enum member has matching value.)", symbols[0], symbols[1], symbols[2]);135case ENUM_VARIABLE_WITHOUT_DEFAULT:136CHECK_SYMBOLS(1);137return vformat(R"(The variable "%s" has an enum type and does not set an explicit default value. The default will be set to "0".)", symbols[0]);138case EMPTY_FILE:139return "Empty script file.";140case DEPRECATED_KEYWORD:141CHECK_SYMBOLS(2);142return vformat(R"(The "%s" keyword is deprecated and will be removed in a future release. Please replace it with "%s".)", symbols[0], symbols[1]);143case CONFUSABLE_IDENTIFIER:144CHECK_SYMBOLS(1);145return vformat(R"(The identifier "%s" has misleading characters and might be confused with something else.)", symbols[0]);146case CONFUSABLE_LOCAL_DECLARATION:147CHECK_SYMBOLS(2);148return vformat(R"(The %s "%s" is declared below in the parent block.)", symbols[0], symbols[1]);149case CONFUSABLE_LOCAL_USAGE:150CHECK_SYMBOLS(1);151return vformat(R"(The identifier "%s" will be shadowed below in the block.)", symbols[0]);152case CONFUSABLE_CAPTURE_REASSIGNMENT:153CHECK_SYMBOLS(1);154return vformat(R"(Reassigning lambda capture does not modify the outer local variable "%s".)", symbols[0]);155case INFERENCE_ON_VARIANT:156CHECK_SYMBOLS(1);157return vformat("The %s type is being inferred from a Variant value, so it will be typed as Variant.", symbols[0]);158case NATIVE_METHOD_OVERRIDE:159CHECK_SYMBOLS(2);160return vformat(R"*(The method "%s()" overrides a method from native class "%s". This won't be called by the engine and may not work as expected.)*", symbols[0], symbols[1]);161case GET_NODE_DEFAULT_WITHOUT_ONREADY:162CHECK_SYMBOLS(1);163return vformat(R"*(The default value uses "%s" which won't return nodes in the scene tree before "_ready()" is called. Use the "@onready" annotation to solve this.)*", symbols[0]);164case ONREADY_WITH_EXPORT:165return R"("@onready" will set the default value after "@export" takes effect and will override it.)";166#ifndef DISABLE_DEPRECATED167// Never produced. These warnings migrated from 3.x by mistake.168case PROPERTY_USED_AS_FUNCTION: // There is already an error.169case CONSTANT_USED_AS_FUNCTION: // There is already an error.170case FUNCTION_USED_AS_PROPERTY: // This is valid, returns `Callable`.171break;172#endif173case WARNING_MAX:174break; // Can't happen, but silences warning.175}176ERR_FAIL_V_MSG(String(), vformat(R"(Invalid GDScript warning "%s".)", get_name_from_code(code)));177178#undef CHECK_SYMBOLS179}180181int GDScriptWarning::get_default_value(Code p_code) {182ERR_FAIL_INDEX_V_MSG(p_code, WARNING_MAX, WarnLevel::IGNORE, "Getting default value of invalid warning code.");183return default_warning_levels[p_code];184}185186PropertyInfo GDScriptWarning::get_property_info(Code p_code) {187return PropertyInfo(Variant::INT, get_settings_path_from_code(p_code), PROPERTY_HINT_ENUM, "Ignore,Warn,Error");188}189190String GDScriptWarning::get_name() const {191return get_name_from_code(code);192}193194String GDScriptWarning::get_name_from_code(Code p_code) {195ERR_FAIL_COND_V(p_code < 0 || p_code >= WARNING_MAX, String());196197static const char *names[] = {198PNAME("UNASSIGNED_VARIABLE"),199PNAME("UNASSIGNED_VARIABLE_OP_ASSIGN"),200PNAME("UNUSED_VARIABLE"),201PNAME("UNUSED_LOCAL_CONSTANT"),202PNAME("UNUSED_PRIVATE_CLASS_VARIABLE"),203PNAME("UNUSED_PARAMETER"),204PNAME("UNUSED_SIGNAL"),205PNAME("SHADOWED_VARIABLE"),206PNAME("SHADOWED_VARIABLE_BASE_CLASS"),207PNAME("SHADOWED_GLOBAL_IDENTIFIER"),208PNAME("UNREACHABLE_CODE"),209PNAME("UNREACHABLE_PATTERN"),210PNAME("STANDALONE_EXPRESSION"),211PNAME("STANDALONE_TERNARY"),212PNAME("INCOMPATIBLE_TERNARY"),213PNAME("UNTYPED_DECLARATION"),214PNAME("INFERRED_DECLARATION"),215PNAME("UNSAFE_PROPERTY_ACCESS"),216PNAME("UNSAFE_METHOD_ACCESS"),217PNAME("UNSAFE_CAST"),218PNAME("UNSAFE_CALL_ARGUMENT"),219PNAME("UNSAFE_VOID_RETURN"),220PNAME("RETURN_VALUE_DISCARDED"),221PNAME("STATIC_CALLED_ON_INSTANCE"),222PNAME("MISSING_TOOL"),223PNAME("REDUNDANT_STATIC_UNLOAD"),224PNAME("REDUNDANT_AWAIT"),225PNAME("MISSING_AWAIT"),226PNAME("ASSERT_ALWAYS_TRUE"),227PNAME("ASSERT_ALWAYS_FALSE"),228PNAME("INTEGER_DIVISION"),229PNAME("NARROWING_CONVERSION"),230PNAME("INT_AS_ENUM_WITHOUT_CAST"),231PNAME("INT_AS_ENUM_WITHOUT_MATCH"),232PNAME("ENUM_VARIABLE_WITHOUT_DEFAULT"),233PNAME("EMPTY_FILE"),234PNAME("DEPRECATED_KEYWORD"),235PNAME("CONFUSABLE_IDENTIFIER"),236PNAME("CONFUSABLE_LOCAL_DECLARATION"),237PNAME("CONFUSABLE_LOCAL_USAGE"),238PNAME("CONFUSABLE_CAPTURE_REASSIGNMENT"),239PNAME("INFERENCE_ON_VARIANT"),240PNAME("NATIVE_METHOD_OVERRIDE"),241PNAME("GET_NODE_DEFAULT_WITHOUT_ONREADY"),242PNAME("ONREADY_WITH_EXPORT"),243#ifndef DISABLE_DEPRECATED244"PROPERTY_USED_AS_FUNCTION",245"CONSTANT_USED_AS_FUNCTION",246"FUNCTION_USED_AS_PROPERTY",247#endif248};249250static_assert(std_size(names) == WARNING_MAX, "Amount of warning types don't match the amount of warning names.");251252return names[(int)p_code];253}254255String GDScriptWarning::get_settings_path_from_code(Code p_code) {256return "debug/gdscript/warnings/" + get_name_from_code(p_code).to_lower();257}258259GDScriptWarning::Code GDScriptWarning::get_code_from_name(const String &p_name) {260for (int i = 0; i < WARNING_MAX; i++) {261if (get_name_from_code((Code)i) == p_name) {262return (Code)i;263}264}265266return WARNING_MAX;267}268269#endif // DEBUG_ENABLED270271272