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

Incorrect "Use of unassigned local variable" for (x is T y) == false. #20969

Closed
NightElfik opened this issue Jul 19, 2017 · 5 comments
Closed
Assignees
Labels
Area-Compilers Resolution-By Design The behavior reported in the issue matches the current design
Milestone

Comments

@NightElfik
Copy link

Version Used:
Microsoft Visual Studio Community 2017
Version 15.2 (26430.16) Release
VisualStudio.15.Release/15.2.0+26430.16
Microsoft .NET Framework
Version 4.6.01586

Steps to Reproduce:
This produces "Use of unassigned local variable" but it should not.

if ((variable is Type x) == false) {
    return;
}
x.Function();

This should be logically equivalent to code above and it works fine.

if (!(variable is Type x)) {
    return;
}
x.Function();

We like to use == flase instead of ! in our code base for added readability. Unfortunately this does not work with above statement :/ Would it be possible to add it?

Thanks!

@sharwell
Copy link
Member

sharwell commented Jul 19, 2017

We like to use == flase instead of ! in our code base for added readability.

📝 Note that for typical C# developers, the use of == false implies that the left hand side is the nullable type bool? and expressly not of type bool. For example, the following code would need to use an explicit comparison:

static void SomeExtension<T>(this List<T> list)
{
  if (list?.Any() != true)
  {
    // The list is null or empty
    return;
  }

  // do something here...
}

@HaloFour
Copy link

HaloFour commented Jul 19, 2017

As this proposal includes a change to the syntax of the C# language I'd suggest opening it on the C# Language Repository.

Oh nevermind, I assumed that there was a request to allow == false to negate the pattern, not that it already works but trips up definite assignment analysis.

@sharwell

I do know a number of C# developers who prefer using == false to negate Boolean expressions as it stands out more than a !.

@gafter
Copy link
Member

gafter commented Jul 29, 2017

The C# ! operator inverts "definitely assigned when true" and "definitely assigned when false". The use of == false does not do that. According to the rules of definite assignment, the compiler is correct.

@gafter gafter closed this as completed Jul 29, 2017
@gafter gafter added Resolution-By Design The behavior reported in the issue matches the current design and removed Investigation Required labels Jul 29, 2017
@CyrusNajmabadi
Copy link
Member

We like to use == flase instead of ! in our code base for added readability. Unfortunately this does not work with above statement

@NightElfik Very interesting case. As Neal points out, this is currently by design as per the C# language, and dotnet/roslyn is for bugs on the C# compiler.

:/ Would it be possible to add it?

Ah! Quite possibly. For us to do that, we'd need a proposal filed over at https://github.com/dotnet/csharplang . Basically, i think we want a proposal that says that definite assignment should take into account boolean expressions involving == and != when compared to a constant boolean value.

@NightElfik
Copy link
Author

Thanks @CyrusNajmabadi, I have opened a new proposal at dotnet/csharplang.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Resolution-By Design The behavior reported in the issue matches the current design
Projects
None yet
Development

No branches or pull requests

7 participants