浏览代码

Merge pull request #61666 from nathanfranke/fix-match-bind

gdscript: use correct error for unused bind match, suppress with underscore
George Marques 3 年之前
父节点
当前提交
67156aa4c2

+ 2 - 2
modules/gdscript/gdscript_analyzer.cpp

@@ -1657,8 +1657,8 @@ void GDScriptAnalyzer::resolve_match_pattern(GDScriptParser::PatternNode *p_matc
 			p_match_pattern->bind->set_datatype(result);
 #ifdef DEBUG_ENABLED
 			is_shadowing(p_match_pattern->bind, "pattern bind");
-			if (p_match_pattern->bind->usages == 0) {
-				parser->push_warning(p_match_pattern->bind, GDScriptWarning::UNASSIGNED_VARIABLE, p_match_pattern->bind->name);
+			if (p_match_pattern->bind->usages == 0 && !String(p_match_pattern->bind->name).begins_with("_")) {
+				parser->push_warning(p_match_pattern->bind, GDScriptWarning::UNUSED_VARIABLE, p_match_pattern->bind->name);
 			}
 #endif
 			break;

+ 13 - 0
modules/gdscript/tests/scripts/parser/features/match_bind_unused.gd

@@ -0,0 +1,13 @@
+# https://github.com/godotengine/godot/pull/61666
+
+func test():
+	var dict := {"key": "value"}
+	match dict:
+		{"key": var value}:
+			print(value) # used, no warning
+	match dict:
+		{"key": var value}:
+			pass # unused, warning
+	match dict:
+		{"key": var _value}:
+			pass # unused, suppressed warning from underscore

+ 6 - 0
modules/gdscript/tests/scripts/parser/features/match_bind_unused.out

@@ -0,0 +1,6 @@
+GDTEST_OK
+>> WARNING
+>> Line: 9
+>> UNUSED_VARIABLE
+>> The local variable 'value' is declared but never used in the block. If this is intended, prefix it with an underscore: '_value'
+value