dotnet / dotnet/fsharp

for 0 in ... do gives warning "Incomplete pattern matches" (FS0025)

Open
#14,140 11 comments 5 reactions 0 assignees View on GitHub
Area-Diagnostics Feature Improvement Theme-Simple-F#
Dominant language
F#
Stars
4.3k
Forks
876
Avg merge
4d 22h
Merged PRs (30d)
144

Description

Ok, to be fair, I understand why this happens, but still, reporting it as I believe we can come up with a better warning in cases like this.

![image](https://user-images.githubusercontent.com/16015770/196536996-5aeece93-544a-4702-8394-55259f7cad56.png)

## Repro steps

Any `for..in..do` loop with a constant:

```f#
for 0 in 1..10 do // warning on '0'
()
```

Or
```f#
[]
let Foo = 42

for Foo in 1..10 do // warning on Foo
()
```

## Expected behavior

A warning, sure, but not this one. It's rather uncommon for people to know, let alone use, patterns in the variable designation of a for-loop.

Maybe something like this:

> Warning: use of a constant in a for-loop identifier position may lead to runtime errors, maybe you meant to use a variable or a pattern?

## Actual behavior

You get a (rather confusing, esp. to newbies) warning:

> warning FS0025: Incomplete pattern matches on this expression. For example, the value '0' may indicate a case not covered by the pattern(s).

## Known workarounds

The code is clearly a mistake, so giving a warning is good. Workaround is obviously to fix the code.

If there was a way to statically detect that such code would always fail, we could report that as an error. Though I guess, just as with regular patterns, we have to be careful not accidentally disallowing stuff that may be out in the wild.

## Related information

As silly as it may sound, this warning actually caught me off-guard. It's probably just late in the day and I wrote `for 0 in 1..10 do`, not immediately realizing it was supposed to be `for _ in 1..10 do`.

Anyway, just throwing it out here to put on the list of warning/errors that could benefit from some attention.

## PS

Just tried the following, never even considered it, but this is legal (again, I understand why it is legal, but still, this is just weird!)

```f#
let 0 = 10 // legal, gives same FS0025 warning
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.