Skip to content

Commit b569ae3

Browse files
committed
Merge pull request #92027 from dalexeev/gds-fix-standalone-expression-for-preload
GDScript: Fix `STANDALONE_EXPRESSION` warning for `preload()`
2 parents 5b34162 + 7dd801c commit b569ae3

File tree

8 files changed

+30
-7
lines changed

8 files changed

+30
-7
lines changed

doc/classes/ProjectSettings.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -554,10 +554,10 @@
554554
When set to [code]warn[/code] or [code]error[/code], produces a warning or an error respectively when defining a local or subclass member variable that would shadow a variable that is inherited from a parent class.
555555
</member>
556556
<member name="debug/gdscript/warnings/standalone_expression" type="int" setter="" getter="" default="1">
557-
When set to [code]warn[/code] or [code]error[/code], produces a warning or an error respectively when calling an expression that has no effect on the surrounding code, such as writing [code]2 + 2[/code] as a statement.
557+
When set to [code]warn[/code] or [code]error[/code], produces a warning or an error respectively when calling an expression that may have no effect on the surrounding code, such as writing [code]2 + 2[/code] as a statement.
558558
</member>
559559
<member name="debug/gdscript/warnings/standalone_ternary" type="int" setter="" getter="" default="1">
560-
When set to [code]warn[/code] or [code]error[/code], produces a warning or an error respectively when calling a ternary expression that has no effect on the surrounding code, such as writing [code]42 if active else 0[/code] as a statement.
560+
When set to [code]warn[/code] or [code]error[/code], produces a warning or an error respectively when calling a ternary expression that may have no effect on the surrounding code, such as writing [code]42 if active else 0[/code] as a statement.
561561
</member>
562562
<member name="debug/gdscript/warnings/static_called_on_instance" type="int" setter="" getter="" default="1">
563563
When set to [code]warn[/code] or [code]error[/code], produces a warning or an error respectively when calling a static method from an instance of a class instead of from the class directly.

modules/gdscript/gdscript_analyzer.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3394,6 +3394,7 @@ void GDScriptAnalyzer::reduce_call(GDScriptParser::CallNode *p_call, bool p_is_a
33943394
}
33953395

33963396
#ifdef DEBUG_ENABLED
3397+
// FIXME: No warning for built-in constructors and utilities due to early return.
33973398
if (p_is_root && return_type.kind != GDScriptParser::DataType::UNRESOLVED && return_type.builtin_type != Variant::NIL &&
33983399
!(p_call->is_super && p_call->function_name == GDScriptLanguage::get_singleton()->strings._init)) {
33993400
parser->push_warning(p_call, GDScriptWarning::RETURN_VALUE_DISCARDED, p_call->function_name);

modules/gdscript/gdscript_parser.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1877,6 +1877,10 @@ GDScriptParser::Node *GDScriptParser::parse_statement() {
18771877
case Node::CALL:
18781878
// Fine.
18791879
break;
1880+
case Node::PRELOAD:
1881+
// `preload` is a function-like keyword.
1882+
push_warning(expression, GDScriptWarning::RETURN_VALUE_DISCARDED, "preload");
1883+
break;
18801884
case Node::LAMBDA:
18811885
// Standalone lambdas can't be used, so make this an error.
18821886
push_error("Standalone lambdas cannot be accessed. Consider assigning it to a variable.", expression);

modules/gdscript/gdscript_warning.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ String GDScriptWarning::get_message() const {
7474
case UNREACHABLE_PATTERN:
7575
return "Unreachable pattern (pattern after wildcard or bind).";
7676
case STANDALONE_EXPRESSION:
77-
return "Standalone expression (the line has no effect).";
77+
return "Standalone expression (the line may have no effect).";
7878
case STANDALONE_TERNARY:
7979
return "Standalone ternary operator: the return value is being discarded.";
8080
case INCOMPATIBLE_TERNARY:

modules/gdscript/tests/scripts/parser/warnings/return_value_discarded.gd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ func i_return_int() -> int:
44

55
func test():
66
i_return_int()
7+
preload("../../utils.notest.gd") # `preload` is a function-like keyword.

modules/gdscript/tests/scripts/parser/warnings/return_value_discarded.out

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,7 @@ GDTEST_OK
33
>> Line: 6
44
>> RETURN_VALUE_DISCARDED
55
>> The function "i_return_int()" returns a value that will be discarded if not used.
6+
>> WARNING
7+
>> Line: 7
8+
>> RETURN_VALUE_DISCARDED
9+
>> The function "preload()" returns a value that will be discarded if not used.

modules/gdscript/tests/scripts/parser/warnings/standalone_expression.gd

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,16 @@ func test():
66
Vector3.ZERO
77
[true, false]
88
float(125)
9+
# The following statements should not produce `STANDALONE_EXPRESSION`:
10+
var _a = 1
11+
_a = 2 # Assignment is a local (or global) side effect.
12+
@warning_ignore("redundant_await")
13+
await 3 # The `await` operand is usually a coroutine or a signal.
14+
absi(4) # A call (in general) can have side effects.
15+
@warning_ignore("return_value_discarded")
16+
preload("../../utils.notest.gd") # A static initializer may have side effects.
17+
"""
18+
Python-like "comment".
19+
"""
20+
@warning_ignore("standalone_ternary")
21+
1 if 2 else 3 # Produces `STANDALONE_TERNARY` instead.

modules/gdscript/tests/scripts/parser/warnings/standalone_expression.out

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ GDTEST_OK
22
>> WARNING
33
>> Line: 3
44
>> STANDALONE_EXPRESSION
5-
>> Standalone expression (the line has no effect).
5+
>> Standalone expression (the line may have no effect).
66
>> WARNING
77
>> Line: 4
88
>> STANDALONE_EXPRESSION
9-
>> Standalone expression (the line has no effect).
9+
>> Standalone expression (the line may have no effect).
1010
>> WARNING
1111
>> Line: 6
1212
>> STANDALONE_EXPRESSION
13-
>> Standalone expression (the line has no effect).
13+
>> Standalone expression (the line may have no effect).
1414
>> WARNING
1515
>> Line: 7
1616
>> STANDALONE_EXPRESSION
17-
>> Standalone expression (the line has no effect).
17+
>> Standalone expression (the line may have no effect).

0 commit comments

Comments
 (0)