godotengine / godotengine/godot
Unexpected `CONFUSABLE_LOCAL_DECLARATION` warning
- Dominant language
- C++
- Stars
- 117k
- Forks
- 26.8k
- PR merge metrics
- PR metrics pending
Description
### Tested versions
4.4.1.stable & master[b89c47bb8]
### System information
Godot v4.5.dev (b89c47bb8) - Arch Linux #1 SMP PREEMPT_DYNAMIC Thu, 22 May 2025 05:37:49 +0000 on X11 - X11 display driver, Multi-window, 2 monitors - OpenGL 3 (Compatibility) - NVIDIA GeForce RTX 3060 Ti (nvidia; 570.153.02) - 13th Gen Intel(R) Core(TM) i5-13400F (16 threads)
### Issue description
```gdscript
func example(input: int) -> int:
if input > 0:
var id := input * 2 # CONFUSABLE_LOCAL_DECLARATION
return id
var id := input + 2
return id
```
The code above produces a warning on line 3:
> CONFUSABLE_LOCAL_DECLARATION: The variable "id" is declared below in the parent block.
The two `id`s are in different scopes that never overlap.
In the actual project, I perform checks and variable construction within a `for` loop, GDScript produces the same warning for early `continue`s.
```gdscript
func example(input: int) -> Array:
var result := []
for i in input:
if i % 2 == 0:
var id := input * 2 # CONFUSABLE_LOCAL_DECLARATION
result.append(id)
continue
if i % 3 == 1:
var id := input * 3 # CONFUSABLE_LOCAL_DECLARATION
result.append(id)
continue
var id := input + 2
result.append(id)
return result
```
Lambda:
```gdscript
var neighbors := NEIGHBOR_OFFSETS.map(func (offset: Vector2i) -> bool:
var data := map.get_cell_tile_data(cell + offset) # CONFUSABLE_LOCAL_DECLARATION
return data and data.get_custom_data("type") == "wall"
)
var data := map.get_cell_tile_data(cell)
if data and data.get_custom_data("type") == "wall":
pass
```
### Steps to reproduce
Use the code in the issue description.
### Minimal reproduction project (MRP)
N/A
Contributor guide
Assessment
This issue has not been assessed yet.