Path: blob/master/modules/gdscript/gdscript_warning.cpp
10277 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 ASSERT_ALWAYS_TRUE:121return "Assert statement is redundant because the expression is always true.";122case ASSERT_ALWAYS_FALSE:123return "Assert statement will raise an error because the expression is always false.";124case INTEGER_DIVISION:125return "Integer division. Decimal part will be discarded.";126case NARROWING_CONVERSION:127return "Narrowing conversion (float is converted to int and loses precision).";128case INT_AS_ENUM_WITHOUT_CAST:129return "Integer used when an enum value is expected. If this is intended, cast the integer to the enum type using the \"as\" keyword.";130case INT_AS_ENUM_WITHOUT_MATCH:131CHECK_SYMBOLS(3);132return vformat(R"(Cannot %s %s as Enum "%s": no enum member has matching value.)", symbols[0], symbols[1], symbols[2]);133case ENUM_VARIABLE_WITHOUT_DEFAULT:134CHECK_SYMBOLS(1);135return 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]);136case EMPTY_FILE:137return "Empty script file.";138case DEPRECATED_KEYWORD:139CHECK_SYMBOLS(2);140return vformat(R"(The "%s" keyword is deprecated and will be removed in a future release. Please replace it with "%s".)", symbols[0], symbols[1]);141case CONFUSABLE_IDENTIFIER:142CHECK_SYMBOLS(1);143return vformat(R"(The identifier "%s" has misleading characters and might be confused with something else.)", symbols[0]);144case CONFUSABLE_LOCAL_DECLARATION:145CHECK_SYMBOLS(2);146return vformat(R"(The %s "%s" is declared below in the parent block.)", symbols[0], symbols[1]);147case CONFUSABLE_LOCAL_USAGE:148CHECK_SYMBOLS(1);149return vformat(R"(The identifier "%s" will be shadowed below in the block.)", symbols[0]);150case CONFUSABLE_CAPTURE_REASSIGNMENT:151CHECK_SYMBOLS(1);152return vformat(R"(Reassigning lambda capture does not modify the outer local variable "%s".)", symbols[0]);153case INFERENCE_ON_VARIANT:154CHECK_SYMBOLS(1);155return vformat("The %s type is being inferred from a Variant value, so it will be typed as Variant.", symbols[0]);156case NATIVE_METHOD_OVERRIDE:157CHECK_SYMBOLS(2);158return 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]);159case GET_NODE_DEFAULT_WITHOUT_ONREADY:160CHECK_SYMBOLS(1);161return 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]);162case ONREADY_WITH_EXPORT:163return R"("@onready" will set the default value after "@export" takes effect and will override it.)";164#ifndef DISABLE_DEPRECATED165// Never produced. These warnings migrated from 3.x by mistake.166case PROPERTY_USED_AS_FUNCTION: // There is already an error.167case CONSTANT_USED_AS_FUNCTION: // There is already an error.168case FUNCTION_USED_AS_PROPERTY: // This is valid, returns `Callable`.169break;170#endif171case WARNING_MAX:172break; // Can't happen, but silences warning.173}174ERR_FAIL_V_MSG(String(), vformat(R"(Invalid GDScript warning "%s".)", get_name_from_code(code)));175176#undef CHECK_SYMBOLS177}178179int GDScriptWarning::get_default_value(Code p_code) {180ERR_FAIL_INDEX_V_MSG(p_code, WARNING_MAX, WarnLevel::IGNORE, "Getting default value of invalid warning code.");181return default_warning_levels[p_code];182}183184PropertyInfo GDScriptWarning::get_property_info(Code p_code) {185return PropertyInfo(Variant::INT, get_settings_path_from_code(p_code), PROPERTY_HINT_ENUM, "Ignore,Warn,Error");186}187188String GDScriptWarning::get_name() const {189return get_name_from_code(code);190}191192String GDScriptWarning::get_name_from_code(Code p_code) {193ERR_FAIL_COND_V(p_code < 0 || p_code >= WARNING_MAX, String());194195static const char *names[] = {196PNAME("UNASSIGNED_VARIABLE"),197PNAME("UNASSIGNED_VARIABLE_OP_ASSIGN"),198PNAME("UNUSED_VARIABLE"),199PNAME("UNUSED_LOCAL_CONSTANT"),200PNAME("UNUSED_PRIVATE_CLASS_VARIABLE"),201PNAME("UNUSED_PARAMETER"),202PNAME("UNUSED_SIGNAL"),203PNAME("SHADOWED_VARIABLE"),204PNAME("SHADOWED_VARIABLE_BASE_CLASS"),205PNAME("SHADOWED_GLOBAL_IDENTIFIER"),206PNAME("UNREACHABLE_CODE"),207PNAME("UNREACHABLE_PATTERN"),208PNAME("STANDALONE_EXPRESSION"),209PNAME("STANDALONE_TERNARY"),210PNAME("INCOMPATIBLE_TERNARY"),211PNAME("UNTYPED_DECLARATION"),212PNAME("INFERRED_DECLARATION"),213PNAME("UNSAFE_PROPERTY_ACCESS"),214PNAME("UNSAFE_METHOD_ACCESS"),215PNAME("UNSAFE_CAST"),216PNAME("UNSAFE_CALL_ARGUMENT"),217PNAME("UNSAFE_VOID_RETURN"),218PNAME("RETURN_VALUE_DISCARDED"),219PNAME("STATIC_CALLED_ON_INSTANCE"),220PNAME("MISSING_TOOL"),221PNAME("REDUNDANT_STATIC_UNLOAD"),222PNAME("REDUNDANT_AWAIT"),223PNAME("ASSERT_ALWAYS_TRUE"),224PNAME("ASSERT_ALWAYS_FALSE"),225PNAME("INTEGER_DIVISION"),226PNAME("NARROWING_CONVERSION"),227PNAME("INT_AS_ENUM_WITHOUT_CAST"),228PNAME("INT_AS_ENUM_WITHOUT_MATCH"),229PNAME("ENUM_VARIABLE_WITHOUT_DEFAULT"),230PNAME("EMPTY_FILE"),231PNAME("DEPRECATED_KEYWORD"),232PNAME("CONFUSABLE_IDENTIFIER"),233PNAME("CONFUSABLE_LOCAL_DECLARATION"),234PNAME("CONFUSABLE_LOCAL_USAGE"),235PNAME("CONFUSABLE_CAPTURE_REASSIGNMENT"),236PNAME("INFERENCE_ON_VARIANT"),237PNAME("NATIVE_METHOD_OVERRIDE"),238PNAME("GET_NODE_DEFAULT_WITHOUT_ONREADY"),239PNAME("ONREADY_WITH_EXPORT"),240#ifndef DISABLE_DEPRECATED241"PROPERTY_USED_AS_FUNCTION",242"CONSTANT_USED_AS_FUNCTION",243"FUNCTION_USED_AS_PROPERTY",244#endif245};246247static_assert(std::size(names) == WARNING_MAX, "Amount of warning types don't match the amount of warning names.");248249return names[(int)p_code];250}251252String GDScriptWarning::get_settings_path_from_code(Code p_code) {253return "debug/gdscript/warnings/" + get_name_from_code(p_code).to_lower();254}255256GDScriptWarning::Code GDScriptWarning::get_code_from_name(const String &p_name) {257for (int i = 0; i < WARNING_MAX; i++) {258if (get_name_from_code((Code)i) == p_name) {259return (Code)i;260}261}262263return WARNING_MAX;264}265266#endif // DEBUG_ENABLED267268269