Improve Error Reporting: Unnecessarily vague pattern match warning #3765
Labels
Area-Compiler-PatternMatching
pattern compilation, active patterns, performance, codegen
Feature Improvement
Impact-Medium
(Internal MS Team use only) Describes an issue with moderate impact on existing code.
Theme-Simple-F#
A cross-community initiative called "Simple F#", keeping people in the sweet spot of the language.
Milestone
What
Pattern matching provides an exhaustiveness warning if you fail to match against all cases. However, the match seems to be generalised as much as possible for e.g. tuples or records, even when it is clear that there is only a single case outstanding e.g given the following code:
we are given the following warning:
warning FS0025: Incomplete pattern matches on this expression. For example, the value '(_,Small)' may indicate a case not covered by the pattern(s).
In reality, there is only one case outstanding -
Yellow, Small
.Why
There are two reasons that this should be improved:
_
in may be confusing to newcomers. A concrete example would be easier to understand.How
I believe that the warning should be changed to always give a fully concrete example where possible e.g. numbers, strings, DU cases etc.. for all elements in a Tuple or Record.
Whilst we're at it, the wording could be slightly improved, with more guidance:
warning FS0025: The pattern match over this expression cannot be proved to account for all cases. For example, the value '(Yellow, Small)' may indicate a case not covered. You should account for all cases either by adding extra patterns for each case, or through the use of the _ (wildcard) pattern. Leaving unhandled cases in a pattern match can lead to runtime errors, and is not recommended.
I know that this is more verbose, but adding the explanation at the end will again provide a bit of guidance to new developers as well as explaining the risks of not doing it.
The text was updated successfully, but these errors were encountered: