-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Update 'use null propagation' to understand more patterns #78992
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
Conversation
|
||
// we have `if (x != null) x.Y();`. Update `x.Y()` to be `x?.Y()`, then replace the entire | ||
// if-statement with that expression statement. | ||
var newWhenTrueStatement = CreateConditionalAccessExpression( | ||
syntaxFacts, generator, whenPartIsNullable, whenTrueStatement, match); | ||
Contract.ThrowIfNull(newWhenTrueStatement); | ||
|
||
var isElseIf = syntaxFacts.IsElseClause(ifStatement.Parent); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i reordered thids pretty heavily. it had this local variable to indicate if we are in the else if (x != null) x.Foo()
case. And then lots of contorted logic doing work, but throwing in random checks of this local value. So i just split things into an if/else where we keep all the logic for this case in one place, and then handle the normal if
case below that.
|
||
Protected Overrides Function ReplaceBlockStatements(block As ExecutableStatementSyntax, newInnerStatement As ExecutableStatementSyntax) As ExecutableStatementSyntax | ||
Throw ExceptionUtilities.Unreachable() | ||
End Function |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not needed for VB. we now just call into C# to handle the entire case of else if
, which means it can just inline the tryGetBlock and replaceblockstatements within PostProcessElseIf. VB has no need for it as PostProcessElseIf is never called for it.
// `if (<expr> != null) { <expr>.Method(); <expr> = null; }` | ||
// | ||
// If 'expr' is not null, then we execute the body and then end up with expr being null. So `expr?.Method(); expr = null;` | ||
// preserves those semantics. Simialrly, if is expr is null, then `expr?.Method();` does nothing, and `expr = null` keeps it |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// preserves those semantics. Simialrly, if is expr is null, then `expr?.Method();` does nothing, and `expr = null` keeps it | |
// preserves those semantics. Similarly, if is expr is null, then `expr?.Method();` does nothing, and `expr = null` keeps it |
Specifically, if you write:
Then this is fine to translate to:
This is a somewhat common pattern we see in cleanup methods (like Dispose) where an operation is done on a field if it is non-null, and then it is null'ed out.