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

Use-throw-expression should not be offered when the 'if-block' has additional statements in it #19379

Merged
merged 3 commits into from
May 11, 2017

Conversation

CyrusNajmabadi
Copy link
Member

@CyrusNajmabadi CyrusNajmabadi commented May 9, 2017

Fixes #19377

@CyrusNajmabadi
Copy link
Member Author

Tagging @dotnet/roslyn-ide

@CyrusNajmabadi
Copy link
Member Author

Tagging @MattGertz

Customer scenario

our 'use throw expressoin' analyzer is too aggressive and it gets offered in reasonable code where it shouldn't be offered. This is problematic especially as one can 'fix all' and might end up breaking code unintentionally.

Bugs this fixes:

#19377

Workarounds, if any

None.

Risk

Low.

Performance impact

Low.

Is this a regression from a previous update?

No.

Root cause analysis:

In sufficient checks to only run on an exact user code pattern.

How was the bug found?

Customer reported.

@MattGertz
Copy link
Contributor

Approved pending tests and code reviews.

@CyrusNajmabadi
Copy link
Member Author

retest windows_debug_vs-integration_prtest please

@@ -116,6 +116,12 @@ private void AnalyzeOperation(OperationAnalysisContext context, INamedTypeSymbol
return;
}

if (!IsOnlyStatementOfIf(ifOperation, throwOperation))
Copy link
Member

Choose a reason for hiding this comment

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

💭 It may be more efficient to roll this into GetContainingIfOperation.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good idea.


return ifOperation.IfTrueStatement is IBlockStatement block &&
block.Statements.Length == 1 &&
block.Statements[0].Syntax == throwStatement.Syntax;
Copy link
Member

Choose a reason for hiding this comment

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

💡 I believe the first and last subexpressions are redundant (always true). The full expression could be reduced to:

return ((IBlockStatement)ifOperation.IfTrueStatement).Statements.Length == 1;

@sharwell
Copy link
Member

Oh, I like the way that turned out 👍

@CyrusNajmabadi CyrusNajmabadi merged commit bedaa5e into dotnet:master May 11, 2017
@CyrusNajmabadi CyrusNajmabadi deleted the useThrowExtraStatemnets branch May 11, 2017 17:08
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.

4 participants