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 Wrong identification of unassigned property #7405

Open
JornWildt opened this issue Sep 4, 2024 · 0 comments
Open

CS8618 Wrong identification of unassigned property #7405

JornWildt opened this issue Sep 4, 2024 · 0 comments

Comments

@JornWildt
Copy link

Analyzer

Diagnostic ID: [CS8618] Non-nullable property 'XXX' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Analyzer source

.NET SDK:
Version: 8.0.304
Commit: 352dc5a01f
Workload version: 8.0.300-manifests.113cb230
MSBuild version: 17.10.4+10fbfbf2e

Runtime Environment:
OS Name: Windows
OS Version: 10.0.22631
OS Platform: Windows
RID: win-x64
Base Path: C:\Program Files\dotnet\sdk\8.0.304\

.NET workloads installed:
[aspire]
Installation Source: VS 17.10.35201.131
Manifest Version: 8.1.0/8.0.100
Manifest Path: C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.aspire\8.1.0\WorkloadManifest.json
Install Type: FileBased

Host:
Version: 8.0.8
Architecture: x64
Commit: 08338fcaa5

.NET SDKs installed:
8.0.304 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
Microsoft.AspNetCore.App 8.0.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 6.0.33 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 8.0.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 8.0.8 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other architectures found:
x86 [C:\Program Files (x86)\dotnet]
registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]

Describe the bug

The analyzer identifies a member property Message as being not-set in the following constructor:

public class Test
{
  private string Message { get; set; }

  public Test()
  {
    Message = "";
    Console.WriteLine($"Message size: {Message?.Length}");
  }
}

Steps To Reproduce

Compile the above class.

Expected behavior

No warnings.

Actual behavior

The compiler (and VS2022 editor) complains with:

warning CS8618: Non-nullable property 'Message' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Additional context

If Message?.Length is changed to Message!.Length then no warning is issued. But I would not expect the ?. operator to change the compiler warning - from my point of view, the property Message should have the same nullability whether ?. or !. is used - the code does assign Message a value in the constructor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant