Add features to offer using conditional expressions ?: over explicit if-statement flows. (squashed)#26572
Add features to offer using conditional expressions ?: over explicit if-statement flows. (squashed)#26572CyrusNajmabadi wants to merge 3 commits intodotnet:masterfrom
?: over explicit if-statement flows. (squashed)#26572Conversation
…t if-statement flows.
|
Tagging @ivanbasov you can review this. I'll incorporate your suggested changes into both branches. |
|
Thank you very much, @CyrusNajmabadi! Will review. |
|
|
||
| // We found a valid local declaration right above the if-statement. | ||
| var localDeclaration = localDeclarationOperation.Syntax; | ||
| var declarator = localDeclarationOperation.Declarations[0].Declarators[0]; |
There was a problem hiding this comment.
var declarator = localDeclarationOperation.Declarations[0].Declarators[0]; [](start = 12, length = 74)
what if there are multiple declarations just above the if?
e.g.
int x = 5, y = M();
if (condition)
{
x = 4;
}
else
{
x = 6;
}
There was a problem hiding this comment.
then we will convert that to:
int x = 5, y = M();
x = condition ? 4 : 6;There was a problem hiding this comment.
Likely, you have a check for the count of local declarations in the method below. Then, a comment in the current method should be enough.
In reply to: 185655094 [](ancestors = 185655094)
There was a problem hiding this comment.
Thank you! I see.
I just want to mention that this can be found when reviewing the method below but not the current one.
I'd add a comment saying something like:
// The call of TryFindMatchingLocalDeclarationImmediatelyAbove above guarantees a single Declaration and a single Declarator.
In reply to: 185656240 [](ancestors = 185656240)
There was a problem hiding this comment.
I updated the helper method to pass back the declarator as well. that just keeps things nice and simple.
| protected override void InitializeWorker(AnalysisContext context) | ||
| => context.RegisterOperationAction(AnalyzeOperation, OperationKind.Conditional); | ||
|
|
||
| private void AnalyzeOperation(OperationAnalysisContext context) |
There was a problem hiding this comment.
AnalyzeOperation [](start = 21, length = 16)
If I'm not mistaken, this method completely coincides with the same method in AbstractUseConditionalExpressionForAssignmentDiagnosticAnalyzer. Should not we create a base class for both them sharing the method?
There was a problem hiding this comment.
i'll take a looksie.
There was a problem hiding this comment.
Done. Great suggestion!
|
@jinujoseph for ask-mode approval for 15.8. Thanks |
|
@jinujoseph Sorry, the present PR is actually a squashed version of #26236. Please approve that one (#26236). |
?: over explicit if-statement flows.?: over explicit if-statement flows. (squashed)
|
Closing in favor of #26236 |
[jcouv edited] This PR is a squashed version of #26236
For example, with:
We offer:
Todo:
?:expression.if-statements withreturnstatements in their bodies.For the last one, this means recognizing:
and offering to convert to:
[jcouv edited] Fixes #26539