Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Error Reporting: Make Nullness Warning 3261 less intimidating #18170

Open
Tracked by #1103
isaacabraham opened this issue Dec 20, 2024 · 2 comments
Open
Tracked by #1103

Comments

@isaacabraham
Copy link
Contributor

isaacabraham commented Dec 20, 2024

What

The standard FS3261 warning for e.g.

let x : string | null = null
x.Length // unsafe access - FS3261

is as follows:

warning FS3261: Nullness warning: The types 'string' and 'string | null' do not have compatible nullability.

Why

This error is not especially friendly or easy to understand, nor does it give a solution to fix the issue.

How

Consider replacing with:

warning FS3261: The symbol `x` may be null, and accessing the `Length` member could lead to a runtime exception. Consider pattern matching and / or converting to an Option using 'Option.ofObj' to safely access the value.
@smoothdeveloper
Copy link
Contributor

warning FS3261: Nullness warning: The types 'string' and 'string | null' do not have compatible nullability.

->

warning FS3261: The symbol x may be null, and accessing the Length member could lead to a runtime exception. Consider pattern matching and / or converting to an Option using 'Option.ofObj' to safely access the value.

->

warning FS3261: Nullness warning: Accessing Length on x is not null-safe and can fail at runtime. More about null-safe member access: URL

url: a doc page explaining null checks, non-null, more details about use cases, performance implication, etc.

I don't feel we should recommend Option.ofObj or a specific way to make the code null-safe, but rather link to a dedicated and comprehensive page.

@isaacabraham
Copy link
Contributor Author

isaacabraham commented Dec 20, 2024

@smoothdeveloper That's a good point - I had a similar thought. However, I'm not aware of this being done anywhere else in error messages for F# (although we have started doing this in SAFE Stack packages). You're right that Option.ofObj is opinionated, but realistically, what other ways of safely handling nullness in F# is there - pattern match on null or convert to Option and then use pattern matching / map etc.?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: New
Development

No branches or pull requests

2 participants