# E0017

Warning name: `ambiguous_loop_argument`

The usage of identifier is ambiguous. If it refers to the loop variable, please
use `as <id>` to bind it in the pattern. If it refers to the original value of
the variable before entering the loop, please bind it to a new binder outside
the loop.

## Erroneous example

```{literalinclude} /sources/error_codes/0017_error/top.mbt
:language: moonbit
:start-after: start example
:end-before: end example
:prepend: "///|\nfn main {"
:append: "}"
```

Output:

```{literalinclude} /sources/error_codes/0017_error/__snapshot__/snapshot
```

Because `a` refers to the value of `a` before entering the loop, therefore the
value is always the same.

## Suggestion

Since `loop` syntax will be removed soon, prefer rewriting new code with a
`for` loop when possible. For example, this code can be written as:

```moonbit
let text : StringView = "asdf"
for i = 0; i < text.length(); i = i + 1 {
  println(text[i:])
}
```

If you are maintaining existing `loop` code, and you want to refer to the loop
variable that changes with loop iterations, use `as <id>` to bind it in the
pattern.

```{literalinclude} /sources/error_codes/0017_fixed/example_0.mbt
:language: moonbit
:start-after: start example
:end-before: end example
:prepend: "///|\nfn main {"
:append: "}"
```

Output:

```{literalinclude} /sources/error_codes/0017_fixed/__snapshot__/snapshot_0
```

Or, if you want to refer to the original value of the variable before entering
the loop, explicitly bind it to another name outside the loop.

```{literalinclude} /sources/error_codes/0017_fixed/example_1.mbt
:language: moonbit
:start-after: start example
:end-before: end example
:prepend: "///|\nfn main {"
:append: "}"
```

Output:

```{literalinclude} /sources/error_codes/0017_fixed/__snapshot__/snapshot_1
```
