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

CS8618 Gives a misleading or confusing message #68716

Closed
wilfriedb opened this issue Jun 21, 2023 · 2 comments · Fixed by #72756 or #72767
Closed

CS8618 Gives a misleading or confusing message #68716

wilfriedb opened this issue Jun 21, 2023 · 2 comments · Fixed by #72756 or #72767
Labels
Area-Compilers Concept-Diagnostic Clarity The issues deals with the ease of understanding of errors and warnings. help wanted The issue is "up for grabs" - add a comment if you are interested in working on it
Milestone

Comments

@wilfriedb
Copy link

wilfriedb commented Jun 21, 2023

Version Used: NET 7

Steps to Reproduce:

  1. Make a class with a property with as type a non-nullable reference type (e.g. string)
  2. The warning "CS8618 Non-nullable variable must contain a non-null value when exiting constructor. Consider declaring it as nullable." appears.

This message is either misleading or confusing because while making the property nullability is an easy fix, it is often not the best one. In my experience, adding the required modifier is often a better solution. Of course this varies case-by-case.

On StackOverflow there are many questions about this. Apparently it confuses people. People propose ugly fixes like:
public string Myproperty { get; set; } = null!; (Adding the null forgiving operator)
or
public string Myproperty { get; set; } = string.Empty; (The null is replaced with an empty string, which is only slightly better)

Also here on GitHub people ask questions about this message, see #37975

My proposal is to change the message to something like: " Non-nullable variable must contain a non-null value when exiting constructor. Consider adding the required modifier or declaring it as nullable."

Adding the required modifier also has the advantage that is adds an error if you still forget to initialize, be it in the constructor or with an object initializer.

The documentation at
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/nullable-warnings#nonnullable-reference-not-initialized
should be updated as well if this message is adjusted.

Diagnostic Id: CS8618

Expected Behavior:
Non-nullable variable must contain a non-null value when exiting constructor. Consider adding the required modifier or declaring it as nullable.

Actual Behavior:
Non-nullable variable must contain a non-null value when exiting constructor. Consider declaring it as nullable.

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Jun 21, 2023
@CyrusNajmabadi CyrusNajmabadi added the Concept-Diagnostic Clarity The issues deals with the ease of understanding of errors and warnings. label Jun 21, 2023
@IdeaHunter
Copy link

That is very usefull suggestion, i didnt even known the 'required' feature is here at all

@jcouv jcouv added this to the C# 12.0 milestone Jun 26, 2023
@jcouv jcouv added help wanted The issue is "up for grabs" - add a comment if you are interested in working on it and removed untriaged Issues and PRs which have not yet been triaged by a lead labels Jun 26, 2023
@jaredpar jaredpar modified the milestones: C# 12.0, Backlog Sep 11, 2023
@wilfriedb
Copy link
Author

Thank you very much for fixing this issue!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Concept-Diagnostic Clarity The issues deals with the ease of understanding of errors and warnings. help wanted The issue is "up for grabs" - add a comment if you are interested in working on it
Projects
None yet
5 participants