question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

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:open
  • Created a year ago
  • Reactions:4
  • Comments:11 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
dsymecommented, Oct 26, 2022

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.

> let f 0 = 1;;

  let f 0 = 1;;
  ------^

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

val f: int -> int

Agreed with @charlesroddie that the proposed diagnostic needs more work. Possibly

Incomplete pattern matches on this expression. Did you use a constant where a loop variable was expected?  For example, the value '1' may indicate a case not covered by the pattern(s).

Incomplete pattern matches on this expression. Did you use a constant where an argument name was expected?  For example, the value '1' may indicate a case not covered by the pattern(s).
1reaction
vzarytovskiicommented, Oct 21, 2022

Oh yeah, this is very confusing message. We should definitely improve it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Compiler error FS0025 - F# | Microsoft Learn
FS0025 : Incomplete pattern matches on this expression. For example, the value 'Cold' may indicate a case not covered by the pattern(s).
Read more >
F# pattern matching oddity
warning FS0025 : Incomplete pattern matches on this expression. For example, the value '[]' may indicate a case not covered by the pattern(s)....
Read more >
Why is an incomplete pattern match only a warning and not ...
This was surprising to me at first but there are situations where you guarantee a pattern matching on a higher level it becomes...
Read more >
Exhaustive pattern matching
Incomplete pattern matches on this expression. ... All it takes is for one piece of code to forget to do this, and the...
Read more >
How do I make VS Code treat a specific warning as an error?
In VS I set the FS0025 warning (Incomplete pattern matches on this expression) to be treated as an error so I can't compile...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found