dotnet / dotnet/fsharp

Better diagnostics for mixing different nullness pattern matching ways

Open
#17,432 6 comments 0 reactions 0 assignees View on GitHub
Area-Compiler-PatternMatching Area-Diagnostics Area-Nullness
Dominant language
F#
Stars
4.3k
Forks
876
Avg merge
4d 22h
Merged PRs (30d)
144

Description

There are 2 ways to do null-related pattern matching now:

```fsharp
let len1 (str: string | null) =
match str with
| Null -> -1
| NonNull s -> s.Length

let len2 (str: string | null) =
match str with
| null -> -1
| s -> s.Length
```

Both produce no warnings, all good. What I don't like is the behavior when we start mixing those two approaches:

```fsharp
let len3 (str: string | null) =
match str with
| null -> -1
| NonNull s -> s.Length
```
> warning FS3262: Value known to be without null passed to a function meant for nullables: You can remove this |Null|NonNull| pattern usage.

```fsharp
let len4 (str: string | null) =
match str with
| Null -> -1
| s -> s.Length
```
> warning FS3261: Nullness warning: The types 'string' and 'string | null' do not have compatible nullability.

---

As a user I just want consistent diags here in the spirit of "hey, pick one way or another, don't mix things up" - ideally with exact pointers what to change but that's an extra.

I think such errors can happen really often because of fatfingering or copypasting - and casing differences are not even very noticeable, so there is accessibility in the game as well.

_Originally posted by @psfinaki in https://github.com/dotnet/fsharp/pull/15181#discussion_r1677921389_

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.