-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
CS8602: Incorrect nullable flow analysis for indirect null check #49653
Comments
Duplicate of #48354, which was closed as by-design. |
@Youssef1313 no, this one is different. Compiler can know that |
Then adding @jcouv to have a look. |
This is by design. When you null check a variable, we don't attempt to infer what other variables could have their flow state modified based on that null check. |
But there IS a null check on
string? b;
if (a == null || (b = a.ToString()) == null) return;
if ((a?.ToString()) == null) return;
string? b;
if ((b = a?.ToString()) == null) return; Also as already stated above, any other analyzer claiming to be state of the art and industrial standard (CodeContracts, ReSharper, SonarCube) can do this right. |
@RikkiGibson and @Youssef1313 are correct that this is behaving as designed. The flow checking engine used by the compiler does not handle cases where the result of a null check is stored in a
This works because it includes a direct check of
Same as above.
This is not the same as it involves extra constructs, notably the assignment, that the compiler must include in its flow analysis. Sure it's a case we could add into the analysis but at the moment it's not part of the included set. |
@jaredpar I raised this issue because this is not part of the included set. |
The roslyn repository holds the compiler code base which is the implementation of the language design. At this point the compiler is correctly implementing the language design which does not include this level of requests. I agree this could be a feature request but it should be a feature request on the language in dotnet/csharplang. |
I see, this was the missing information. I'll create a request in that repo. |
@jaredpar dotnet/csharplang is only about the language spec - but this is not about a new spec. |
dotnet/csharplang is about the language spec, features and design alterations to existing features. |
That is the C# 8 version of the spec, probably want to refer to the 9.0 one |
OK, only found this related to the problem: var person = new Person();
// The receiver is a tracked expression hence the member_access of the property
// is tracked as well
if (person.FirstName is not null)
{
Use(person.FirstName);
} So doesn't this mean it should work? |
Or where in this document is written that the flow analysis should not work in that case? |
Specs of this nature are generally focused on discussing the places in which the behavior takes place. Specifically cases in which we will track the |
@jaredpar I've created the proposal in issues, but it has been moved to discussions: dotnet/csharplang#4208 |
Microsoft Visual Studio Professional 2019
Version 16.8.2
Steps to Reproduce:
Expected Behavior:
No warning in
var c = a.ToString();
Actual Behavior:
CS8602
The text was updated successfully, but these errors were encountered: