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

SDK 7.0.200 creates false positive CS0165, Use of unassigned local variable #66987

Closed
pieter-venter opened this issue Feb 21, 2023 · 1 comment

Comments

@pieter-venter
Copy link

Version Used:
SDK 7.0.200

Steps to Reproduce:

  1. Create a new console application with this in Program.cs
class Program
{
    static async Task Main(string[] args)
    {
        DateTime timestamp;

        async Task DoSomething()
        {
            timestamp = DateTime.UtcNow;
            await Task.Delay(1);
        }

        await DoSomething();
        Console.WriteLine(timestamp);
    }
}
  1. Run dotnet build
  2. Observe the error:
MSBuild version 17.5.0-preview-23061-01+040e2a90e for .NET
  Determining projects to restore...
  All projects are up-to-date for restore.
/home/myuser/cs0165/Program.cs(14,27): error CS0165: Use of unassigned local variable 'timestamp' [/home/myuser/cs0165/cs0165.csproj]

Diagnostic Id:

cs0165

Compare to previous version
I compared using a docker image of the previous version of the SDK.

docker run -it mcr.microsoft.com/dotnet/sdk:7.0.103-jammy

Once inside, confirm version:

root@5328a15698be:/# dotnet --version
7.0.103

Then I created the same console app.

dotnet new console -n cs0165

and replace the contents of Program.cs with the code above.

dotnet build
MSBuild version 17.4.1+fedecea9d for .NET
  Determining projects to restore...
  All projects are up-to-date for restore.
  cs0165 -> /cs0165/bin/Debug/net7.0/cs0165.dll

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:03.12

Also confirmed that it runs

root@5328a15698be:/cs0165# dotnet run
02/21/2023 21:52:21
@pieter-venter
Copy link
Author

Leaving this here for posterity's sake:

The language rules are defined here. I think it could be clarified but I'm not a language/rules designer :)

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