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

Invalid "Use of unassigned variable". #47585

Closed
CheloXL opened this issue Sep 10, 2020 · 5 comments
Closed

Invalid "Use of unassigned variable". #47585

CheloXL opened this issue Sep 10, 2020 · 5 comments

Comments

@CheloXL
Copy link

CheloXL commented Sep 10, 2020

Version Used:
Using .net5/preview 8

Steps to Reproduce:
The following code was compiling just fine with the 3.1 sdk:

private static string? FindRequestValue(HttpRequest? request, string value)
{
	if (request?.Headers?.TryGetValue(value, out var headers) != true)
	{
		return null;
	}

	return headers.Count > 0 ? headers[0] : null;
}

Expected Behavior:
Code compiles

Actual Behavior:
Error compiling: Use of unassigned local variable 'headers' in the return line.

@Youssef1313
Copy link
Member

Youssef1313 commented Sep 10, 2020

Is "TryGetValue" a method that you defined?

I cannot find it in the API reference: https://docs.microsoft.com/en-us/dotnet/api/system.collections.specialized.namevaluecollection?view=net-5.0

Also HttpRequest doesn't seem to be available (https://docs.microsoft.com/en-us/dotnet/api/system.web.httprequest?view=net5.0).

Here is another repro to simplify tracking this for the compiler team:

#nullable enable

using System;

public class MyClass
{
    public bool TryGetSomething(out string something)
    {
        something = "Something";
        return true;
    }
}

public class C
{     
    public static void MyMethod(MyClass? myHeader)
    {
        if (myHeader?.TryGetSomething(out var something) != true)
        {
            return;
        }
        Console.WriteLine(something);
    }
}

@CheloXL
Copy link
Author

CheloXL commented Sep 10, 2020

The project is a netcoreapp3.1. You have to include in your csproj the following line: <FrameworkReference Include="Microsoft.AspNetCore.App" /> to have access to the HttpRequest.

Anyways, the problem is related to the code analysis.

If I change the code to

if (request?.Headers == null)
{
	return null;
}

if (!request.Headers.TryGetValue(value, out var headers))
{
	return null;
}

return headers.Count > 0 ? headers[0] : null;

The method compiles without issues.

@RikkiGibson
Copy link
Contributor

Duplicate of #45582.

Many people have requested this change, but a definite assignment spec change is required first.

@bpaczkowski
Copy link

@RikkiGibson I'm not sure if it's the same issue. The issue described in this report is a change in .NET 5.0, when compiled to .NET Core 3.1 it compiles without errors. That would mean that before .NET 5.0 it wasn't working as per spec, but in .NET 5.0 it was fixed, which is causing this?

@RikkiGibson
Copy link
Contributor

That would mean that before .NET 5.0 it wasn't working as per spec, but in .NET 5.0 it was fixed, which is causing this?

Yeah. What's happening is most likely similar to #52834 (comment)

We expect to fix this scenario in C# 10. dotnet/csharplang#4465

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

No branches or pull requests

5 participants