dotnet / dotnet/fsharp

FS0026 "This rule will never be matched" not fired for combined match branches

Open
#7,878 5 comments 3 reactions 1 assignee Claimed by @abelbraaksma View on GitHub
Area-Compiler-PatternMatching Feature Improvement
Dominant language
F#
Stars
4.3k
Forks
876
Avg merge
4d 11h
Merged PRs (30d)
131

Description

The following gives FS0026 correctly:

```f#
match None with
| Some _ -> ()
| Some "" -> () // FS0026 This rule will never be matched
| None -> ()
| _ -> () // FS0026 This rule will never be matched
```

The following does not:

```f#
match None with
| Some _
| Some "" -> () // does not raise warning FS0026 This rule will never be matched
| None
| _ -> () // does not raise warning FS0026 This rule will never be matched
```

Though in either case, the 2nd and 4th branches are redundant. Does the compiler optimize that away, which would make the warning redundant? Either way, I think the warning ought to be raised in the first and second case.

**Repro steps**

See code above, just copy/paste.

**Expected behavior**

Both cases should give two warnings.

**Actual behavior**

Second case does not give a warning.

**Known workarounds**

Manually check your code for redundant checks or branches. Which is precisely what we're _not_ supposed to have to do (and it is how I found this case in existing code I was reviewing), hence I'm filing it as a bug ;).

Whether either example leads to all branches being compiled, I don't know. I'd hope/guess/suspect that in release builds, the redundant branches are removed, but I didn't verify yet.

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.