GDScript: Warn when enum variable has no default

The default will always be set to `0`, so if it's not a valid value in
the enum, the warning is shown.
This commit is contained in:
George Marques 2024-04-16 11:16:36 -03:00
parent 658e97c93a
commit f9048fcd7d
No known key found for this signature in database
GPG key ID: 046BD46A3201E43D
7 changed files with 38 additions and 0 deletions

View file

@ -1957,6 +1957,18 @@ void GDScriptAnalyzer::resolve_assignable(GDScriptParser::AssignableNode *p_assi
} else {
parser->push_warning(p_assignable, GDScriptWarning::UNTYPED_DECLARATION, declaration_type, p_assignable->identifier->name);
}
} else if (specified_type.kind == GDScriptParser::DataType::ENUM && p_assignable->initializer == nullptr) {
// Warn about enum variables without default value. Unless the enum defines the "0" value, then it's fine.
bool has_zero_value = false;
for (const KeyValue<StringName, int64_t> &kv : specified_type.enum_values) {
if (kv.value == 0) {
has_zero_value = true;
break;
}
}
if (!has_zero_value) {
parser->push_warning(p_assignable, GDScriptWarning::ENUM_VARIABLE_WITHOUT_DEFAULT, p_assignable->identifier->name);
}
}
#endif