for 0 in ... do gives warning "Incomplete pattern matches" (FS0025)
See original GitHub issueOk, 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.
Repro steps
Any for..in..do
loop with a constant:
for 0 in 1..10 do // warning on '0'
()
Or
[<Literal>]
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!)
let 0 = 10 // legal, gives same FS0025 warning
Issue Analytics
- State:
- Created a year ago
- Reactions:4
- Comments:11 (11 by maintainers)
Top GitHub Comments
Yes this is not a bug, but the error message could be improved - actually also at argument positions and other places where incomplete patterns really aren’t expected, e.g.
Agreed with @charlesroddie that the proposed diagnostic needs more work. Possibly
Oh yeah, this is very confusing message. We should definitely improve it.