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

Handle return statements with no expression in CSharpAsAndNullCheckDi… #25258

Merged
merged 2 commits into from
Mar 7, 2018

Conversation

mavasani
Copy link
Contributor

@mavasani mavasani commented Mar 6, 2018

…agnosticAnalyzer.GetLeftmostCondition

Fixes #25237

Ask Mode template

Customer scenario

Open a C# source file which has a void method with a return; statement. The built-in IDE analyzer CSharpAsAndNullCheckDiagnosticAnalyzer throws a NullReferenceException producing an AD0001 diagnostic in the error list. Multiple instances of this diagnostic can easily show up in a normal C# solution.

Bugs this fixes

#25237

Workarounds, if any

Disable IDE0019 using a ruleset entry for every project that has return statements with no expression.

Risk

Low risk. Fix involves just adding a null check.

Performance impact

None

Is this a regression from a previous update?

Yes, recent regression introduced in 10726d1#diff-64e1a867f5d1363da7ca518cdb658623.

Root cause analysis

Missing unit tests. A regression test has been added with the PR. Verified the NRE on the added unit test before the fix.

How was the bug found?

Dogfooding.

Test documentation updated?

N/A

@mavasani mavasani requested review from CyrusNajmabadi, jinujoseph and a team March 6, 2018 08:21
if (node == null)
{
return null;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider adding the null check above the switch-statement. then we can remove all the null checks inside the switch cases.

Copy link
Member

@CyrusNajmabadi CyrusNajmabadi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@@ -413,6 +413,11 @@ private static SyntaxNode GetLeftmostCondition(SyntaxNode node)
{
while (true)
{
if (node == null)
{
return null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not while (node != null)?

}

node = value;
node = declarators.Count == 1 ? declarators[0].Initializer?.Value : null;
continue;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about having break; in all cases and a return from the default case? might be easier to understand in my opinion than having continue from inside a switch statement

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to keep an escrow regression fix minimal, without style changes. Personally, I also prefer code as per your suggestion, but the current style is also readable as we loop with explicit continue statements and exit when we reach null node or node of unhandled kind.

Copy link
Member

@jasonmalinowski jasonmalinowski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should really just use a while loop. The "non-idiomatic"ness of this made it really hard to follow...might be easier if we clean it up.

@CyrusNajmabadi
Copy link
Member

CyrusNajmabadi commented Mar 6, 2018

+1 on making teh while loop simpler. However, i would still keep the 'continue' form as that make a lot of sense to me. Each block decomposes the node to get the 'left' side, then 'continues' the act of decomposition. 'break' makes me think that something will come 'next', when all that you really want to say is "keep going".

This is effectively a recursive algorithm that is non recursive (because of concerns about large expressions iirc). So keeping the concepts similar between the two would be my preference.

--

That said, i think large expressions here are pretty darn unlikely, so (after ask mode) i think it would be fine to be made recursive.

@jasonmalinowski jasonmalinowski changed the base branch from dev15.7.x to dev15.7-preview1 March 6, 2018 22:08
@mavasani
Copy link
Contributor Author

mavasani commented Mar 6, 2018

@jasonmalinowski @dotnet/roslyn-infrastructure - it seems ubuntu_16_mono_debug_prtest is failing for an unrelated reason, and it is failing in quite a few branches as well.

@jasonmalinowski
Copy link
Member

@mavasani That's fine. This is a branch we just created for these bugfixes, and it doesn't have the fix to fix that. Go ahead and ignore it.

@jinujoseph
Copy link
Contributor

QB approval pending

@jinujoseph
Copy link
Contributor

Approved to merge via link

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

Successfully merging this pull request may close these issues.

6 participants